Skip to content

Commit

Permalink
#141: improve unique users tracking, refactor trackStats.event
Browse files Browse the repository at this point in the history
  • Loading branch information
robhrt7 committed Aug 31, 2015
1 parent 300a6c9 commit 491dbbb
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 35 deletions.
30 changes: 25 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@ var log = logger.log;
global.log = log;

if (commander.html) {
trackStats.staticEvent('features', 'enabled html parser');
trackStats.event({
group: 'features',
event: 'enabled html parser'
});

global.opts.plugins.htmlParser.enabled = true;
global.opts.plugins.htmlParser.onStart = true;
}
if (commander.port) global.opts.core.server.port = parseInt(commander.port);
if (commander.hostname) global.opts.core.server.hostname = commander.hostname;
if (!commander.watch) {
trackStats.staticEvent('features', 'disabled watch');
trackStats.event({
group: 'features',
event: 'disabled watch'
});
global.opts.core.watch.enabled = false;
}
/* /Globals */
Expand Down Expand Up @@ -97,7 +103,15 @@ app.use(require('express-session')({
app.use(function (req, res, next) {
res.cookie('source-mode', global.MODE, { maxAge: 3600000, httpOnly: false });

// keep executing the router middleware
next();
});

var shortid = require('shortid');
app.use(function (req, res, next) {
if (req.cookies && !req.cookies['source-track']) {
res.cookie('source-track', shortid.generate(), { maxAge: 3600000, httpOnly: true });
}

next();
});

Expand Down Expand Up @@ -250,9 +264,15 @@ if (!module.parent) {
});
} else {
if (global.opts.core.common.trackAnonymusStatistics) {
trackStats.staticEvent('start', 'default');
trackStats.event({
group: 'start',
event: 'default'
});
} else {
trackStats.staticEvent('start', 'no stats', true);
trackStats.event({
group: 'start',
event: 'no stats'
}, true);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion core/middlewares/clarify.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ module.exports.process = function(req, res, next) {
html = msg;
}

trackStats.page('clarify', req.sessionID);
trackStats.page({
pageName: 'clarify',
sessionID: trackStats.getSessionID(req)
});

res.send(html);
}).fail(function(err) {
Expand Down
66 changes: 41 additions & 25 deletions core/trackStats.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,75 @@
'use strict';

var url = require('url');
var _ = require('lodash');
var path = require('path');
var macaddress = require('macaddress');
var ua = require('universal-analytics');
var log = require(path.join(global.pathToApp, 'core/logger')).log;
var crypto = require('crypto');

if (global.commander && global.commander.test) {
global.opts.core.common.trackAnonymusStatistics = false;
}

var generateMachineID = function(){
var macNums = macaddress.networkInterfaces();
var unique = '';
var macItem;

for (macItem in macNums) {
var val = macNums[macItem];

_.forOwn(macNums, function(val){
if (val.mac) {
unique += val.mac;
} else if (val.ipv4) {
unique += val.ipv4;
}
}
});

return 'host_' + crypto.createHash('md5').update(unique).digest('hex').slice(0, 5);
};

var staticVisitor = ua('UA-66924051-1', generateMachineID(), {strictCidFormat: false});
var machineID = generateMachineID();
var hostVisitor = ua('UA-66924051-1', machineID, {strictCidFormat: false});

// Track page visits by unique session ID
var trackPage = function(opts){
if (!global.opts.core.common.trackAnonymusStatistics) return;
var _trackPage = function(opts){
if (!global.opts.core.common.trackAnonymusStatistics || !opts.pageName) return;

var visitor = ua('UA-66924051-1', opts.sessionID, {strictCidFormat: false});
var visitor = opts.sessionID ? ua('UA-66924051-1', opts.sessionID, {strictCidFormat: false}) : hostVisitor;

log.trace('track page', opts.pageName);
log.trace('as a visitor', visitor);

visitor.pageview(opts.pageName).send();
};

// Track host-initiated events (by unique machine id)
var staticEvent = function(group, event, force){
if (!force && !global.opts.core.common.trackAnonymusStatistics) return;
var _trackEvent = function(opts, force){
if (!force && !global.opts.core.common.trackAnonymusStatistics || !opts.event) return;

log.trace('track event', group, event);
var visitor = opts.sessionID ? ua('UA-66924051-1', opts.sessionID, {strictCidFormat: false}) : hostVisitor;
var group = opts.group || 'default';

staticVisitor.event(group, event).send();
log.trace('track event', group + ':' + opts.event);
log.trace('as a visitor', visitor);

visitor.event(group, opts.event).send();
};

var getSessionID = module.exports.getSessionID = function(req) {
var sessionID = req.sessionID;
var host = req.headers && req.headers.host ? req.headers.host : undefined;

if (host && (/^localhost/.test(host) || /^127.0.0.1/.test(host))) {
sessionID = machineID;
} else if (req.cookies && req.cookies['source-track']) {
sessionID = req.cookies['source-track'];
}

return sessionID;
};

// Track specs
module.exports.specs = function(req) {
var pageName = 'spec';
var sessionID = req.sessionID;

var parsedUrl = url.parse(req.url, true);
var q = parsedUrl.query || {};
Expand All @@ -60,17 +82,11 @@ module.exports.specs = function(req) {
pageName = 'navigation';
}

trackPage({
sessionID: sessionID,
pageName: pageName
});
};

module.exports.page = function(pageName, sessionID) {
trackPage({
sessionID: sessionID,
_trackPage({
sessionID: getSessionID(req),
pageName: pageName
});
};

module.exports.staticEvent = staticEvent;
module.exports.page = _trackPage;
module.exports.event = _trackEvent;
12 changes: 9 additions & 3 deletions core/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ var options = global.opts = loadOptions(enginePath);
var trackStats = require(path.join(global.pathToApp, 'core/trackStats'));

if (options && options.core.common && options.core.common.trackAnonymusStatistics) {
trackStats.staticEvent('install', 'default');
trackStats.event({
group: 'install',
event: 'default'
});

console.log('\n[SOURCEJS] Note: engine tracks anonymous usage statistics. To disable it, edit `core.common.trackAnonymusStatistics` configuration.\n');
console.log('[SOURCEJS] Note: engine tracks anonymous usage statistics. To disable it, edit `core.common.trackAnonymusStatistics` configuration.\n');
} else {
trackStats.staticEvent('install', 'no stats', true);
trackStats.event({
group: 'install',
event: 'no stats'
}, true);
}
2 changes: 1 addition & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = {
// Name of options field in info.json, used to override configuration per spec
infoFileOptions: 'sourcejs',

trackAnonymusStatistics: true
trackAnonymusStatistics: false
},

// Server options are passed to app.listen (https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"pretty-hrtime": "^1.0.0",
"q": "^1.1.1",
"serve-favicon": "^2.2.0",
"shortid": "^2.2.2",
"time-grunt": "~0.2.10",
"tinyforever": "0.0.3",
"universal-analytics": "^0.3.9"
Expand Down

0 comments on commit 491dbbb

Please sign in to comment.