Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Digital Analytics

How to Track Downloads in Google Analytics

April 29, 2011
UPDATE: Please reference our updated post on tracking downloads and external links. It is now more efficient and flexible.

Google Analytics uses client-side code (JavaScript) to record pageviews and other interactions (which are then sent as a tracking pixel request to Google’s servers). This works great for html pages on your website (regardless of the programming language it was developed in) and it even works great for Flash, Silverlight, and other web technologies.

File Downloads

Out of the box, Google Analytics will not track how many times people download PDFs or other file types, simply because those files do not have the ability to request a tracking pixel. In this blog post, we’ll be covering the ways to properly track file downloads in Google Analytics.

Throughout this blog post, I’ll be assuming that you are wanting to track a PDF download, but keep in mind that you could use this same technique to track the download of a .mp3, .mp4, .xlsx, etc — it really does not matter. Additionally, I recommend tracking file downloads as events (so that you are not inflating pageviews on your site), but you could very well track them as pageviews. In the new Google Analytics v5, you can now setup events as goals!

The code you will want to use is different depending on if you are opening the PDF in a new window via target=”_blank” or if you are opening the PDF in the current window.

Tracking PDF Download (in new window/tab)

If you are linking to a PDF that opens in a new window (by using target=”_blank” for example), then the tracking code is fairly simple to implement.

Your link will look something like: <a href=”pdfs/my-file.pdf” target=”_blank”>Download my file</a>

To properly track this in GA, add the following code to link tag:

  • <a href=”pdfs/my-file.pdf” target=”_blank” onclick=”_gaq.push([‘_trackEvent’,’Download’,’PDF’,this.href]);”>Download my file</a>

When a user clicks to download your file, a tracking pixel is requested and recorded in GA’s database. Problem solved!

Tracking PDF Download (in current window)

If you do not use the link tag’s target attribute to open the PDF in a new window and instead you have it opening in the same window, some browsers will interrupt the tracking request and send the visitor straight to the download. When this happens, you won’t see the event recorded in GA.

Your link will look something like: <a href=”pdfs/my-file.pdf”>Download my file</a>

To properly track this in GA, add the following code to the link tag:

  • <a href=”pdfs/my-file.pdf” onclick=”var that=this;_gaq.push([‘_trackEvent’,’Download’,’PDF’,this.href]);setTimeout(function(){location.href=that.href;},200);return false;”>Download my file</a>

We have to give the browser enough time to make the request, and this is done by using the setTimeout() javascript function to delay the request by a fraction of a second. We typically do 200ms (that’s 1/5 of a second), but if you have more requests, you may want to increase this. You can use debugging tools to understand if you need to increase this value.

Automate It!

If you have a lot of file links on your site or you just don’t want to have to worry about adding this code each time you add a new link, then you can certainly automate the tagging for download tracking.

There are a number of solutions that help you do this and that also provide additional automated tracking functionality. One of our favorites is Analytics Engine, which is developed by our friends at Analytics Pros (a fellow Google Analytics Certified Partner). Definitely check out their product to learn more.

Another option is to use the code we’ve provided below. Note that this code requires the jQuery JavaScript library to be on your pages.

Feel free to customize this code. It can be placed in its own .js file and should be placed in the <head> on each of your pages. This script automates the following:

  • Tracks file downloads as events for the following extensions: .zip, .exe, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3 (again feel free to modify the list)
  • Tracks outbound clicks as events if the href value contains http:// or https:// and the domain value doesn’t match the current domain
  • Tracks mailto email clicks

The script detects to see if you are opening the file in a new window or not and automatically uses setTimeout() for 200ms if you are not. If you have multiple tracking objects on a page or have a named tracker, then you’ll want to customize this script. This script uses the asynchronous ga.js tagging syntax.

Bounce Rate Impact on Tracking File Downloads

When you fire event tracking or a virtual pageview, this will impact your bounce metric in a positive direction. Events and pageviews count as an interaction. If you have more than one interaction (pageview or event), then that visit is not counted as a bounce. Since the visitor took some action on the site, they should not be counted as a bounce anyways, so by coding your file download links, you are actually increasing the accuracy of your metrics.

Joe Christopher
About the Author

As Vice President of Analytics at Blast Analytics, Joe leads a team of talented analytics consultants responsible for helping clients understand and take action on their vast amounts of data, to continuously improve and EVOLVE their organizations. With over 20 years of experience in analytics and digital marketing, Joe offers a high-level of knowledge and guidance to clients across all industries. He is an expert in all major analytics platforms including Google Analytics and Adobe Analytics, as well as various tag management systems such as Tealium and Adobe Launch. He also consults on data visualization, data governance, and data quality strategies. Having extensive expertise in many areas, has enabled Joe to become a well known thought leader and speak at industry events such as Tealium’s Digital Velocity series. Joe remains on the pulse of various information technology, programming languages, tools and services, keeping Blast and its clients on the leading edge.

Connect with Joe on LinkedIn. Joe Christopher has written on the Blast Digital Customer Experience and Analytics Blog.