Getting statistics about events that don't trigger page requests with Google Analytics

You probably already heard of Google Analytics. It's a pretty nice program that (basically) gathers data about visits of your site and creates reports of it. It works by including some JavaScript code on your page, so that each page request triggers a call to the Analytics tracker sending along some data such as which page is requested and which resolution was used. (no personal or other privacy-sensitive data is sent). But here is the deal! I just discovered that you can also track events that don't require page requests!
Think of links to files or to external locations, JavaScript events (Ajax anyone?) or even Flash events (but who is crazy enough to use Flash anyway?).

The principle is very easy: similar to normal page loads which trigger a call of the JavaScript function urchinTracker() you just add onClick="javascript:urchinTracker('/this-is-something-i-want-to-track');" to your hyperlink.
You can even work with subdivisions. The examples on the help pages for download and outbound links propose urchinTracker('/downloads/map'); and

Now we also know why Google says the "normal" tracking code should be put at the end of the page, right before the </body> tag. I always wondered why they recommend this weird location. This is because calls like the ones above should happen prior to the "normal" one.

If only I had known this earlier. I would love to have known an accurate number of downloads of my thesis text (and the number of unique downloaders) :-)

For those who don't have Google Analytics setup yet:
Drupal users should install the Drupal Google Analytics module.
CakePHP users can put the Tracking code in the layout file or even in an element.

Have fun tracking all events on your website!

Add comment