forked from PlanBCom/hubot-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.js
182 lines (159 loc) · 5.13 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
// Description
// A hubot script to get google analytics reports
//
// Configuration:
// GOOGLE_API_CLIENT_EMAIL
// GOOGLE_API_PRIVATE_KEY
//
// Commands:
// analytics profiles - Shows profiles to which the bot has access
// analytics pageviews 123123123 - Shows pageviews and visits of website with id 123123123
// analytics devices 123456 - Get percentage mobile x desktop access of website with id 123456
// analytics browsers 654321 - Get browsers percentage access with id 654321
// analytics 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 analytics = google.analytics("v3");
var globalError;
module.exports = function(robot) {
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 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 response = entries.items.map(function(item) {
return item.id + " - " + item.name;
}).join("\n");
return res.reply(response);
}
);
});
});
robot.hear(/analytics pageviews\s+(\d+)/i, function(res)
{
var siteId = res.match[1];
var today = Date.today();
var startDate = today.removeDays(30).toYMD("-");
var endDate = today.toYMD("-");
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"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+(\d+)/i, function(res)
{
var siteId = res.match[1];
var today = Date.today();
var startDate = today.removeDays(30).toYMD("-");
var endDate = today.toYMD("-");
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"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'])
var 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+(\d+)/i, function(res)
{
var siteId = res.match[1];
var today = Date.today();
var startDate = today.removeDays(30).toYMD("-");
var endDate = today.toYMD("-");
if(globalError) {
return res.reply(globalError);
}
oauth2Client.authorize(function(err)
{
analytics.data.ga.get({
auth: oauth2Client,
"ids": "ga:"+siteId,
"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);
}
// console.log(entries);
var total = parseInt(entries.totalsForAllResults['ga:sessions'])
var 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 email/i, function(res)
{
return res.send(GOOGLE_API_CLIENT_EMAIL||"Blank - you must config your environment variables.");
});
};