Analyte - Easy to use include for Google Analytics
2
Fordiman
Forgive the formality here; this is one of my professional scripts.
Analyte 1.0 (c) 2008 Bryan Elliott
Purpose:
Simplify inclusion of Google Analytics into any site, add GA tracking to non-HTML links, and reduce 'blocking' action of Google Analytics scripts.
Use:
License:
Unlimited license granted to Abacus Studios, Inc. and I-Site, Inc.
All other parties licensed under CC-BY-SA-3.0:
Creative Commons Attribution / Share Alike 3.0 US
Fulltext and synopsis at
http://creativecommons.org/licenses/by-sa/3.0/us/
This license need not appear in minified or otherwise compressed forms
of this script, so long as the script is in active use by a web site or
application, and no other credit is claimed.
Analyte 1.0 (c) 2008 Bryan Elliott
Purpose:
Simplify inclusion of Google Analytics into any site, add GA tracking to non-HTML links, and reduce 'blocking' action of Google Analytics scripts.
Use:
License:
Unlimited license granted to Abacus Studios, Inc. and I-Site, Inc.
All other parties licensed under CC-BY-SA-3.0:
Creative Commons Attribution / Share Alike 3.0 US
Fulltext and synopsis at
http://creativecommons.org/licenses/by-sa/3.0/us/
This license need not appear in minified or otherwise compressed forms
of this script, so long as the script is in active use by a web site or
application, and no other credit is claimed.
var Analyte = {
Browser: {
IE: !!(window.attachEvent && !window.opera),
Opera: !!window.opera,
WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
MobileSafari: !!navigator.userAgent.match(/iPhone.*Mobile.*Safari/)
},
ssl:'https:'== document.location.protocol,
watchEvent: function (o,e,f) {
var IE=Analyte.Browser.IE, __method = IE?
function () { return f.apply(o, arguments); }:
f;
if (e=='domloaded') return Analyte.onDOMLoaded(__method);
return o[IE?'attachEvent':'addEventListener']((IE?'on':'')+e,__method,false);
},
_tags: function (a,o) { if (!o) o=document; return o.getElementsByTagName(a); },
headElement: _tags('head')[0],
extend: function (d,s) { for (var i in s) d[i]=s[i]; },
onDOMLoaded: function () {
Analyte.onDOMLoaded.execute.apply(Analyte.onDOMLoaded,arguments);
},
trackDownloads: function () {
var elements = Analyte._tags('a');
for (var i=0; i<elements.length; i++) {
t = elements[i].getAttribute('href');
if (!/\.[^\/]*$/.test(t))
continue;
if (!/\.(html|htm|asp|aspx|pl|rb|php|php3|phtml|shtml|jsp|cfm)$/i.test(t))
Analyte.watchEvent(elements[i],'click',function () {
var link=this.href.replace(/^[^\/]*\/\/[^\/]*\//,'/');
if (!!window.pageTracker)
pageTracker._trackPageview(link);
else
urchinTracker(link);
});
}
},
googleAnalyticsLoader: function () {
var i,t,elements;
if (!window._gat)
return setTimeout(Analyte.googleAnalyticsLoader,250);
var i,t,elements = Analyte._tags('script');
for (var i=0; i<elements.length; i++)
if (/^UA-/i.test(t=elements[i].getAttribute('id'))) break;
if (t==null) return false;
//set up the global pageTracker object
window.pageTracker = _gat._getTracker(t);
pageTracker._initData();
pageTracker._trackPageview();
Analyte.trackDownloads();
}
};
with (Analyte) {
//Core stuff for onDOMLoaded
extend(onDOMLoaded,{
repo: [],
bound: false,
ready: false,
//The pseudo event aggregator
imReady: function () {
if (onDOMLoaded.ready) return;
onDOMLoaded.ready=true;
for (i=0; i<onDOMLoaded.repo.length; i++) {
onDOMLoaded.repo[i].apply();
}
},
//drop the function in, or if the DOM is ready, just run it.
register: function (fnc) {
if (!onDOMLoaded.ready)
onDOMLoaded.repo.push(fnc);
else
fnc.apply();
return;
}
});
switch (true) {
case (document.addEventListener && !Browser.Opera):
onDOMLoaded.execute = function (fnc) {
if (!onDOMLoaded.bound) {
onDOMLoaded.bound=true;
document.addEventListener('DOMContentLoaded', onDOMLoaded.imReady, false);
}
onDOMLoaded.register(fnc);
};
break;
case (Browser.IE):
onDOMLoaded.execute = function (fnc) {
if (!onDOMLoaded.bound) {
onDOMLoaded.bound=true;
onDOMLoaded.checkReady();
}
onDOMLoaded.register(fnc);
};
onDOMLoaded.checkReady = function () {
try {
document.documentElement.doScroll('left');
} catch (error) {
setTimeout(onDOMLoaded.checkReady,50);
return;
}
onDOMLoaded.imReady();
};
break;
case (Browser.Opera):
onDOMLoaded.execute = function (fnc) {
if (!onDOMLoaded.bound) {
onDOMLoaded.bound=true;
document.addEventListener('DOMContentLoaded', function () {
if (onDOMLoaded.ready) return;
for (var i=0; i<document.styleSheets.length; i++)
if (document.styleSheets[i].disabled) {
setTimeout(arguments.callee,50);
return;
}
onDOMLoaded.imReady();
}, false);
}
onDOMLoaded.register(fnc);
};
break;
case (Browser.Safari):
onDOMLoaded.execute = function (fnc) {
if (!onDOMLoaded.bound) {
onDOMLoaded.bound=true;
onDOMLoaded.checkReady();
}
onDOMLoaded.register(fnc);
};
onDOMLoaded.checkReady = function () {
if (document.readyState != 'loaded' && document.readyState != 'complete') {
setTimeout(arguments.callee, 50);
return;
}
if (numStyles === undefined) {
var numStyles = document.getElementsByTagName('style').length;
var links = document.getElementsByTagName('link');
for (var i=0; i<links.length; i++) {
var t=links[i].getAttribute('rel');
if (t!=null && t.toLowercase()=='stylesheet')
numStyles++;
}
}
if (document.styleSheets.length != numStyles) {
setTimeout(arguments.callee, 50);
return;
}
onDOMLoaded.imReady();
};
break;
}
watchEvent(window,'load',onDOMLoaded.imReady);
watchEvent(window,'domloaded',function () {
if (!!window._gat) {
if (!!window.pageTracker) return trackDownloads();
return googleAnalyticsLoader();
}
if (!!window.urchinTracker) return trackDownloads();
var gaJs = ((ssl) ? "https://ssl." : "http://www.")+'google-analytics.com/ga.js';
var gsTag = document.createElement('script');
gsTag.setAttribute('src',gaJs);
gsTag.setAttribute('type','text/javascript');
headElement.appendChild(gsTag);
setTimeout(googleAnalyticsLoader,50);
});
watchEvent(window,'unload',function () {
delete window.Analyte;
});
};






<script src="/path/to/analyte.js" type="text/javascript" id="UA-xxxxxx-x"<>/script>