-
Notifications
You must be signed in to change notification settings - Fork 30
/
googleMeasurement.js
48 lines (41 loc) · 992 Bytes
/
googleMeasurement.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
var request = require('request');
var querystring = require('querystring');
var baseURL = "https://www.google-analytics.com/";
var Measurements = function(tid) {
this.tid = tid;
};
Measurements.prototype.send = function(options) {
if (!this.tid){
return;
}
var required = {
v: 1,
tid: this.tid,
t: 'event'
};
var url = baseURL;
var body;
if (Array.isArray(options)) {
for (var i=0; i<options.length && i < 20; i++) {
var temp = Object.assign(required, options[i]);
body += querystring.stringify(temp);
body += "\n";
}
url += "batch";
} else {
options = Object.assign(required, options);
body = querystring.stringify(options);
url += "collect";
}
// console.log(body);
request.post({url: url, body: body},
function(err, response, body){
if (err) {
console.log(err);
}
// console.log("Analytics response: %d",response.statusCode);
// console.log(response.headers);
// console.log(body);
});
}
module.exports = Measurements;