Tracking document downloads in Google Analytics
– posted November 6th, 2006 by Colm McBarron Comments (38)
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 Content, Web Site Management, Web analytics


38 comments so far
1. Thomas on Nov 12th, 2006 - 22:24
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 Nov 13th, 2006 - 09:13
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 Jan 30th, 2007 - 05:34
Great!!
How can we track external links as well?
4. Colm on Jan 30th, 2007 - 11:40
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 Feb 8th, 2007 - 09:48
Hi Colm
Thats deadly, thanks. Just tried it to track .mp3 downloads.
Maura.
6. Scott Hendison on Feb 11th, 2007 - 16:08
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 Feb 14th, 2007 - 19:49
I’ve updated the script, the one above now contains code to track external links.
See my post for more information
8. Maura on Feb 15th, 2007 - 13:10
Hi Colm,
Just wondering does the script track .m4b files? As if you don’t have engough work!!
Maura.
9. Colm on Feb 15th, 2007 - 18:04
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 Feb 16th, 2007 - 17:03
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 Feb 16th, 2007 - 17:09
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 Feb 19th, 2007 - 04:16
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 Feb 19th, 2007 - 14:47
SummitExceller - yeah thats all you need to do alright.
14. Jim Richq on Mar 8th, 2007 - 20:06
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 Mar 12th, 2007 - 10:41
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 Mar 15th, 2007 - 22:49
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 Mar 20th, 2007 - 13:16
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.
18. » Tracking documents and external links in google analytics - iQ Blog on Jul 11th, 2008 - 11:10
[...] on from my previous post on tracking documents with google analytics, I’ve updated the javascript to now track external links as well as non-html [...]
19. john on Jul 15th, 2008 - 21:58
Hi there,
I have pasted the GA code just before the . As you said to paste your code there as well.
I have found few other blogs telling to paste them just after tag.
Now I am a bit confused. Some argued that if someone clicks the file before the whole page downloads, GA will not show it on the report.
Would appreciate ppreciate your comment.
20. john on Jul 18th, 2008 - 11:33
Just an update, I have implemented the JS and the code but it is not working. I asked my friends to download files but it is not working(yet!)
21. Ulf on Aug 5th, 2008 - 15:59
Hi Colm,
could you extend your script for ga.js? Currently it is only working with the old urchin.js
The change would be only one line: pageTracker._trackPageview(file_path);
instead of
urchinTracker(file_path);
Kind regards
Ulf
22. Laurence Veale on Aug 14th, 2008 - 15:58
Hi Ulf,
we’ve updated the code, it’s available here: http://www.iqcontent.com/js/gataglinks.js
23. Casparina on Sep 3rd, 2008 - 11:53
This is really useful. I was not looking forward to manually adding the GA tracking code to every single link (I’m planning to have loads of PDFs on my site).
So thanks very much - you’ve saved me alot of time!
24. Paul on Sep 5th, 2008 - 11:10
Hi Colm,
I have no need to track the external links and was wondering what code should be used to simply track document downloads ( I believe this was the original functionality)?
25. Kathleen on Sep 12th, 2008 - 12:13
Hi Colm,
thank you very much for the code.
is there an opportunity to track form submissions with this code, too?
26. Andrew on Sep 30th, 2008 - 11:54
Hi Colm,
I have no need to track the external links and was wondering what code should be used to simply track document downloads ( I believe this was the original functionality)?
27. Matthias on Oct 2nd, 2008 - 03:10
I installed the script and i can see the /exlink pages in my GA, but it only shows “undefinded/undefined” for the hostname and pathname.
I was wondering if it has sth to do with the getElementsByTagName(”a”) in the JS code… where should the “a” come from ??
28. Kyala on Oct 2nd, 2008 - 18:52
Thanks for the script. I had problems with the script distinguishing external downloads from internal downloads, and had local downloads being reported like:
/exlinks//www.mydomain.com/down/foo.pdf
instead of:
/down/foo.pdf
The problem seemed due to this.hostname being undefined in the “location.host != this.hostname” condition, so I used array_element.srcElement.hostname in place of it, which seems to work fine.
If anyone else has the same problem and can’t do the edit, I can send you my edited script.
29. Shawn Koppenhoefer on Oct 8th, 2008 - 11:14
I think that I understand that this script method only tracks clicks made to resources referenced on the pages,.. but how can track people arriving at those resources (e.g., pdf files) from a an external source (e.g., an email containing a url to the pdf).?
Could we make a sort-of fake .pdf file that encapsulates the real pdf file with javascript php or other?
30. Shawn Koppenhoefer on Oct 8th, 2008 - 11:16
oops.. I forgot to ask…
I’m unable to add the code BEFORE the google analytics code.. but I CAN add it right AFTER (but before the body close).. Is this a deadly error-waiting-to-happen?
31. Eivind on Oct 23rd, 2008 - 12:26
(What happened to my post? :-o)
32. Eivind on Oct 23rd, 2008 - 12:27
>All comments are immmediately posted >(unless you write something that looks >like spam!)
Why can’t I post?
33. Ben Bishop on Nov 4th, 2008 - 13:07
Hi, thanks for this script. Unfortunately it doesn’t seem to be modifying my pdf links with the js. I am using the latest ga code and have the link to your file just before my js for Analytics. Any ideas why this might happen? Think I may be doing something odd.
Here is the page http://r2rtennis.co.uk/courses/
Thanks very much for your time.
34. Hubert on Nov 6th, 2008 - 14:27
Hi Kyala,
>The problem seemed due to this.hostname being undefined in the “location.host != this.hostname” condition, so I used >array_element.srcElement.hostname in place of it, which seems to work fine.
Actually you’re a right, the only problem is that srcElement only seems to work with IE.
Firefox doesn’t know the srcElement property.
I solved the problem by falling back to currentTarget property if srcElement is undefinied (which is the case for Firefox).
See the coe here:
—————————————-
function trackfiles(array_element) {
file_path = “”;
targetHostname = “”;
if (!array_element.currentTarget) {
// works in IE
targetHostName = array_element.srcElement.hostname;
} else {
// works in FF
targetHostName = array_element.currentTarget.host;
}
if (location.host != targetHostName) {
file_path = “/exlinks/” + ((array_element.srcElement) ? “/” + array_element.srcElement.hostname : this.hostname);
}
file_path = file_path + ((array_element.srcElement) ? “/” + array_element.srcElement.pathname : this.pathname);
pageTracker._trackPageview(file_path);
}
—————————————-
I’d be happy if somebody could review the code since I’m no JavaScript expert.
In any case this fix should be included into the script because it’s a major drawback to this great script.
35. Ben on Nov 9th, 2008 - 06:26
I have added the taglinks.js code right before and then the GA tracking code under that. I have clicked a few of the links myself but don’t see any downloads appearing on GA. Once clicked, will it only record the download if I actually save the file or does that not matter?
I have uploaded the js file to the root of my web server and used the following code on my page:
Could someone help me out? Thanks.
36. Carlos on Nov 25th, 2008 - 17:17
HOw to track mp3 and downloads in IWEB?
Thanks,
37. Josh on Nov 25th, 2008 - 22:09
I added the script, then modified the link, but I’m not getting any results. Did I mess up:
http://www.joshholliday.com
38. auska on Dec 4th, 2008 - 06:47
1. I like to know it is possible to check whether the GA will provide the status of Attempts made to download a .pdf(eg) file or it will show the status of the completed downloads of a (.pdf) files.
2.Let me know it is possible to track the download starttime and download endtime of a particular (.pdf) file using GA
Thanks in advance