task Tracking File Downloads and External Links with Google Analytics


Google Analytics is pretty robust. Out of the box, it provides most of the stats that website owners and administrators need to understand how much their site is being used, how it's being, by who, when, and from where. The one thing it doesn't do automatically though, is track file downloads -- downloads of files such as PDFs, PowerPoints, Word Docs, MP3s etc. It only tracks the loading of webpages by default.

We work with nonprofit clients that provide a lot of content via PDFs, such as reports, policy briefs, fact sheets, infographics and more. Getting a complete understanding of how much their resources are being used for the sake of ROI analysis and more, requires that we capture data about usage of these files too. Our current approach to satisfy this need is to use Google Analytics event tracking, which allows us to record specific actions users take on our websites -- such as clicking buttons, playing videos etc. And for this specific need, what we track are clicks on links that lead to specific files (e.g, links that go to PDF files).

One of our implementations uses the code below. It's a revised version of code presented on blastam.com from 2013 that required revision in order to work with the lastest Google Analytics tracking code (i.e., analytics.js instead of the legacy ga.js). Our revised version tracks file downloads for any of the specified file types, as well as links to external websites. Note that it requires jQuery and assumes that you have the Google Analytics tracking code installed on your site.

	// Google Analytics tracking of file downloads and external links

	    // Modified version from http://www.blastam.com/blog/index.php/2013/03/how-to-track-downloads-in-google-analytics-v2

	    // Modified to work with analytics.js (instead of legacy ga.js)

	    // Modified to not track mailto links

	    var filetypes = /.(pdf|doc.*|xls.*|ppt.*|mp3)$/i;

	    var baseHref = '';

	    if ($('base').attr('href') != undefined) baseHref = $('base').attr('href');

	    $('a').on('click', function(event) {

	      var el = $(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(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 track = false;

	        if (track) {

	            // _gaq.push(['_trackEvent', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value, elEv.non_i]);

	            ga('send', 'event', elEv.category.toLowerCase(), elEv.action.toLowerCase(), elEv.label.toLowerCase(), elEv.value);

	            if ( el.attr('target') == undefined || el.attr('target').toLowerCase() != '_blank') {

	                setTimeout(function() { location.href = elEv.loc; }, 400);

	                return false;





It's important to note that tracking clicks on links to PDF documents and other files is, at best, a proxy for actual file downloads. The technique will miss downloads that don't occur on the website. For example, the solution we present below won't capture the following:

  • Users that find a direct link to the PDF/file from a third-party website (including Google)
  • Users that click on a direct link to the PDF/file within an email
  • Users that download the file via a direct bookmark to the file.

In order to capture those direct downloads as well, you would need to consider doing something like server log analysis.