resin-universal-ga
Advanced tools
Comparing version
{ | ||
"name": "resin-universal-ga", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A CommonJS wrapper for both Node.js and browser (async) version of Google Analytics library", | ||
@@ -5,0 +5,0 @@ "main": "src/node.js", |
@@ -7,22 +7,30 @@ require('./ga-loader') | ||
module.exports = function (propertyId, site, debug) { | ||
loggedIn = false | ||
var booted = false | ||
return { | ||
login: function (userId) { | ||
var options = { | ||
userId: userId | ||
} | ||
boot: function() { | ||
if (booted) return | ||
var options = {} | ||
if (debug) { | ||
options.cookieDomain = 'none' | ||
} | ||
window.ga('create', propertyId, site, TRACKER_NAME, options) | ||
loggedIn = true | ||
booted = true | ||
}, | ||
anonLogin: function () { | ||
this.boot() | ||
}, | ||
login: function (userId) { | ||
this.boot() | ||
ga(TRACKER_NAME + '.set', 'userId', userId) | ||
}, | ||
logout: function () { | ||
if (!loggedIn) return Promise.reject(new Error("GA logout called before login")) | ||
return Promise.fromCallback(function (callback) { | ||
window.ga(function() { | ||
window.ga.remove(TRACKER_NAME) | ||
loggedIn = false | ||
if (booted) { | ||
window.ga.remove(TRACKER_NAME) | ||
booted = false | ||
} | ||
callback() | ||
@@ -33,4 +41,3 @@ }) | ||
track: function (category, action, label) { | ||
if (!loggedIn) return Promise.reject(new Error("Can't record GA events without a login first")) | ||
this.boot() | ||
return Promise.fromCallback(function (callback) { | ||
@@ -37,0 +44,0 @@ var options = { |
@@ -5,28 +5,31 @@ var Promise = require('bluebird') | ||
module.exports = function(propertyId, site, debug) { | ||
var visitor = null | ||
function createVisitor(userId) { | ||
if (visitor) return visitor | ||
return visitor = ua(propertyId, userId, { | ||
strictCidFormat: false, | ||
https: true | ||
}) | ||
if (debug) { | ||
visitor = visitor.debug() | ||
} | ||
} | ||
function destroyVisitor() { | ||
visitor = null | ||
} | ||
var ga = null | ||
return { | ||
boot: function() { | ||
if (ga) return | ||
ga = ua(propertyId, { | ||
strictCidFormat: false, | ||
https: true | ||
}) | ||
if (debug) { | ||
ga = ga.debug() | ||
} | ||
}, | ||
anonLogin: function(userId) { | ||
this.boot() | ||
}, | ||
login: function(userId) { | ||
createVisitor(userId) | ||
this.boot() | ||
ga.set('uid', userId) | ||
}, | ||
logout: function() { | ||
destroyVisitor() | ||
ga = null | ||
}, | ||
track: function(category, action, label) { | ||
// if called before `login` create the object with the random ID | ||
createVisitor() | ||
this.boot() | ||
return Promise.fromCallback(function (callback) { | ||
visitor.event(category, action, label, undefined, callback) | ||
ga.event(category, action, label, undefined, callback) | ||
}) | ||
@@ -36,2 +39,1 @@ } | ||
} | ||
97
8.99%3729
-1.48%