Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Digital Analytics

How to Track Downloads & Outbound Links in Universal Analytics

September 4, 2013

By default Google Analytics does not track file downloads, email, telephone or other outbound link clicks automatically. We developed dynamic link tracking code a couple years ago to address this and due to the popularity, we revisit it from time to time as the Google Analytics platform evolves.

The latest evolution is Universal Analytics and we have updated the code we provided in our previous ‘How to Track Downloads & Outbound Links in Google Analytics’ blog post to use the new Universal Analytics syntax. The recent version of the tracking code includes an additional feature that leverages the NEW hit callback functionality.

If you are not yet familiar with Google’s Universal Analytics, we encourage you to read a brief case study, ‘Yellow Pages Group New Zealand Upgrades to Universal Analytics‘, about one of our Google Analytics Premium clients who is utilizing Universal Analytics. The post outlines the benefits of the new platform, as well as, how we leverage and extended Universal Analytics to meet their unique business needs.

Dynamically Track Downloads & Other External Links

The code below will dynamically detect clicks on file downloads and external links. As before, this code requires the jQuery JavaScript library to be set before the code.

The code can be easily modified to suit your needs. We recommend placing the code in its own .js file. This script automates the following:

  • Tracks file downloads as events for the following extensions: .zip, .exe, dmg, .pdf, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .txt, .rar, .wma, .mov, .avi, .wmv, .flv, .wav (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
  • Tracks Tel telephone clicks

In addition, the code takes advantage of a new Google Universal Analytics callback feature to determine when the hit request finishes sending data. This is especially useful in cases where the link does not open a new window, since the hit request needs time to finish before taking the user to their destination. This eliminates the requirement of putting a timeout in place to delay the click longer than it needs to be delayed (or quicker than it should on high latency connections).

[sourcecode language="js"]<script type='text/javascript'>
if (typeof jQuery != 'undefined') {
	var filetypes = /\.(zip|exe|dmg|pdf|doc.*|xls.*|ppt.*|mp3|txt|rar|wma|mov|avi|wmv|flv|wav)$/i;
	var baseHref = '';
	if (jQuery('base').attr('href') != undefined) baseHref = jQuery('base').attr('href');
	var hrefRedirect = '';

	jQuery('body').on('click', 'a', function(event) {
		var el = jQuery(this);
		var track = true;
		var href = (typeof(el.attr('href')) != 'undefined' ) ? el.attr('href') : '';
		var isThisDomain = href.match(document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]);
		if (!href.match(/^javascript:/i)) {
			var elEv = []; elEv.value=0, elEv.non_i=false;
			if (href.match(/^mailto\:/i)) {
				elEv.category = 'email';
				elEv.action = 'click';
				elEv.label = href.replace(/^mailto\:/i, '');
				elEv.loc = href;
			}
			else if (href.match(filetypes)) {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				elEv.category = 'download';
				elEv.action = 'click-' + extension[0];
				elEv.label = href.replace(/ /g,'-');
				elEv.loc = baseHref + href;
			}
			else if (href.match(/^https?\:/i) && !isThisDomain) {
				elEv.category = 'external';
				elEv.action = 'click';
				elEv.label = href.replace(/^https?\:\/\//i, '');
				elEv.non_i = true;
				elEv.loc = href;
			}
			else if (href.match(/^tel\:/i)) {
				elEv.category = 'telephone';
				elEv.action = 'click';
				elEv.label = href.replace(/^tel\:/i, '');
				elEv.loc = href;
			}
			else track = false;

			if (track) {
				var ret = true;

				if((elEv.category == 'external' || elEv.category == 'download') && (el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') ) {
					hrefRedirect = elEv.loc;

					ga('send','event', elEv.category.toLowerCase(),elEv.action.toLowerCase(),elEv.label.toLowerCase(),elEv.value,{
						'nonInteraction': elEv.non_i ,
						'hitCallback':gaHitCallbackHandler
					});

					ret = false;
				}
				else {
					ga('send','event', elEv.category.toLowerCase(),elEv.action.toLowerCase(),elEv.label.toLowerCase(),elEv.value,{
						'nonInteraction': elEv.non_i
					});
				}

				return ret;
			}
		}
	});

	gaHitCallbackHandler = function() {
		window.location.href = hrefRedirect;
	}
}
</script>
[/sourcecode]

The script sets download, email and tel link clicks as interaction events while the external site clicks are non-interaction, which can be adjusted as needed.

Detailed Download & External Link Reports

Once you have implemented the new tracking code you will be presented with well organized event data around file downloads, external link, email link, and telephone link clicks. These reports are especially useful to determine the effectiveness of your file downloads and various links.

events_report_new

If you would like to see the most popular downloads, you can do so by drilling down into the event category and selecting event label as the primary dimension. Here is a download report example:
events_downloads_new

Let us know if you have any questions about this new evolution of the outbound link and download file tracking code, or if you want to know more about Google Universal Analytics in general.

Ryan Chase
About the Author

Ryan is a Senior Analytics Consultant at Blast Analytics. He is passionate about helping businesses improve their usage of data to make better decisions. He has experience across industries and analytics platforms to help set and meet long term business goals.

Connect with Ryan on LinkedIn. Ryan Chase has written on the Blast Digital Customer Experience and Analytics Blog.