PDFs and Google Analytics: What a combo!
– posted April 27th, 2009 by Peter McKenna Comments (3)
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.
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
- Add our custom script onto your server, eg. /scripts/gataglinks.js
- Add an extra line of code to every page on your site, immediately before the ga.js GA tracking code
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
- .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.
Digg this post Submit to del.icio.us Submit to technorati
Categories Content, Technology, Web analytics




3 comments so far
1. Jonathan on Apr 27th, 2009 - 23:48
Google Analytics has been providing a way to do this for a while now, by adding an “onclick” attribute for your links.
http://www.google.com/support/.....tx=sibling
Does your script do something better than Googles method?
2. Jonathan on Apr 27th, 2009 - 23:51
I think I just answered my own question. With Google’s method you have to encode every link, and your script prevents having to do that.
Sorry I didn’t think it through before I posted.
3. » Tracking downloads in Google Analytics. You’re set up, what’s next? - iQ Blog on Apr 28th, 2009 - 11:41
[...] you have deployed our script as Peter showed you, you’ll be able to track the downloads on your site. Now it’s time to analyse your [...]