Tracking document downloads in Google Analytics
– posted November 6th, 2006 by Colm McBarron Comments (17) for Tracking document downloads in Google Analytics
We've been using Google Analytics for some time, both for a number of clients, and for our own site. It's a very real and impressive way to measure what areas of a site people are most interested in. What's even better is that it's free, you can signup anytime.
Tracking standard webpages is a breeze with a couple of lines of javascript placed at the bottom of your page, for most sites there is generally a single file to modify and/or an automated way to do this.
Tracking documents such as PDFs and word is not so easy however, looking at the GA help files, it suggests that you manually tag every PDF or word document with some javascript like this
<a href="/files/map.pdf" onClick="javascript:urchinTracker ('/downloads/map'); ">
This is fine for a small site, but once you get into a site of any size then it becomes a tedious manual process, some content management systems can help, but from our experience these systems are limited in what they can do.
After some thinking and googling around for a solution that avoids the mass find and replace, we came across this clever idea - to attach some event listeners to all document links.
So I've taken the code and modified it a little to make it just for file downloads, and to cover more file extensions that the original. This will track the following file types:
- PDF (.pdf)
- Microsoft Word (.doc)
- Microsoft Excel (.xls)
- Microsoft PowerPoint (.ppt)
- Microsoft Visio (.vsd)
- Microsoft Visio XML (.vxd)
- ZIP Archive(.zip)
- RAR Archive (.rar)
- Text file (.txt)
- Downloadable Javascript file (.js)
- Downloadable CSS file (.css)
- Executable (.exe)
- NEW Windows Media Audio (.wma)
- NEW Quicktime (.mov)
- NEW Audio Video Interleave (.avi)
- NEW Windows Media Video (.wmv)
- NEW Mpeg Layer-3 (.mp3)
The result - some unobtrusive javascript, to use simply download the js file, place it somewhere on your webserver and paste the following code directly above your standard Google Analytics tracking code (which by the way, should be as near the end of the <body> tag as possible).
<script src="/js/taglinks.js" type="text/javascript"></script>
(Don't forget to replace the '/js/taglinks.js' with the correct path to the javascript file)
Hey presto you've now got tracking for all downloadable files!
UPDATE: I've update the Javascript so it won't throw a wobbly if there are anchor links on the page, also some new file types have been added (see above).
UPDATE: The script now tracks external links as well as document downloads.
Digg this post Submit to del.icio.us Submit to technorati
Categories: Web analytics and Web Site Management


17 comments so far
1. Thomas on November 12th, 2006 - 10:24PM
Great -- I've been looking for something like this.
I've followed your instructions, but then, how do I look at what's been downloaded -- where can I see this information?
Thanks,
Tom
2. Colm on November 13th, 2006 - 9:13AM
Hi Thomas,
In GA the documents should look like regular entries in the top content section.
Their paths will be where ever they reside if you were to link to them directly.
So for our site, most of our downloads are in a folder called downloads - and our PDFs would be something like /downloads/myfile.pdf
Hope this helps :)
3. Tomi on January 30th, 2007 - 5:34AM
Great!!
How can we track external links as well?
4. Colm on January 30th, 2007 - 11:40AM
Hi Tomi,
That should be pretty easy too, by changing the javascript to check the url doesn't contain the current hostname.
If I get time to update the script I might post that one too.
5. Maura on February 8th, 2007 - 9:48AM
Hi Colm
Thats deadly, thanks. Just tried it to track .mp3 downloads.
Maura.
6. Scott Hendison on February 11th, 2007 - 4:08PM
Colm,
Would you please post the code for tracking the external URL's. Tomi's question has been boggling my mind...
That would be a big help to figure out.
7. Colm on February 14th, 2007 - 7:49PM
I've updated the script, the one above now contains code to track external links.
See my post for more information
8. Maura on February 15th, 2007 - 1:10PM
Hi Colm,
Just wondering does the script track .m4b files? As if you don't have engough work!!
Maura.
9. Colm on February 15th, 2007 - 6:04PM
In the js file there is a regular expression with the following:
doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3
just add m4b to it so you would have:
doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|m4b
Hope this helps.
10. Dan on February 16th, 2007 - 5:03PM
Hi Colm,
Thanks for all the work you did making this script. I tested it last night in order to track PDF downloads, but all GA indicates as being accessed is
/undefined
as opposed to indicated the specific name of the downloaded pdf file.
Do you know what I'm doing wrong? Thanks in advance.
Dan
11. Colm on February 16th, 2007 - 5:09PM
Hi Dan,
There's several factors that could warrant that result - can you post up a link and see if we can solve the issue?
if you don't want a public comment then fill in our contact form http://www.iqcontent.com/about/contact/
Colm
12. SummitExceler on February 19th, 2007 - 4:16AM
Can I follow suit as described in post 9 and simply include "xls" and "xlt" in the js string in order to also track downloads of Excel workbooks and templates from a web page?
13. Colm on February 19th, 2007 - 2:47PM
SummitExceller - yeah thats all you need to do alright.
14. Jim Richq on March 8th, 2007 - 8:06PM
Can this script be modified to capture the userid of the person who simple opens one of these files? I have a request to record who looks at certain files on our intranet.
Thanks,
Jim
15. Colm on March 12th, 2007 - 10:41AM
Hi Jim,
Google Analytics only works on public websites and not intranets. However if you have the full version of Urchin, you could modify the script to do that.
Colm
16. Scott on March 15th, 2007 - 10:49PM
Thanks for the script...nice piece of work. I am trying to track mp3 downloads. I basically have images that are linked to mp3 files. I know someones sort of asked this before but could anyone tell me where to specifically find this information in google analytics, theres a ton of information and drop downs there. I've downloaded a few to test but haven't seem them provided in the info anywhere. Your help is greatly appreciated.
Scott
17. Colm on March 20th, 2007 - 1:16PM
Scot: If you have placed the script in the correct place on your page, you should be able to find the mp3's (or other files) if you go into:
Content Optimisation > Content Performance > Top Content
or
Content Optimisation > Content Performance > Content Drilldown
The path to your mp3 (or other file) should show up in both of these locations.