-
Notifications
You must be signed in to change notification settings - Fork 8
/
ga-controller.js
61 lines (53 loc) · 1.9 KB
/
ga-controller.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
var google = require("googleapis");
var ga = google.analytics("v3");
var moment = require("moment");
module.exports = function (app) {
var gaQuery = function (req, res, options) {
var jwt = new google.auth.JWT(
options.serviceEmail,
options.pemFilename,
null,
["https://www.googleapis.com/auth/analytics.readonly"],
null);
if (app.get("env") !== "development") {
res.header("Access-Control-Allow-Origin", options.origin);
}
jwt.authorize(function (err, tokens) { // eslint-disable-line no-unused-vars
if (err) {
console.log("jwt error", err);
return;
}
ga.data.ga.get({
auth: jwt,
ids: options.accountId,
"start-date": "2014-01-01",
"end-date": moment().format("YYYY-MM-DD"),
"metrics": "ga:pageviews",
"filters": "ga:pagePath==/" + options.pathRoot + "/" + (req.query.path || "")
}, function (err, result) {
if (err) {
console.log("api error", err, result);
}
res.send(result && result.rows && result.rows[0] ? result.rows[0][0] : "(unknown)");
});
});
};
app.get("/ga", function (req, res) {
gaQuery(req, res, {
serviceEmail: process.env.SERVICE_EMAIL,
pemFilename: "grnsight.pem",
origin: "https://dondi.github.io",
accountId: "ga:91279024",
pathRoot: "GRNsight"
});
});
app.get("/grnmap", function (req, res) {
gaQuery(req, res, {
serviceEmail: process.env.GRNMAP_SERVICE_EMAIL,
pemFilename: "grnmap.pem",
origin: "http://kdahlquist.github.io",
accountId: "ga:97014398",
pathRoot: "GRNmap"
});
});
};