Automatically Track Outgoing Links in Google Analytics with Javascript

I’m currently reading the O’Reilly Google Analytics book, so of course I’m going to sandbox some stuff (albeit not here at this site.) One of the first things I seized on was the ability to track outgoing links by calling the urchinTracker() function onclick. Taking a few minutes out of my morning I put this together:

//window.onload we run this
var anchors=document.getElementsByTagName("a");
for (i=0;i<anchors.length;i++) {
    anchors[i].onclick=trackOutBoundLinks;
}

the trackOutBoundLinks function looks like this:

function trackOutBoundLinks(){
var thelink=this.href;
if (thelink.indexOf(location.hostname) ==-1 ){
   urchinTracker('outgoing:'+thelink+'');
   document.location.href=''+thelink+'';
   return false;;
   }
}

And with that you get fancy entries in your content list like this:

"/outgoing:http://www.dccomics.com/graphic_novels/?gn=7679"

Of course, the above will skew your page views, etc. so you’ll need to apply some filters and/or create a new profile, if you want to track page views in the default way in addition to tracking outgoing links, etc. but all that’s a Google Analytics matter*. The biggest JavaScript caveat is that if you’ve got any other onclick events on your anchor tags the above will muck with them. But if not, it’s that simple to start tracking outgoing links. Pretty cool, I think.

Here’s a zip file with a small script file that will insert the above into any page it’s attached to. I might write a more generic version of this that will work in any environment if I get the time, but for now if people are interested here it is to play around with. Simply upload the analytics.js to your server and attach it to the page in question and it should work:

<script src="/your-path-to/analytics.js" type="text/javascript"></script>

*albeit a pretty big one. I can’t stress enough that this code will completely skew your content reports. You need to have two profiles- one which filters against outgoing: and one which doesn’t. The one which does will track page views in the normal way, the one that doesn’t will have all the fancy outgoing links interspersed with the content reports. I am not savvy enough with analytics to solidly answer questions about that side of this business, so if you’re unsure look to some Google Analytics expert for sage advice on the ins and outs of profiles and filters. Maybe after reading the rest of this book and sandboxing these techniques for a month or two I’ll be more certain about some of these things, but for now I’m a javascript nerd who’s just exploring the GA landscape.

6 thoughts on “Automatically Track Outgoing Links in Google Analytics with Javascript

  1. Hi,

    I did something similar using the jquery framework.

    This is the code I use:

    $(document).ready(function() {
    $(“a[@href^=http://www.xyz.com]”).each(function() {
    var thelink=$(this).attr(‘href’);
    $(this).attr(“onclick”, “urchinTracker (‘/outgoing:”+thelink+”‘);”);
    });
    });

  2. with onClick you may lose some information. If you click the right button and select the context menu “Open in new tab” – event onClick does not work, Google Analytics will not count anything, you lose some visits or events. read about it here http://chenado.net/en/1040.html

  3. Thanks for pointing that out. Yet another place where GA can lose some information. It’s an imperfect science.

Leave a Reply

Your email address will not be published. Required fields are marked *