Tracking document downloads in Google Analytics

By Colm McBarron | 38 Comments

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.


38 responses to “Tracking document downloads in Google Analytics

  1. 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?



  2. 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. 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.

  4. In the js file there is a regular expression with the following:


    just add m4b to it so you would have:


    Hope this helps.

  5. 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


    as opposed to indicated the specific name of the downloaded pdf file.

    Do you know what I’m doing wrong? Thanks in advance.


  6. 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?

  7. 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.



  8. 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.


  9. 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.


  10. 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


    Content Optimisation > Content Performance > Content Drilldown

    The path to your mp3 (or other file) should show up in both of these locations.

  11. 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.

  12. 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!)

  13. 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

    Kind regards

  14. 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!

  15. 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)?

  16. Hi Colm,
    thank you very much for the code.

    is there an opportunity to track form submissions with this code, too?

  17. 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)?

  18. 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 ??

  19. Thanks for the script. I had problems with the script distinguishing external downloads from internal downloads, and had local downloads being reported like:
    instead of:

    The problem seemed due to this.hostname being undefined in the “ != 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.

  20. 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?

  21. 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?

  22. 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

    Thanks very much for your time.

  23. Hi Kyala,

    >The problem seemed due to this.hostname being undefined in the “ != 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 =;

    if ( != 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);
    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.

  24. 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.

  25. 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

Comments are closed.