Seva's notes

Wisdom, Awareness, Responsibility :)

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: