Automatically Tracking Downloads and Outbound Links

IMPORTANT: This is a historical post from 28 September 2009.

The following post outlines one method for achieving automatic download and outbound link tracking for Google Analytics.

Although manually tagging files and links is the best way to track your downloads and outbound links, it is not very practical. Tagging more than a handful of links becomes painful.

Okay, so what are the downsides to automatic tracking?

  • The JavaScript required to modify the links is going to add to the loading time of your site.
  • If you have many, many files or links on a page it is going to take time for the script to run through your page and apply the tags to each file or link.

Quick steps:

  1. Download the following file (gatag.zip), unzip it and upload it to your server (in this example we have uploaded it into a folder called /javascript/).
  2. Place the following code above your Google Analytics Tracking Code:

This script only works for the ga.js Google Analytics Tracking Code, if you are still using urchin.js you will need to update this first.

This script was originally developed by Goodwebpractices.com with the help of ShoreTel, MerryMan and Colm McBarron. Our version has had some slight modification from VKI Studios and ourselves, Mangold Sengers.

Your Google Analytics Tracking Code should now look something like this:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try {
var pageTracker = _gat._getTracker("UA-123456-1");
pageTracker._trackPageview();
} catch(err) {}

Check your Google Analytics reports in 12 to 24 hours to ensure that file downloads and outbound links are tracking correctly. You should see your downloads and links being tracked into the Top Content and Content Drilldown reports e.g. /downloads/path-to-file/file.pdf and /outbound/www.externalsite.com/index.html