From f3173b731aece4f0d7698a10d126ad8c3029909a Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Mon, 13 Jan 2014 17:06:39 +0600 Subject: [PATCH] Pass params into track Closes #6 --- lib/insight.js | 15 +++++++++++++-- lib/providers.js | 11 ++++++++--- lib/push.js | 3 ++- package.json | 2 +- test/test-insight.js | 27 ++++++++++++++++++++++----- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/insight.js b/lib/insight.js index 4aae1b7..19e77f1 100644 --- a/lib/insight.js +++ b/lib/insight.js @@ -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'); @@ -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(); }; diff --git a/lib/providers.js b/lib/providers.js index 32d14eb..68d33b6 100644 --- a/lib/providers.js +++ b/lib/providers.js @@ -1,5 +1,6 @@ 'use strict'; var request = require('request'); + /** * Tracking providers. * @@ -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 @@ -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 }; @@ -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 }; diff --git a/lib/push.js b/lib/push.js index da754f9..cb1f5fe 100644 --- a/lib/push.js +++ b/lib/push.js @@ -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); } diff --git a/package.json b/package.json index bfed984..0c2fea9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test-insight.js b/test/test-insight.js index c90e06e..3237dbc 100644 --- a/test/test-insight.js +++ b/test/test-insight.js @@ -2,17 +2,19 @@ '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(); @@ -20,6 +22,19 @@ describe('Insight()', function() { 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({}); @@ -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({ @@ -52,7 +68,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; assert.equal(qs.tid, code); @@ -60,6 +76,7 @@ describe('providers', function() { assert.equal(qs.an, pkg); assert.equal(qs.av, ver); assert.equal(qs.dp, path); + assert.equal(qs.dl, path + '?' + querystring); }); }); @@ -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];