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
urchinTracker('/outgoing/example_com');
respectively.

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!

Trackback URL for this post:

http://dieter.plaetinck.be/trackback/18
Submitted by Dieter_be on Sun, 06/24/2007 - 19:27. categories [ ]

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Security question, designed to stop automated spam bots