Skip to content

Commit

Permalink
Pass params into track
Browse files Browse the repository at this point in the history
Closes yeoman#6
  • Loading branch information
floatdrop committed Jan 13, 2014
1 parent 14aba4f commit f3173b7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
15 changes: 13 additions & 2 deletions lib/insight.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var path = require('path');
var qs = require('querystring');
var fork = require('child_process').fork;
var Configstore = require('configstore');
var chalk = require('chalk');
Expand Down Expand Up @@ -73,12 +74,22 @@ Insight.prototype.track = function () {
return;
}

var path = '/' + [].map.call(arguments, function (el) {
var args = Array.prototype.slice.call(arguments, 0);

var querystring = {};

if (args.length > 0 && typeof args[args.length - 1] === 'object') {
querystring = args.pop();
}

querystring.version = querystring.version || this.packageVersion;

var path = '/' + [].map.call(args, function (el) {
return String(el).trim().replace(/ /, '-');
}).join('/');

// timestamp isn't unique enough since it can end up with duplicate entries
this._queue[Date.now() + ' ' + path] = path;
this._queue[Date.now() + ' ' + path + ' ' + qs.stringify(querystring)] = path;
this._save();
};

Expand Down
11 changes: 8 additions & 3 deletions lib/providers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var request = require('request');

/**
* Tracking providers.
*
Expand All @@ -9,7 +10,7 @@ var request = require('request');
*/
module.exports = {
// Google Analytics — https://www.google.com/analytics/
google: function(id, path) {
google: function(id, path, querystring) {
var now = Date.now();
var qs = {
v: 1, // GA API version
Expand All @@ -20,6 +21,7 @@ module.exports = {
an: this.packageName,
av: this.packageVersion,
dp: path,
dl: path + '?' + querystring,
qt: now - parseInt(id, 10), // queue time - delta (ms) between now and track time
z: now // cache busting
};
Expand All @@ -30,16 +32,19 @@ module.exports = {
};
},
// Yandex.Metrica — http://metrica.yandex.com
yandex: function(id, path) {
yandex: function(id, path, querystring) {
var ts = new Date(parseInt(id, 10))
.toISOString()
.replace(/[-:T]/g, '')
.replace(/\..*$/, '');

var url = 'http://' + this.packageName + '.insight';
url += path + '?' + querystring;

var qs = {
wmode: 3,
ut: 'noindex',
'page-url': 'http://' + this.packageName + '.insight' + path + '?version=' + this.packageVersion,
'page-url': url,
'browser-info': 'i:' + ts + ':z:0:t:' + path,
rn: Date.now() // cache busting
};
Expand Down
3 changes: 2 additions & 1 deletion lib/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ process.on('message', function(msg) {
var parts = el.split(' ');
var id = parts[0];
var path = parts[1];
var qs = parts[2];

request(insight.getRequest(id, path), function(error) {
request(insight.getRequest(id, path, qs), function(error) {
if (error) {
return cb(error);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "git://github.com/yeoman/insight.git"
},
"scripts": {
"test": "node node_modules/mocha/bin/mocha test/test-*.js"
"test": "node node_modules/mocha/bin/mocha test/test-*.js --reporter spec"
},
"dependencies": {
"chalk": "~0.2.0",
Expand Down
27 changes: 22 additions & 5 deletions test/test-insight.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@
'use strict';
var assert = require('assert');
var sinon = require('sinon');
var qs = require('querystring');
var Insight = require('../lib/insight');
var _ = require('lodash');

describe('Insight()', function() {
var insight = new Insight({
var insightOpts = {
trackingCode: 'xxx',
packageName: 'yeoman',
packageVersion: '0.0.0'
});
};

it('should put tracked path in queue', function(cb) {
var insight = new Insight(insightOpts);
Insight.prototype._save = function() {
assert.equal('/test', _.values(this._queue)[0]);
cb();
};
insight.track('test');
});

it('should put tracked path and params in queue', function(cb) {
var insight = new Insight(insightOpts);
Insight.prototype._save = function() {
assert.equal('/test/test2', _.values(this._queue)[0]);
var key = _.keys(this._queue)[0];
var parts = key.split(' ');
assert.equal('/test/test2', parts[1]);
assert.equal('param=one&version=0.0.0', parts[2]);
cb();
};
insight.track('test', 'test2', { param: 'one' });
});

it('should throw exception when trackingCode or packageName is not provided', function(cb) {
assert.throws(function() {
var insight = new Insight({});
Expand All @@ -42,7 +57,8 @@ describe('providers', function() {
ver = '0.0.0',
code = 'GA-1234567-1',
ts = Date.UTC(2013, 7, 24, 22, 33, 44),
path = '/test/path';
path = '/test/path',
querystring = 'version=0.0.0';

describe('Google Analytics', function() {
var insight = new Insight({
Expand All @@ -52,14 +68,15 @@ describe('providers', function() {
});

it('should form valid request', function() {
var req = insight.getRequest(ts, path),
var req = insight.getRequest(ts, path, querystring),
qs = req.qs;

assert.equal(qs.tid, code);
assert.equal(qs.cid, insight.clientId);
assert.equal(qs.an, pkg);
assert.equal(qs.av, ver);
assert.equal(qs.dp, path);
assert.equal(qs.dl, path + '?' + querystring);
});
});

Expand All @@ -72,7 +89,7 @@ describe('providers', function() {
});

it('should form valid request', function() {
var req = insight.getRequest(ts, path),
var req = insight.getRequest(ts, path, querystring),
qs = req.qs,
cookie = req.jar.cookies[0];

Expand Down

0 comments on commit f3173b7

Please sign in to comment.