diff --git a/package.json b/package.json index 032081d..66e2557 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", "keywords": "hubot, hubot-scripts, google analytics, ga", diff --git a/src/analytics.js b/src/analytics.js index 4173c0b..9a7b271 100644 --- a/src/analytics.js +++ b/src/analytics.js @@ -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; @@ -48,6 +52,7 @@ module.exports = function(robot) { return res.send(helpTxt); }); + robot.hear(/analytics profiles/, function(res) { if(globalError) { @@ -66,10 +71,15 @@ 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); } ); @@ -77,9 +87,9 @@ module.exports = function(robot) { }); - 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("-"); @@ -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" @@ -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 = ""; @@ -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", @@ -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 = ""; @@ -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", @@ -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."); diff --git a/src/lib/HubotAnalytics.js b/src/lib/HubotAnalytics.js new file mode 100644 index 0000000..5499944 --- /dev/null +++ b/src/lib/HubotAnalytics.js @@ -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;