-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First version for tracking pageviews and events
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ lib-cov | |
pids | ||
logs | ||
results | ||
node_modules | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
module.exports = require('./lib/index.js'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |