|
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); |
|
}, |
|
} |