-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (47 loc) · 1.28 KB
/
index.js
File metadata and controls
64 lines (47 loc) · 1.28 KB
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
'use strict';
var ua, url, redis, visitor, visitors, rtg, GA_ID, http;
// Enable New Relic
if (process.env.NEW_RELIC_LICENSE_KEY) {
require('newrelic');
}
// HTTP server
http = require('http');
// Google Analytics
ua = require('universal-analytics');
GA_ID = 'UA-47963532-1';
// Connect to redis
url = require('url');
rtg = url.parse(process.env.REDIS_URL);
redis = require('redis').createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(':')[1]);
// Store visitors
visitors = {};
// Cache visitors
visitor = function (id) {
return visitors.hasOwnProperty(id) ?
visitors[id] :
visitors[id] = ua(GA_ID);
};
redis.on('subscribe', function (channel, count) {
console.log('Listening to events on ' + channel);
});
redis.on('message', function (channel, message) {
var id, category, event;
console.log('message', channel, message);
id = message.match(/^(\d+)\|/);
id = id ? id[1] : undefined;
message = message.match(/\|?(\w+)\.(\w+)$/);
category = message[1];
event = message[2];
console.log({
id: id,
category: category,
event: event
});
visitor(id).event(category, event, id).send();
});
redis.subscribe('analytics');
// Keep heroku happy
http.createServer(function (req, res) {
res.end('hello world');
}).listen(process.env.PORT || 8000);