Skip to content

Commit

Permalink
[#21] Track events on Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorbaptista committed Oct 19, 2015
1 parent 8f9c8dc commit 1e54fd0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
53 changes: 53 additions & 0 deletions analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.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,
};
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var errorHandler = require('errorhandler');

var config = require('./config');
var db = require('./db');
var analytics = require('./analytics');

var indexRoute = require('./routes/index');
var roomsRoute = require('./routes/rooms');

Expand All @@ -30,6 +32,10 @@ if (config.env == 'development') {
res.locals.isDev = true;
next();
});
} else {
if (analytics.trackingId) {
app.use(analytics.middleware(analytics.trackingId));
}
}

app.use('/', indexRoute);
Expand Down
4 changes: 4 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ config.mongodb = {
config.cache = {
ttl: 3600,
};

config.analytics = {
tracking_id: process.env.GOOGLE_ANALYTICS_TID,
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"browserify": "11.2.0",
"errorhandler": "1.4.2",
"express": "4.13.3",
"extend": "3.0.0",
"gulp": "3.9.0",
"gulp-minify-css": "1.2.1",
"gulp-rename": "1.2.2",
Expand All @@ -24,6 +25,7 @@
"node-cache": "3.0.0",
"socket.io": "1.3.7",
"socket.io-client": "^1.3.7",
"universal-analytics": "0.3.9",
"vinyl-source-stream": "^1.1.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

var express = require('express');
var analytics = require('../analytics');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
res.render('index');
next();
}, analytics.pageview());

module.exports = router;
18 changes: 15 additions & 3 deletions routes/rooms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var express = require('express');
var analytics = require('../analytics');
var router = express.Router();
var roomPrefix;
var io;
Expand All @@ -11,19 +12,28 @@ var roomsModel;
router.get('/:room', function(req, res, next) {
res.render('room');
next();
});
}, analytics.pageview());

/* POST room. */
router.post('/:room', function(req, res, next) {
authorizeOrDie(req, res, function() {
var room = req.url;
var size = JSON.parse(req.body.size);
var message = req.body.message;
var analyticsEventOptions = {};

io.sockets.in(room).emit('size', size);
io.sockets.in(room).emit('message', message);

roomsModel.push(room, size, message);

if (size.rows && size.cols) {
// Use screenResolution as the terminal's size.
analyticsEventOptions.sr = size.rows + 'x' + size.cols;
}
analytics.sendEvent(req, 'rooms', 'write', room, message.length,
analyticsEventOptions);

res.sendStatus(200);
});
});
Expand All @@ -32,8 +42,8 @@ router.post('/:room', function(req, res, next) {
router.delete('/:room', function(req, res, next) {
authorizeOrDie(req, res, function() {
var room = req.url;
roomsModel.drop(room, function(err, col) {
});
roomsModel.drop(room);
analytics.sendEvent(req, 'rooms', 'delete', room);
res.sendStatus(202);
});
});
Expand All @@ -45,8 +55,10 @@ function authorizeOrDie(req, res, callback) {
// FIXME: secret might be empty
authorizationModel.isAuthorized(room, secret, function(authorized) {
if (!authorized) {
analytics.sendEvent(req, 'rooms', 'authorization', 'failure');
res.sendStatus(401);
} else {
analytics.sendEvent(req, 'rooms', 'authorization', 'success');
callback();
}
});
Expand Down

0 comments on commit 1e54fd0

Please sign in to comment.