Google Analytics Hack: Track Outbound Links In 2 Easy Steps
Posted March 14, 2009 by Brian Cray
Reading time: About 2 minutes
Inbound links answer a basic question: How do people find my site? But that’s just half the equation. People come and go. Where are they all going? Are people leaving your website because they aren’t engaged or because they are following one of your outbound links?
Those outcomes suggest two different things about the effectiveness of your website. In the first case your website isn’t effective; in the second case your visitors read enough to at least follow your links.
Outbound links are not tracked by Google Analytics despite their potential value. That is to say without a little programming wizardry and Google Analytics hackery [insert geek smirk here].
Step 1: Programming wizadry with Javascript
The first step is to add some javascript magic to each of your webpages. You can either copy and paste the following source code into one of your existing javascript files, or you can download a copy of the source code here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | if(typeof jQuery == 'function') { /* use jQuery if it exists because it is a more elegant solution */ jQuery(function () { jQuery('a:not([href*="' + document.domain + '"])').click(function () { pageTracker._trackPageview('/outgoing/' + jQuery(this).attr('href')); }); }); } else { /* use regular Javascript if jQuery does not exist */ window.onload = function () { var links = document.getElementsByTagName('a'); for (var x=0; x < links.length; x++) { links[x].onclick = function () { var mydomain = new RegExp(document.domain, 'i'); if(!mydomain.test(this.getAttribute('href'))) { pageTracker._trackPageview('/outgoing/' + this.getAttribute('href')); } }; } }; } |
The above code works by telling Google Analytics to record a page view for every outbound link. As a result, Google sees outbound links as another page view on your website under the subdirectory /outgoing/. So, for example, if someone clicks a link to http://briancray.com/ on your website, Google Analytics will record a page view of /outgoing/http://briancray.com/. See the example screenshot below taken from the Content � Overview section of Google Analytics:
![]()
Step 2: Google Analytics hackery to filter outbound links
You may have already realized that the above script introduced a slight problem: Your page views now account for actual page views as well as outbound links. If this doesn’t concern you, you can quit reading now and enjoy the script. If this does concern you, here’s step–by– screenshots to explain how to filter outbound links in Google Analytics:
Screenshot 1

Screenshot 2

Screenshot 3

Screenshot 4

Screenshot 5

Screenshot 6

Once you apply your new custom segment in Google Analytics, you will only see traffic details for the outbound links. To filter out the outbound links and see only normal traffic, you would set the “Condition” dropdown to “Does not contain” instead of “Starts with” as seen in screenshot #4.
That’s it!
I hope you enjoyed this little Google Analytics hack and Javascript magic. If you enjoy javascript, jquery, and pretending to be a real hacker, feel free to read my other entries and subscribe to my blog.
25 Article comments
Show/add comments