-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathanalytics.js
53 lines (45 loc) · 1 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
'use strict';
var ua = require('universal-analytics');
var url = require('url');
var extend = require('extend');
var config = require('./config');
function pageview(options) {
return function(req, res, next) {
var defaults = {
dp: req.path,
dh: _getUrl(req),
dr: req.get('Referer'),
uip: req.get('X-Real-IP') || req.ip,
ua: req.get('User-Agent'),
};
if (req.visitor) {
req.visitor
.pageview(extend(defaults, options))
.send();
}
next();
};
}
function sendEvent(req, category, action, label, value, options) {
var defaults = {
t: 'event',
ni: true,
ec: category,
ea: action,
el: label,
ev: value,
};
pageview(extend(defaults, options))(req, {}, function(){});
}
function _getUrl(req) {
return url.format({
protocol: req.protocol,
host: req.get('host'),
});
}
module.exports = {
trackingId: config.analytics.tracking_id,
middleware: ua.middleware,
pageview: pageview,
sendEvent: sendEvent,
};