How to Track Downloads in Google Analytics

How to Track Downloads in Google Analytics

Posted by on Fri, Apr 29, 2011
Filed Under | Google Analytics


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
<script type="text/javascript">
if (typeof jQuery != 'undefined') {
	jQuery(document).ready(function($) {
		var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
		var baseHref = '';
		if (jQuery('base').attr('href') != undefined)
			baseHref = jQuery('base').attr('href');
		jQuery('a').each(function() {
			var href = jQuery(this).attr('href');
			if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
				jQuery(this).click(function() {
					var extLink = href.replace(/^https?\:\/\//i, '');
					_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
					if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = href; }, 200);
						return false;
					}
				});
			}
			else if (href && href.match(/^mailto\:/i)) {
				jQuery(this).click(function() {
					var mailLink = href.replace(/^mailto\:/i, '');
					_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
				});
			}
			else if (href && href.match(filetypes)) {
				jQuery(this).click(function() {
					var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
					var filePath = href;
					_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
					if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
						setTimeout(function() { location.href = baseHref + href; }, 200);
						return false;
					}
				});
			}
		});
	});
}
</script>

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.

Enjoy this post?

Join the discussion below, subscribe to our RSS feed or share it on the web.

This post was written by:

has written 25 posts on the Web Analytics Blog.

Joe is the Analytics Director and a Partner at Blast Analytics & Marketing. He understands Google Analytics like nobody else and is a master of many programming languages.

Add Joe to your circles on Google+


Tags: , ,
  • Doru Catana

    how soon will the event tracking refresh? (see actual data)

  • http://www.blastam.com Joe Christopher

    @dorucatana:disqus Event data is processed generally between 4-24 hours. In some cases, it can be quicker and in other cases longer (even around 48 hours for very high volume, non GA Premium sites). It depends on your site’s volume.

  • john bracey

    You spoke about an automatic tag generation app Analytics Engine. Are there apps like this that can generate tags across multiple analytic platforms that you know of? We often run multiple analytic platforms simulationiously.

  • http://www.blastam.com Joe Christopher

    @johnbracey:disqus Not specifically, but check out this solution, along the same idea: https://segment.io/libraries/analytics.js

  • john bracey

    Glanced over it and it sees like that would certainly be a solution. Do you know if the solution works for Omniture and/or web trends for example? (I want to make sure it doesn’t only apply to open source Analytic tools)

  • http://www.blastam.com Joe Christopher

    I don’t believe they have support for either of those tools. Your other option is to build javascript wrapper functions. I’ve done this in the past and it worked out well.

  • Charles Guo

    Thanks for sharing! Great article!

  • http://www.facebook.com/ernie.natera Ernie Natera

    Thanks for making it so easy. Can you review mine? I entered it on some pdf links to test, but nothing is being recorded in GA.
    http://svswa.org/board_of_directors.cfm

  • http://www.blastam.com Joe Christopher

    @twitter-632430750:disqus thanks for sharing. Pretty cool service.

  • http://www.facebook.com/kristin.dean.50 Kristin Dean

    This is hands down the most helpful post I have found on this topic. Setting the delay made all the difference in my events showing up in GA. Thank you!

  • Thom

    Hi, thanks for this. Very helpful.

    Is there any way to track downloads that don’t occur as a result of someone clicking on the link on my website? For example, if another website includes the direct URL for a PDF on my site, then vistors won’t ever click on the link on my site and presumably GA won’t count this?

    Any help greatly appreciated.

  • http://www.ysugarcrm.com/ sugarcrm development

    I putted inline download code for a PDF eBook on my website but the problem is there, I haven’t seen anything in Google Analytics events, real time or etc. Is there any other code on my page that is preventing
    tracking code from working?



Goal Driven Online Marketing & Analytics
Copyright © 1999-2013 Blast Analytics & Marketing