PDFs and Google Analytics: What a combo!

Most of the time, document downloads, mailtos and external links aren’t track-able in Google Analytics (GA).  Slap a PDF on your site, you won’t know how many people clicked on it, much less read it.  Back in 2006 we wrote a script that fixed just that.  And now, we’ve made it even better.

Don't worry.  It's not that complicated

Don't worry. It's not that complicated

Our new version resolves a couple of issues with the previous one, and includes some updates to how links are tracked and stored.

Our Script 2.0

It’s a Browser thing. The problem with the original script was that it didn’t always work with IE. Now we’ve updated and tested it in all the major browsers including Internet Explorer 6+, Firefox 3 and Safari 3. There is still an issue with the Opera 9 – the script is not registering clicks on relevant links – but it will fail silently, rather than show an error message.

Links can’t get away. Our new and improved script can now track mailto: links.  We’ve updated the path used to store the links as well.

  • External links are tracked as /virtual/exlink/page/destination-url/
  • Downloads are tracked as /virtual/download/page/file-name/
  • mailto: links are tracked as /virtual/mailto/page/email-address/

How to install our script

To get the script up and runninig, all you need to do is

  1. Add our custom script onto your server, eg. /scripts/gataglinks.js
  2. Add an extra line of code to every page on your site, immediately before the ga.js GA tracking code

ga-script-path2

This is the extra line of code which you should add to the footer of each page of your site. This script will automatically tag links to non-page elements (PDFs, mailtos, etc) and it will allow GA to track them.

Once you’ve installed our script, you can track downloads of the following file types:

  • .doc
  • .pdf
  • .xls
  • .ppt
  • .zip
  • .txt
  • .vsd
  • .vxd
  • .js
  • .css
  • .rar
  • .ese
  • .wma
  • .mov
  • .avi
  • .wmv
  • .mp3

If you want to add another file type, you can! Just edit the script to add your file type.

Change this:
(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)

To this:
(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|myfiletype)

Wait, there’s more

We also updated the script to use the new Google tracking code, so make sure to update the code on the page you want to track before adding this script, ga.js.

(Stay tuned: Niamh will let you know in the next post about how to analyse this data using GA).

Download our script

You can download the full script here, but I would recommend using this minified version of the script for your production site to reduce load times.

Categories Content, Technology, Web analytics