Google Analytics is now an essential part of every site we produce. Unless you’ve been living under a rock, you should know that Google purchased Urchin, an industry-leading web analytics package, a few years back, and made a web-based version of it available for free. (Thanks, Google!)In contrast to traditional web log analyzers, Google Analytics collects data via a JavaScript (urchin.js) you add to each page you wish to track. On each page load, a function in the JavaScript (urchinTracker) requests a GIF file from Google and appends a query string full of data onto the request.
When you sign up for a Google Analytics account, you’re provided with a javascript code snippet which includes a unique account number, and you’re instructed to include this snippet on every page in your site that you wish to track. At a glance, this appears to be “all there is to it”, but of course you can do much more.
The Analytics site provides a few recommendations for tracking certain user activity that a single call to urchinTracker doesn’t catch on its own. It’s far from a comprehensive guide, but at this point the capabilities of the script have been pretty thoroughly reverse engineered by end users.
For instance, your web site may include resources that are not standard web pages. Things like PDF files, MP3’s, ZIP archives, etc. Since there is no way to embed the tracking script directly within these resources, we must instead track when users click on links to those resources from within other pages. Similarly, you may also wish to track links to external sites and/or mailto links. If you own an e-commerce site, or want to take advantage of the “campaigns” feature of Google Analytics, the customization can get even deeper.
Identifying and marking up these types of links is simple in concept, but difficult in practice in a modern site where multiple users may be contributing content through a CMS with no knowledge of HTML or JavaScript. Clearly, some form of automation is required. I decided to write a jQuery plugin to make embedding Google Analytics simpler for typical web sites. Currently the plugin does the following:
- Determine whether to include the SSL or non-SSL version of the GA script.
- Include the GA script from within try/catch to help suppress any issues GA may have from time to time.
- Set the _uacct variable to your GA tracking code.
- Call the urchinTracker() function once for the initial page load.
- Examine all of the links on the page and attach onclick events to:
- External links.
- Mailto links.
- Downloads.
- Call urchinTracker() when these links are clicked, prefixing them appropriately.
In addition to the tracking code, the prefixes used for each of the link types above, as well as the extensions considered “downloadable files” are configurable by the user.
It requires jQuery 1.2 or higher for the cross-domain $.getScript() call. Usage is simply:
$.gaTracker('UA-XXXXX-XX');
Or you can specify options like so:
$.gaTracker(
'UA-XXXXX-XX',
{
external: '/external/',
mailto: '/mailto/',
download: '/downloads/',
extensions: [
'pdf','doc','xls','csv','jpg','gif', 'mp3',
'swf','txt','ppt','zip','gz','dmg','xml'
]
}
);
Download the gaTracker plugin at jQuery.com.