Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.0 #14

Merged
merged 2 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-analytics",
"description": "A hubot script to get google analytics reports",
"version": "0.8.2",
"version": "1.0.0",
"author": "Plan B Comunicação <[email protected]>",
"license": "MIT",
"keywords": "hubot, hubot-scripts, google analytics, ga",
Expand Down
32 changes: 23 additions & 9 deletions src/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
var google = require("googleapis");
require("date-utils");

var HubotAnalytics = require('./lib/HubotAnalytics.js');
var hubotAnalytics = new HubotAnalytics();

var analytics = google.analytics("v3");
var globalError;

module.exports = function(robot) {
hubotAnalytics.profilesNames = robot.brain.get('profilesNames');

try {
var GOOGLE_API_CLIENT_EMAIL = process.env.GOOGLE_API_CLIENT_EMAIL;
Expand All @@ -48,6 +52,7 @@ module.exports = function(robot) {
return res.send(helpTxt);
});


robot.hear(/analytics profiles/, function(res)
{
if(globalError) {
Expand All @@ -66,20 +71,25 @@ module.exports = function(robot) {
return res.reply(err);
}

var mapProfilesNames = {};
var response = entries.items.map(function(item) {
mapProfilesNames[item.name] = item.id; //hash by name
return item.id + " - " + item.name;
}).join("\n");

robot.brain.set('profilesNames', mapProfilesNames); // store in brain
hubotAnalytics.profilesNames = mapProfilesNames;

return res.reply(response);
}
);
});
});


robot.hear(/analytics pageviews\s+(\d+)/i, function(res)
robot.hear(/analytics pageviews\s+("[\w .\-\[\]]+")$/i, function(res)
{
var siteId = res.match[1];
var site = hubotAnalytics.getSiteId(res.match[1]);
var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");

Expand All @@ -90,7 +100,7 @@ module.exports = function(robot) {
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"ids": "ga:" + site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:visits, ga:pageviews"
Expand All @@ -110,9 +120,10 @@ module.exports = function(robot) {
});


robot.hear(/analytics devices?\s+(\d+)/i, function(res)
robot.hear(/analytics devices?\s+("[\w .\-\[\]]+")$/i, function(res)
{
var siteId = res.match[1];
var site = hubotAnalytics.getSiteId(res.match[1]);

var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");
var result = "";
Expand All @@ -124,7 +135,7 @@ module.exports = function(robot) {
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"ids": "ga:" + site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:sessions",
Expand All @@ -149,9 +160,11 @@ module.exports = function(robot) {
});
});

robot.hear(/analytics browsers?\s+(\d+)/i, function(res)

robot.hear(/analytics browsers?\s+("[\w .\-\[\]]+")$/i, function(res)
{
var siteId = res.match[1];
var site = hubotAnalytics.getSiteId(res.match[1]);

var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");
var result = "";
Expand All @@ -163,7 +176,7 @@ module.exports = function(robot) {
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"ids": "ga:"+site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:sessions",
Expand All @@ -189,6 +202,7 @@ module.exports = function(robot) {
});
});


robot.hear(/analytics show email/i, function(res)
{
return res.send(GOOGLE_API_CLIENT_EMAIL||"Blank - you must config your environment variables.");
Expand Down
19 changes: 19 additions & 0 deletions src/lib/HubotAnalytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

function HubotAnalytics() {
this.profilesNames = "ok";
}

HubotAnalytics.prototype.getSiteId = function(siteMatch) {
var site = siteMatch;
var targetGa = site.replace(/"/g, '');

var tryValueByName = this.profilesNames[targetGa];
if(tryValueByName != undefined) {
targetGa = tryValueByName;
}

return targetGa;
};


module.exports = HubotAnalytics;