Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merging upstream

  • Loading branch information...
commit fbef89023514689828fee9ad78480cec601655f8 2 parents 0418756 + 5648ca7
@hannesgassert hannesgassert authored
View
7 README.md
@@ -12,4 +12,9 @@ Server side google analytics
var host = 'nodega.jga.me';
var ga = new GoogleAnalytics(ua, host);
ga.trackPage('testing/1');
-
+ ga.trackEvent({
+ category: 'Videos',
+ action: 'Video Loading',
+ label: 'Gone With the Wind',
+ value: 42
+ });
View
47 lib/ga.js
@@ -1,4 +1,5 @@
-var request = require('request');
+var request = require('request'),
+ util = require('util')
var GoogleAnalytics = function(ua, host) {
this.ua = ua;
@@ -35,9 +36,14 @@ GoogleAnalytics.prototype._makeRequest = function(obj, cb) {
url: url
}, function(err, res, body) {
if (self.debug) {
- //console.log(url);
+ util.debug(url);
+ if (err) {
+ util.debug(err);
+ }
+ }
+ if (cb) {
+ cb(err);
}
- if (cb) cb();
});
return userId;
};
@@ -55,4 +61,39 @@ GoogleAnalytics.prototype.trackPage = function(path, userId, cb) {
return this._makeRequest(obj, cb);
};
+// see https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
+// event is an object containing {category, action, [label], [value], [nonInteraction]}
+GoogleAnalytics.prototype.trackEvent = function(event, userId, cb) {
+ var payload = [];
+
+ if (arguments.length === 2 && (typeof userId === 'function')) {
+ cb = userId;
+ userId = null;
+ }
+
+ ['category', 'action', 'label'].forEach(function(key) {
+ var val = event[key];
+ if (val != null) {
+ payload.push(val);
+ }
+ });
+
+ var obj = {
+ userId: userId,
+ utmhn: this.host,
+ utmt: 'event',
+ utme: '5(' + payload.join('*') + ')'
+ };
+
+ if (event.value != null) {
+ obj.utme += '(' + Math.round(event.value) + ')';
+ }
+
+ if (event.nonInteraction) {
+ obj.utmni = 1;
+ }
+
+ return this._makeRequest(obj, cb);
+};
+
module.exports = GoogleAnalytics;
View
2  package.json
@@ -1,7 +1,7 @@
{
"name": "ga",
"description": "server side google analytics",
- "version": "0.0.1",
+ "version": "0.0.2",
"homepage": "https://github.com/jgallen23/node-ga",
"authors": ["Greg Allen, @jgaui"],
"repository": {
View
24 tests/ga.test.js
@@ -23,8 +23,8 @@ test('userId', function() {
asyncTest('pageview', function() {
expect(1);
- this.ga.trackPage('testing/1', function() {
- ok(true);
+ this.ga.trackPage('testing/1', function(err) {
+ ok(!err);
start();
});
});
@@ -32,9 +32,25 @@ asyncTest('pageview', function() {
asyncTest('test user', function() {
expect(1);
var userId = this.ga.trackPage('user/1');
- this.ga.trackPage('user/2', userId, function() {
- ok(true);
+ this.ga.trackPage('user/2', userId, function(err) {
+ ok(!err);
start();
});
});
+asyncTest('event', function() {
+ expect(1);
+ this.ga.trackEvent(
+ {
+ category: 'Videos',
+ action: 'Video Loading',
+ label: 'Gone With the Wind',
+ value: 3
+ },
+ function(err) {
+ ok(!err);
+ start();
+ }
+ );
+});
+
View
23 tests/t.js
@@ -1,8 +1,25 @@
var GoogleAnalytics = require('../lib/ga');
-var testUA = "UA-25564582-1";
+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');
+
+ga.trackPage('test/1', 1, function(err) {
+ console.log(err || 'Page tracked');
});
+
+ga.trackEvent(
+ {
+ category: 'Videos',
+ action: 'Video Loading',
+ label: 'Gone With the Wind',
+ value: 3,
+ nonInteraction: true
+ },
+ 2,
+ function(err) {
+ console.log(err || 'Event tracked');
+ }
+);
Please sign in to comment.
Something went wrong with that request. Please try again.