From 2eb758b851a5884cd591d2b4e1effe0c8c0ae7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Tillmann?= Date: Thu, 7 Feb 2013 22:29:42 +0100 Subject: [PATCH] First version for tracking pageviews and events --- .gitignore | 1 + index.js | 2 + lib/config.js | 7 +++ lib/index.js | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 ++++++++++ 5 files changed, 158 insertions(+) create mode 100644 index.js create mode 100644 lib/config.js create mode 100644 lib/index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore index f356293..5466f24 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ lib-cov pids logs results +node_modules npm-debug.log diff --git a/index.js b/index.js new file mode 100644 index 0000000..d25512e --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ + +module.exports = require('./lib/index.js'); diff --git a/lib/config.js b/lib/config.js new file mode 100644 index 0000000..42e23e1 --- /dev/null +++ b/lib/config.js @@ -0,0 +1,7 @@ + +module.exports = { + endpoint: { + hostname: "http://www.google-analytics.com", + path: "/collect" + } +}; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..8d1ef89 --- /dev/null +++ b/lib/index.js @@ -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); +} + + + + + + + + + + + + + + + + diff --git a/package.json b/package.json new file mode 100644 index 0000000..10b391c --- /dev/null +++ b/package.json @@ -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": "git@github.com:jtillmann/node-ua.git" + }, + "keywords": [ + "google", + "analytics", + "universal", + "tracking" + ], + "dependencies": { + "request": "2.x", + "underscore": "1.x" + }, + "author": "Jörg Tillmann ", + "license": "BSD" +}