Seva's notes

Wisdom, Awareness, Responsibility :)

Archive for August 2008

Prototype styled Google Analytics javascript snippet

leave a comment »

Recently I’ve created a prototype styled javascript snippet to enable google analytics on one of my sites. Save this code in google.analytics.js file and include it from the head tag of the page just after including of the prototype.js:

gaTrackerId = 'ss-ddddddd-d'; // insert your tracker id here
document.observe('dom:loaded', function() {
    var gaJsHost = (
        'https:' == document.location.protocol
            ? 'https://ssl.'
            : 'http://www.'
    ) + 'google-analytics.com/ga.js';
    var script = new Element('script', { 'src': gaJsHost});
    var gaTrack = function() {
        if (
            !script.readyState
            || /loaded|complete/.test(script.readyState)
        ) {
            var pageTracker = _gat._getTracker(gaTrackerId);
            pageTracker._trackPageview();
        }
    };
    script.observe('load', gaTrack);
    script.observe('readystatechange', gaTrack);
    document.body.appendChild(script);
});

Written by Seva

2008-08-11 (August 11) at 08:00:07