Wednesday, January 21, 2009

Google Analytics: script to track outbound links and downloads

Update 2011-10-10! Check out gaAddons free script example! 
Important! Please see "gaAddons open source project: enhancing Google Analytics" instead.
In my opinion, tracking outbound links and downloads should be part of any good web analytics implementation.

In the past, Google Analytics trackPageView() call was often used to track outbound links and downloads. However, with the introduction of events tracking, I find it much more appropriate to use this technique. Counting downloads and outbound links as additional page views impacted several reports (Pageviews, Pages/Visit, Bounce Rate, Top Content, etc.) and people often had to set Goals in order to track those specific Pageviews as success events. No more!

Introducing gaAddons.js

The goal of this script is to automate a couple of common tagging requirements: outbound links, download links and email links tracking. It makes the job very easy and reduce risks of errors. Basically, just add a reference to gaAddons.js as explained bellow and voilĂ !

Outbound links:
  • Event: "outbound"
  • Action: "click"
  • Label: target URL
Email links:
  • Event: "mailto"
  • Action: "click"
  • Label: email
Download links:
  • Event: "download"
  • Action: "click"
  • Label: URL of downloadable document. The default looks for the following regular expression:
    /\.(docx*|xlsx*|pptx*|exe|zip|pdf|xpi)$
    It basically says: "Look for any links ending with a dot, followed by any of the popular file extension"
    Those can easily be changed in the gaAddons.js script.
If you have other automation you would like to see let me know. You are free to modify this script and re-distribute it as long as you keep the references to the authors at the top of the script.

Get going with gaAddons.js

To get going, do the following:
  1. Get the gaAddons.js script and save it somewhere on your own server
  2. Add this line near the closing BODY tag of all your pages, just after the standard Google Analytics tag:
    <script src="/js/gaAddons.js" type="text/javascript"></script>

Integrating the code

Your code should look like this:
<script src='http://www.google-analytics.com/ga.js' type='text/javascript'></script>
<script type='text/javascript'>
var pageTracker = _gat._getTracker("UA-999999-1");
pageTracker._trackPageview();
</script>
<script src='gaAddons.js' type='text/javascript'></script>

Your tracker variable should be named "pageTracker", otherwise you will need to modify the gaA_pageTracker variable in gaAddons.js code.

Looking at the stats

Once your change is live, give Google Analytics some time to gather data and look under Content/Event Tracking for outbound, mailto and downloads stats (if using events, the default), or under Content/Content Drilldown for /outbound, /mailto or /download statistics if using the page view method.

---
Credit where credit is due! A while back, Justin Cutroni at EpikOne published "Google Analytics Short Cuts". This script is an optimized/improved version of his script. Also inspired from the work of Brian Clifton, author of Advanced Web Metrics with Google Analytics. Contributors: Damon Gudaitis, Andy Edmonds.