Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit b1ccc8d3cdd7dfd62fed7c68537635464ec573f9 @jgallen23 committed Sep 8, 2011
Showing with 161 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +2 −0 Makefile
  3. +1 −0 README.md
  4. +25 −0 build.json
  5. +1 −0 index.js
  6. +57 −0 lib/ga.js
  7. +19 −0 package.json
  8. +39 −0 tests/ga.test.js
  9. +7 −0 tests/runner.js
  10. +8 −0 tests/t.js
2 .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
2 Makefile
@@ -0,0 +1,2 @@
+test:
+ @node tests/runner.js
1 README.md
@@ -0,0 +1 @@
+Node-GA
25 build.json
@@ -0,0 +1,25 @@
+{
+ "JAVASCRIPT": {
+ "DIST_DIR": "./",
+ "resistance": [
+ "./lib/copyright.js",
+ "./lib/ga.js"
+ ]
+ },
+ "JSHINT_OPTS": {
+ "boss": true,
+ "forin": false,
+ "curly": true,
+ "debug": false,
+ "devel": false,
+ "evil": false,
+ "regexp": false,
+ "undef": false,
+ "sub": true,
+ "white": true,
+ "indent": 2,
+ "whitespace": true,
+ "asi": true
+ }
+}
+
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/ga');
57 lib/ga.js
@@ -0,0 +1,57 @@
+var request = require('request');
+
+var GoogleAnalytics = function(ua, host) {
+ this.ua = ua;
+ this.host = host;
+ this.debug = false;
+};
+
+GoogleAnalytics.prototype._serialize = function(obj) {
+ var str = [];
+ for(var p in obj)
+ str.push(p + "=" + encodeURIComponent(obj[p]));
+ return str.join("&");
+};
+
+GoogleAnalytics.prototype._makeRequest = function(obj, cb) {
+ var self = this;
+ var userId = null;
+ if (obj.userId) {
+ userId = obj.userId;
+ delete obj.userId;
+ } else {
+ userId = Math.round(Math.random()*1000000);
+ }
+ var today = new Date().getTime().toString();
+ obj.utmac = this.ua;
+ obj.utmn = today;
+ obj.utmv = "1";
+ obj.utmr= "-";
+ obj.utmcc = "__utma="+userId+"."+Math.round(Math.random()*1000000)+"."+today+"."+today+"."+today+".3; __utmz="+userId+"."+today+".1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);";
+ var url = "http://www.google-analytics.com/__utm.gif?"+this._serialize(obj);
+
+ request({
+ url: url
+ }, function(err, res, body) {
+ if (self.debug) {
+ console.log(url);
+ }
+ if (cb) cb();
+ });
+ return userId;
+};
+
+GoogleAnalytics.prototype.trackPage = function(path, userId, cb) {
+ if (arguments.length == 2 && (typeof userId === 'function')) {
+ cb = userId;
+ userId = null;
+ }
+ var obj = {
+ userId: userId,
+ utmhn: this.host,
+ utmp: path
+ };
+ return this._makeRequest(obj, cb);
+};
+
+module.exports = GoogleAnalytics;
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "ga",
+ "description": "server side google analytics",
+ "version": "0.0.1",
+ "homepage": "https://github.com/jgallen23/node-ga",
+ "authors": ["Greg Allen, @jgaui"],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/jgallen23/node-ga.git"
+ },
+ "dependencies": {
+ "request": "*"
+ },
+ "devDependencies": {
+ "qunit": "*"
+ },
+ "main": "./index.js",
+ "keywords": ["google analytics"]
+}
39 tests/ga.test.js
@@ -0,0 +1,39 @@
+var GoogleAnalytics = require('../lib/ga');
+
+var testUA = "UA-25564582-1";
+var testHost = 'nodega.jga.me';
+
+module = QUnit.module;
+module('ga', {
+ setup: function() {
+ this.ga = new GoogleAnalytics(testUA, testHost);
+ this.ga.debug = true;
+ },
+ teardown: function() {
+ }
+});
+
+test('test', function() {
+ ok(true);
+});
+
+asyncTest('pageview', function() {
+ expect(1);
+ this.ga.trackPage('testing/1', function() {
+ ok(true);
+ });
+});
+
+test('userId', function() {
+ var userId = this.ga.trackPage('user/1');
+ ok(userId);
+});
+
+asyncTest('test user', function() {
+ expect(1);
+ var userId = this.ga.trackPage('user/1');
+ this.ga.trackPage('user/2', userId, function() {
+ ok(true);
+ });
+});
+
7 tests/runner.js
@@ -0,0 +1,7 @@
+var testrunner = require('qunit');
+testrunner.options.coverage = false;
+
+testrunner.run({
+ code: './lib/ga.js',
+ tests: './tests/ga.test.js'
+});
8 tests/t.js
@@ -0,0 +1,8 @@
+var GoogleAnalytics = require('../lib/ga');
+var testUA = "UA-25564582-1";
+var testHost = 'nodega.jga.me';
+var ga = new GoogleAnalytics(testUA, testHost);
+ga.debug = true;
+ga.trackPage('test/1', 1, function() {
+ console.log('done');
+});

0 comments on commit b1ccc8d

Please sign in to comment.