Skip to content

Commit

Permalink
First version for tracking pageviews and events
Browse files Browse the repository at this point in the history
  • Loading branch information
jtillmann committed Feb 7, 2013
1 parent 67a3a48 commit 2eb758b
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ lib-cov
pids
logs
results
node_modules

npm-debug.log
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = require('./lib/index.js');
7 changes: 7 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module.exports = {
endpoint: {
hostname: "http://www.google-analytics.com",
path: "/collect"
}
};
123 changes: 123 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@

var _ = require("underscore");
var qs = require("querystring");
var request = require("request");
var uuid = require("node-uuid");

var config = require("./config");


module.exports = init;


var Visitor = function (tid, cid, options) {
if (typeof tid !== 'string') {
options = tid;
tid = cid = null;
}

if (typeof cid !== 'string') {
options = cid;
cid = null;
}
this.options = options || {};

this.tid = tid || this.options.tid;
this.cid = cid || this.options.cid || uuid.v4();
}

Visitor.prototype = {

debug: function (debug) {
this.debug = arguments.length === 0 ? true : debug;
this._log("Logging enabled")
return this;
},


_log: function (message) {
this.debug && console.log("UA " + message);
},


pageview: function (path, params, fn) {
if (typeof params === 'function') {
fn = params;
params = {};
}
params = params || {}
fn = fn || function () {};

_.extend(params, {
dp: path
});

params.dt = params.dt || this.options.dt;

this.track("pageview", params, fn);

return this;
},


event: function (category, action, label, value, fn) {
if (typeof label === 'function') {
fn = label;
label = value = null;
}
if (typeof value === 'function') {
fn = value;
value = 0;
}

this.track("event", {
ec: category,
ea: action,
el: label,
ev: value
}, fn);

return this;
}


track: function (type, params, fn) {
fn = fn || function () {};
params = params || {};

_.extend(params, {
v: "1",
tid: this.tid,
cid: this.cid,
t: type
});
this._log(type + " (" + JSON.stringify(params) + ")");

var path = config.endpoint.hostname + config.endpoint.path + "?" + qs.stringify(params);

request.post(path, fn);
}


}


function init (tid, cid, options) {
return new Visitor(tid, cid, options);
}
















25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "node-ua",
"version": "0.0.1",
"description": "A node module for Google's Universal Analytics tracking",
"main": "index.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "[email protected]:jtillmann/node-ua.git"
},
"keywords": [
"google",
"analytics",
"universal",
"tracking"
],
"dependencies": {
"request": "2.x",
"underscore": "1.x"
},
"author": "Jörg Tillmann <[email protected]>",
"license": "BSD"
}

0 comments on commit 2eb758b

Please sign in to comment.