Last active
September 27, 2019 14:36
-
-
Save jasonrhodes/5320733 to your computer and use it in GitHub Desktop.
Helper library for working with Google Analytics.
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
/** | |
* Create the Scientist analytics object | |
* | |
* Pass nothing to default to the standard global _gaq, or | |
* pass in the GA object if it's named differently. | |
* | |
* If you haven't initialized GA yet, leave it blank and | |
* call scientist.initialize() with your UA string later. | |
*/ | |
var scientist = new Scientist(); | |
/** | |
* To set up Google Analytics | |
*/ | |
scientist.initialize("UA-XXXX"); | |
/** | |
* To track a pageview | |
*/ | |
scientist.trackPageview(); | |
/** | |
* Init and track pageview at once, per usual | |
*/ | |
scientist.initialize("UA-XXXX").trackPageview(); | |
/** | |
* To register and track events | |
*/ | |
// Option 1: Directly | |
$(".thingy").on("click", function (e) { | |
scientist.trackEvent("my category", "my action", "my label", "my value"); | |
}) | |
// Option 2: Register your own event, track it later | |
var myEvent = new scientist.Event("my category"); | |
$(".thingy-1").on("click", function (e) { | |
myEvent.track({ action: "my action", label: "my label", value: 100 }); | |
}); | |
$(".thingy-2").on("change", function (e) { | |
myEvent.track({ action: "my other action", label: "other label", value: 200 }); | |
}); | |
// Option 3: Register events to the GATools.Events object | |
scientist.Events.register("My Event", "my category 1"); | |
scientist.Events.register("Other Event", "other category 2"); | |
$(".thingy-1").on("click", function (e) { | |
scientist.Events.track("My Event", { action: "Clicked button", label: "Title Of My Button" }); | |
}); | |
$(".thingy-2").on("change", function (e) { | |
scientist.Events.track("Other Event", { action: "Submitted form", value: 120 }); | |
}); |
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
/** | |
* Scientist.js | |
* | |
* A small helper library for working with Google Analytics. | |
* | |
* @author Jason Rhodes | |
* @version 0.2 | |
* | |
*/ | |
var Scientist = function (analytics) { | |
var analytics = analytics || window._gaq || []; | |
var lib = this; | |
var log = function (message) { | |
window.console && window.console.log && window.console.log(message); | |
}; | |
this.initialize = function (UA) { | |
analytics.push(["_setAccount", UA]); | |
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; | |
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); | |
return this; | |
}; | |
this.trackPageview = function () { | |
analytics.push(["_trackPageview"]); | |
return this; | |
} | |
this.checkAnalytics = function () { | |
return analytics; | |
}; | |
this.Event = function (category, action) { | |
this.category = category; | |
this.action = action; | |
}; | |
this.Event.prototype.track = function (params) { | |
var category = params.category || this.category; | |
var action = params.action || this.action; | |
//log([category, action, params.label, params.value, params.noninteraction]); | |
analytics.push(["_trackEvent", category, action, params.label, params.value, params.noninteraction]); | |
return this; | |
}; | |
this.trackEvent = function (params) { | |
var event = new this.Event(); | |
if (!params.category && arguments.length > 1) { | |
params = { | |
category: arguments[0], | |
action: arguments[1], | |
label: arguments[2], | |
value: arguments[3], | |
noninteraction: arguments[4] | |
}; | |
} | |
//log(params); | |
event.track(params); | |
return this; | |
}; | |
this.Events = {}; | |
this.Events.Registry = {}; | |
this.Events.register = function (name, category, action) { | |
this.Registry[name] = new lib.Event(category, action); | |
}; | |
this.Events.track = function (name, params) { | |
this.Registry[name].track(params); | |
}; | |
this.prototype.spy = function (options) { | |
// Much awesomeness to come | |
}; | |
}; |
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
/** | |
* Scientist.js | |
* | |
* A small helper library for working with Google Analytics. | |
* | |
* @author Jason Rhodes | |
* @version 0.2 | |
* | |
* MINIFIED VIA UGLIFY.js | |
* | |
*/ | |
var Scientist=function(a){var a=a||window._gaq||[],b=this;this.initialize=function(b){a.push(["_setAccount",b]);var c=document.createElement("script");c.type="text/javascript",c.async=!0,c.src=("https:"==document.location.protocol?"https://ssl":"http://www")+".google-analytics.com/ga.js";var d=document.getElementsByTagName("script")[0];return d.parentNode.insertBefore(c,d),this},this.trackPageview=function(){return a.push(["_trackPageview"]),this},this.checkAnalytics=function(){return a},this.Event=function(a,b){this.category=a,this.action=b},this.Event.prototype.track=function(b){var c=b.category||this.category,d=b.action||this.action;return a.push(["_trackEvent",c,d,b.label,b.value,b.noninteraction]),this},this.trackEvent=function(a){var b=new this.Event;return!a.category&&arguments.length>1&&(a={category:arguments[0],action:arguments[1],label:arguments[2],value:arguments[3],noninteraction:arguments[4]}),b.track(a),this},this.Events={},this.Events.Registry={},this.Events.register=function(a,c,d){this.Registry[a]=new b.Event(c,d)},this.Events.track=function(a,b){this.Registry[a].track(b)},this.prototype.spy=function(){}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment