jQuery Google Analytics Tracking API Integration (ga.js)

March 26th, 2008

Jason Huck’s gaTracker jQuery plugin allows for unobtrusive integration of the Google Analytics tracker into a page as well as attaching tracking events to each external link, mailto and download. It works seamlessly but it does however currently use the old ‘urchin.js’ which has been superceeded by Google’s new Tracking API (ga.js)

ga.js has all the capabilities of urchin.js but is structured in a fully Object oriented way. The new API has some nice new functionality which among other things allows for tracking of e-commerce transactions, which is pretty exciting. If you like that sorta thing.

I’ve quickly modified gaTracker to utilise the new API and call it’s _trackPageview() method rather than using urchinTracker(). The code is available below:

jquery.gatracker.js (Tracking API version)

It works in exactly the same way as the gaTracker 1.0.1 release, and can be dropped in it’s place, but utilises the new tracking API. Feel free to grab a copy until support gets built into the official release.

Posted in Web | Tagged with: ,

6 Responses

  1. brad

    Thanks for this. I’m about to use this for a client site, I’ll let you know how it works out.

    Have you submitted this to the plug-in repository at jquery.com?

  2. Oscar

    Hi!

    Tried this, but GA website don’t see the script? Says tracking isn’t installed..?

  3. Oscar

    After a night of sleep, GA seems to have caught up. Thanks for the plugin!

  4. Jamie Thompson

    yeah, it kicks over at midnight. Glad you found the above script useful

  5. Kris

    Has this been updated to use the newer ga.js script?

  6. Christiaan Stegeman

    Hi Jamie,

    After implementing your script on my website I noticed that if you want to log a download url that has a question mark in it, that it doesn’t work.
    Example URL:
    http://www.slimweb.org/mp3/Elektra2006-12Gauge.m3u?play=1

    I added m3u as file extension and noticed that clicks were not logged. The script seems to think that the extension of the url is: m3u?play=1

    The quickfix I made can be added after the following line:
    var ext = u.split(‘.’)[u.split('.').length - 1];

    The fix:
    if(ext.indexOf(‘?’) > -1) { ext = ext.split(‘?’)[0]; }

    I send you the comment and not the original author because you seem to have the most recent version.

    Regards,

    Chris

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.