-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (92 loc) · 2.71 KB
/
index.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
var
util = require('util'),
path = require('path'),
useragent = require('ua-parser-js'),
fs = require('fs'),
joolaio = require('joola.io.sdk');
module.exports = function (options) {
options = util._extend({
readKey: null,
writeKey: null,
endPoint: '/analytics',
host: 'http://reflect.io:8080',
engine: {
debug: {
enabled: false,
level: 'warn'
}
}
}, options);
var joola_ready = false;
joolaio.init(joolaio.common.extend({
host: options.host,
APIToken: options.writeKey
}, options.engine), function (err, result) {
if (err)
return console.error('Failed to initialize joola.io', err);
joola_ready = true;
});
return function (req, res, next) {
if (!joola_ready)
return next();
var parser = new useragent();
parser.setUA(req.header('user-agent'));
var start = new Date(),
method = req.method,
url = req.url || '-',
referer = req.header('referer') || '-',
ua = parser.getResult(),
httpVersion = req.httpVersionMajor + '.' + req.httpVersionMinor,
ip = ip || req.ip || req.connection.remoteAddress ||
(req.socket && req.socket.remoteAddress) ||
(req.socket.socket && req.socket.socket.remoteAddresss) ||
'127.0.0.1';
if (res._analytics)
return next();
res._analytics = true;
res.on('header', function () {
var duration = new Date() - start;
var status = res.statusCode;
var message = {
timestamp: null,
statusCode: status,
httpVersion: httpVersion,
referer: referer,
url: url,
method: method,
ua: ua,
ip: ip,
duration: duration,
requests: 1
};
joolaio.beacon.insert('expressanalytics', message, function (err) {
if (err)
return console.error('Failed to save beacon message', err);
});
});
if (req.url === options.endPoint) {
return res.render(path.join(__dirname, '/content/views/analytics'), {readKey: options.readKey});
}
else if (req.url === '/express.analytics.js') {
fs.readFile(path.join(__dirname, 'node_modules/joola.io.sdk/bin/joola.io.js'), function (err, data) {
if (err)
return next(err);
fs.readFile(path.join(__dirname, 'content/js/analytics.js'), function (err, jsdata) {
if (err)
return next(err);
res.end(data + '\n' + jsdata);
});
});
}
else if (req.url === '/express.analytics.css') {
fs.readFile(path.join(__dirname, 'content/css/analytics.css'), function (err, data) {
if (err)
return next(err);
res.end(data);
});
}
else {
return next(null);
}
};
};