Skip to content

Commit

Permalink
#141: add tracking refferer, refactor settings format
Browse files Browse the repository at this point in the history
  • Loading branch information
robhrt7 committed Sep 8, 2015
1 parent 3de7279 commit 0746042
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ if (!module.parent) {
}
});
} else {
if (global.opts.core.common.trackAnonymusStatistics) {
if (global.opts.core.tracking.enabled) {
trackStats.event({
group: 'start',
event: 'default'
Expand Down
6 changes: 3 additions & 3 deletions core/warn.js → core/trackInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ var options = global.opts = loadOptions(enginePath);
var trackStats = require(path.join(global.pathToApp, 'core/trackStats'));

if (process.env.CI) {
global.opts.core.common.trackAnonymusStatistics = false;
options.core.tracking.enabled = false;
}

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

console.log('[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.tracking` configuration.');
} else {
trackStats.event({
group: 'install',
Expand Down
51 changes: 42 additions & 9 deletions core/trackStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ var path = require('path');
var macaddress = require('macaddress');
var ua = require('universal-analytics');
var log = require(path.join(global.pathToApp, 'core/logger')).log;
var utils = require(path.join(global.pathToApp, 'core/lib/utils'));
var crypto = require('crypto');

var config = {
enabled: true,
trackingId: 'UA-66924051-2'
};

utils.extendOptions(
config,
global.opts.core.tracking
);

if (process.env.CI || (global.commander && global.commander.test)) {
global.opts.core.common.trackAnonymusStatistics = false;
console.log('Running in CI.');
config.enabled = false;
}

var generateMachineID = function(){
Expand All @@ -29,32 +39,55 @@ var generateMachineID = function(){
};

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

var _trackPage = function(opts){
if (!global.opts.core.common.trackAnonymusStatistics || !opts.pageName) return;
if (!config.enabled || !opts.pageName) return;

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

if (opts.sessionID) {
uid = opts.sessionID;
visitor = ua(config.trackingId, uid, {strictCidFormat: false});
}

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

visitor.pageview(opts.pageName).send();
visitor.pageview({
dp: opts.pageName,
// Document referrer
dr: machineID,
uid: uid
}).send();
};

// Track host-initiated events (by unique machine id)
var _trackEvent = function(opts, force){
force = process.env.CI ? false : force;

if (!force && !global.opts.core.common.trackAnonymusStatistics || !opts.event) return;
if (!force && !config.enabled || !opts.event) return;

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

if (opts.sessionID) {
uid = opts.sessionID;
visitor = ua(config.trackingId, uid, {strictCidFormat: false});
}

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

visitor.event(group, opts.event).send();
visitor.event({
ec: group,
ea: opts.event,
// Document referrer
dr: machineID,
uid: uid
}).send();
};

var getSessionID = module.exports.getSessionID = function(req) {
Expand Down
11 changes: 8 additions & 3 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ module.exports = {
infoFile: 'info.json',

// Name of options field in info.json, used to override configuration per spec
infoFileOptions: 'sourcejs',

trackAnonymusStatistics: true
infoFileOptions: 'sourcejs'
},

// Server options are passed to app.listen (https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback)
Expand Down Expand Up @@ -59,9 +57,16 @@ module.exports = {
// Default thumbnail file path (relative to each spec)
thumbnail: 'thumbnail.png'
},

watch: {
enabled: true,
foreverWatchEnabled: true
},

tracking: {
// Anonymous user statistics tracking.
// Used to get insights about the community and improve engine usage experience.
enabled: true
}
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"universal-analytics": "^0.3.9"
},
"scripts": {
"postinstall": "node ./core/postInstall && grunt update && node ./core/warn",
"postinstall": "node ./core/postInstall && grunt update && node ./core/trackInstall",
"build": "npm i",
"start": "node app",
"test": "grunt ci-pre-run && node app.js --test",
Expand Down

0 comments on commit 0746042

Please sign in to comment.