-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanalytics.js
211 lines (179 loc) · 6.48 KB
/
analytics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Description
// A hubot script to get google analytics reports
//
// Configuration:
// GOOGLE_API_CLIENT_EMAIL
// GOOGLE_API_PRIVATE_KEY
//
// Commands:
// analytics help - Returns a list of commands for this plugin
// analytics profiles - Shows profiles to which the bot has access
// analytics pageviews "Site Name or ID" - Shows pageviews and visits of website with "Site Name or ID"
// analytics devices "Site Name or ID" - Get percentage mobile x desktop access of website with "Site Name or ID"
// analytics browsers "Site Name or ID" - Get browsers percentage access with "Site Name or ID"
// analytics show email - Get email account api configured to give access to others analytics profiles.
//
// Notes:
// <optional notes required for the script>
//
// Author:
// Plan B Comunicação <[email protected]>
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;
var GOOGLE_API_PRIVATE_KEY = process.env.GOOGLE_API_PRIVATE_KEY.replace(/\\n/g, "\n");
var oauth2Client = new google.auth.JWT(GOOGLE_API_CLIENT_EMAIL, null, GOOGLE_API_PRIVATE_KEY, ["https://www.googleapis.com/auth/analytics.readonly"], null);
} catch(err) {
globalError = "Error on load - check your environments variables GOOGLE_API_CLIENT_EMAIL and GOOGLE_API_PRIVATE_KEY.";
}
robot.hear(/analytics help/i, function(res)
{
var helpTxt = "\nanalytics help - Returns a list of commands for this plugin\n" +
"analytics profiles - Shows profiles to which the bot has access\n" +
"analytics pageviews \"Site Name or ID\" - Shows pageviews and visits of website with \"Site Name or ID\"\n" +
"analytics devices \"Site Name or ID\" - Get percentage mobile x desktop access of website with \"Site Name or ID\"\n" +
"analytics browsers \"Site Name or ID\" - Get browsers percentage access with \"Site Name or ID\"\n" +
"analytics show email - Get email account api configured to give access to others analytics profiles.";
return res.send(helpTxt);
});
robot.hear(/analytics profiles/, function(res)
{
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.management.profiles.list(
{
auth: oauth2Client,
accountId: "~all",
webPropertyId: "~all"
},
function(err, entries) {
if (err) {
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+("[\w .\-\[\]]+")$/i, function(res)
{
var site = hubotAnalytics.getSiteId(res.match[1]);
var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:" + site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:visits, ga:pageviews"
},
function(err, entries) {
if (err) {
console.log(err);
return res.reply(err);
}
var visits = entries.totalsForAllResults["ga:visits"];
var pageviews = entries.totalsForAllResults["ga:pageviews"];
var profileName = entries.profileInfo.profileName;
return res.reply(profileName+": "+visits+" visits and "+pageviews+" pageviews.");
});
});
});
robot.hear(/analytics devices?\s+("[\w .\-\[\]]+")$/i, function(res)
{
var site = hubotAnalytics.getSiteId(res.match[1]);
var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");
var result = "";
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:" + site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:sessions",
"dimensions": "ga:deviceCategory"
},
function(err, entries) {
if (err) {
console.log(err);
return res.reply(err);
}
var total = parseInt(entries.totalsForAllResults["ga:sessions"]);
if(total>0) {
result = entries.rows.map(function(item) {
var percentage = (parseInt(item[1]) / total) * 100;
return item[0] + " - " + item[1] + " sessions (" + (percentage.toFixed(2)) + "%)";
}).join("\n");
}
return res.reply(result);
});
});
});
robot.hear(/analytics browsers?\s+("[\w .\-\[\]]+")$/i, function(res)
{
var site = hubotAnalytics.getSiteId(res.match[1]);
var startDate = Date.today().removeDays(30).toYMD("-");
var endDate = Date.today().toYMD("-");
var result = "";
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+site,
"start-date": startDate,
"end-date": endDate,
"metrics": "ga:sessions",
"dimensions": "ga:browser",
"sort": "-ga:sessions"
},
function(err, entries) {
if (err) {
console.log(err);
return res.reply(err);
}
var total = parseInt(entries.totalsForAllResults['ga:sessions'])
if(total>0) {
result = entries.rows.map(function(item) {
var percentage = (parseInt(item[1]) / total) * 100;
return item[0] + " - " + item[1] + " sessions (" + (percentage.toFixed(2)) + "%)";
}).join("\n");
}
return res.reply(result);
});
});
});
robot.hear(/analytics show email/i, function(res)
{
return res.send(GOOGLE_API_CLIENT_EMAIL||"Blank - you must config your environment variables.");
});
};