Update! We have a new script for tracking downloads. Check it out our new script and how to use it.
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.
November 12, 2006 at 10:24 pm
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
November 13, 2006 at 9:13 am
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
January 30, 2007 at 5:34 am
Great!!
How can we track external links as well?
January 30, 2007 at 11:40 am
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.
February 8, 2007 at 9:48 am
Hi Colm
Thats deadly, thanks. Just tried it to track .mp3 downloads.
Maura.
February 11, 2007 at 4:08 pm
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.
February 14, 2007 at 7:49 pm
I’ve updated the script, the one above now contains code to track external links.
See my post for more information
February 15, 2007 at 1:10 pm
Hi Colm,
Just wondering does the script track .m4b files? As if you don’t have engough work!!
Maura.
February 15, 2007 at 6:04 pm
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.
February 16, 2007 at 5:03 pm
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
February 16, 2007 at 5:09 pm
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
February 19, 2007 at 4:16 am
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?
February 19, 2007 at 2:47 pm
SummitExceller – yeah thats all you need to do alright.
March 8, 2007 at 8:06 pm
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
March 12, 2007 at 10:41 am
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
March 15, 2007 at 10:49 pm
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
March 20, 2007 at 1:16 pm
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.
Pingback: » Tracking documents and external links in google analytics - iQ Blog
July 15, 2008 at 9:58 pm
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.
July 18, 2008 at 11:33 am
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!)
August 5, 2008 at 3:59 pm
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
August 14, 2008 at 3:58 pm
Hi Ulf,
we’ve updated the code, it’s available here: http://www.iqcontent.com/js/gataglinks.js
September 3, 2008 at 11:53 am
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!
September 5, 2008 at 11:10 am
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)?
September 12, 2008 at 12:13 pm
Hi Colm,
thank you very much for the code.
is there an opportunity to track form submissions with this code, too?
September 30, 2008 at 11:54 am
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)?
October 2, 2008 at 3:10 am
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 ??
October 2, 2008 at 6:52 pm
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.
October 8, 2008 at 11:14 am
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?
October 8, 2008 at 11:16 am
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?
October 23, 2008 at 12:26 pm
(What happened to my post?
)
October 23, 2008 at 12:27 pm
>All comments are immmediately posted >(unless you write something that looks >like spam!)
Why can’t I post?
November 4, 2008 at 1:07 pm
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.
November 6, 2008 at 2:27 pm
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.
November 9, 2008 at 6:26 am
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.
November 25, 2008 at 5:17 pm
HOw to track mp3 and downloads in IWEB?
Thanks,
November 25, 2008 at 10:09 pm
I added the script, then modified the link, but I’m not getting any results. Did I mess up:
http://www.joshholliday.com
December 4, 2008 at 6:47 am
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