How to Track File Downloads on Your Website

We have already discussed some relevant SEO tools and recommended Google Analytics as one of the ways to track web visitor traffic, goal conversions and get insightful analytics related to search engine optimization and digital marketing. In this article, I will lay out a very useful technique that allows you to track file downloads (be it a pdf, doc, xls, etc.) and clicks on links that lead to external websites. The technique below uses a simple global JavaScript that tracks all the clicks and displays the results in your Google Analytics statistics.

Tracking file downloads or clicks on external links behavior is very important to analyze if people are interacting with the website the way intended. For example, businesses that have their brochures or product catalogs downloadable as PDFs can see if their publications are in demand and what the best way is to position the file links on the website to increase the clickthrough rate.

The official Google Analytics tutorial says that any link can be tracked if the script below is added in each link that you'd like to track:

onclick="pageTracker._trackPageview('/file_name.file_extension').

If you already have lots of pdfs, docs or other files that need to be tracked, adding the aforementioned script to every download link on your website can be a very tedious task. So we will present a much simpler way to track file downloads and clicks on external links. Here we go:

  1. Download Google Analytics file download tracking script and upload it to your web site (add it to this folder: http://www.yoursite.com/scripts/ )
  2. Add the previously downloaded JavaScript code in thesection of your page: <script src="http://www.yoursite.com/scripts/gatag.js" type="text/javascript"></script>
  3. Add jQuery JavaScript library

Note: This tracker is written for Google Analytics new tracking code which uses ga.js. If you are using the old tracking code with urchin.js, you should not use the method described above to track your file downloads. The script also detects whether you open the file in a new window (_blank) or not and then automatically uses setTimeout() for 200ms if you are not. If you have multiple tracking objects on a page or have a named tracker, we recommend to customize the script to suit your needs.

You can also check out the SEO Book's tutorial on how to track events (file downloads, clicks, etc) in the new Google Analytics.

Oleksiy is the Founder of ArtDriver. He oftentimes takes the lead as the Agile Project Manager and SEO expert on selected projects, which allows him to be hands-on with the latest trends. In his spare time, Oleksiy enjoys playing the guitar and spending time with his family.

  • Keith McAleer

    Hello,

    Thanks for the tutorial. I have been trying to get tracking to work for months, and this seems like the most promising thing that I've found, but I am still not sure if it works.

    I downloaded gatag.js and placed it in my /scripts/ folder

    then I embedded the js in my page with this code:

    I put the code right before the analytics js (which is right before /head)

    I uploaded that to our site and then downloaded our podcast episode and clicked on a few links to see if something changed. I looked in analytics and did not see anything in the events tracking or anywhere else that would reference these links being tracked.

    Any ideas? (www.allanandjacksonloveamerica.com)

  • Sorry, here is the code I put in to track:

  • Keith McAleer

    I guess pasting code doesn't work...maybe If I do it without the brackets..

    script type="text/javascript" src="scripts/gatag.js"
    /script

  • Oleksiy Synelnychenko

    Keith, we've updated the script, so please give it another shot and let us know if it works for you?