Skip to content

Instantly share code, notes, and snippets.

@rafikhan
Last active August 10, 2016 18:31
Show Gist options
  • Save rafikhan/e1d321a2190c5b9c3c0448257fd5c1a9 to your computer and use it in GitHub Desktop.
Save rafikhan/e1d321a2190c5b9c3c0448257fd5c1a9 to your computer and use it in GitHub Desktop.
Google Analytics Component for CEP that plays nicely with the AESP licensing framework

Notes

This snippet contains the code necessary to make google analytics calls from your extension.
It plays nicely with the AESP licensing framework so if the user decides to disable analytics this component will honor that automatically. You don't need to do anything special to make that behavior work. Just call the analytics functions assuming the user granted permission and if they didn't the calls will do a no-op internally.

Warning

This code came straight out of the code for After Ease, is designed for its taxonomy/ontology and wasn't meant for distribution. As such you'll want to massage it before using it. For example you'll want to change line 70 to have the domain of your website in there.

Usage

var analytics = new Analytics('YOUR-TRACKING-ID');

// Call when your app opens a new screen (FYI the licensing 
// framework already does this when the extension opens)
analytics.visit('app');

// Called to track a custom event
analytics.track(...);

// If you want to log if an error was raised
analytics.exception(e);

// Gets/sets if analytics is enabled.  You shouldn't ever
// need this but just in case.
analytics.enabled();

function setKey(key, val)
{
localStorage[key] = val;
return val;
}
function getKey(key, def)
{
if (localStorage[key] === undefined)
return setKey(key, def);
return localStorage[key];
}
function getKeyAsBool(key, def)
{
return getKey(key, def) == "true";
}
function guid()
{
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
function GA(tid)
{
this.v = 1;
this.tid = tid;
this.cid = this.getUserId();
}
GA.prototype =
{
getUserId : function() {
return getKey('ga.userid')
},
send : function(t, data)
{
var payload = ['v=' + this.v, 'tid=' + this.tid, 'cid=' + this.cid, 't=' + t];
for (var m in data)
{
if (data[m] != null && data[m] != undefined)
payload.push(m + '=' + encodeURIComponent(data[m]));
}
payload.push('z=' + 1 * new Date());
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() { };
xhr.onerror = function() { }
xhr.open("POST", 'http://www.google-analytics.com/collect', true);
xhr.send(payload.join('&'));
},
exception : function (msg, fatal)
{
this.send('exception', { exd : msg, exf : fatal ? '1' : '0' });
},
pageview : function(path, title)
{
this.send('pageview', { dh : 'your.website.domain', dp : path, dt : title });
},
event : function(ec, ea, el, ev)
{
this.send('event', { ec : ec, ea : ea, el : el, ev : ev });
}
}
function Analytics(tid)
{
if (!getKey('ga.userid'))
setKey('ga.userid', guid());
this.tid = tid;
this.init();
}
Analytics.prototype = {
getUserId : function() {
return getKey('userid', null);
},
init : function() {
this.ga = new GA(this.tid);
},
expired : function() {
if (!this.enabled())
return;
this.track('app', 'expiration');
},
exception : function(e, context) {
if (!this.enabled())
return;
this.ga.exception(e.message, false);
},
install : function(path) {
if (!this.enabled())
return;
this.ga.pageview(path);
},
visit : function(path) {
if (!this.enabled())
return;
this.ga.pageview(path);
},
pageview : function(path) {
if (!this.enabled())
return;
this.ga.pageview(path);
},
button : function(id, action, val) {
if (!this.enabled())
return;
this.track(id, action, 'button', val);
},
track : function(id, action, category, val) {
if (!this.enabled())
return;
this.ga.event(id, action, category, val);
},
enabled : function(val)
{
if (val === null || val === undefined)
return getKeyAsBool('ga.enabled', true);
else
setKey('ga.enabled', val);
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment