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.

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