From the mind of Jamie Huskisson: Nottingham freelance UK PHP, Magento, Wordpress developer

Track outbound links for Google Analytics automatically

Posted on January 9th, 2007 in

Place this code before the </body> tag and it’ll track all outbound links that aren’t part of your website.

<script type=”text/javascript”>
if (document.getElementsByTagName) {
var ahrefs = document.getElementsByTagName(‘a’);
for (var i=0; i<ahrefs.length;i++) {
if (ahrefs[i].href.indexOf(‘http://www.jhuskisson.com‘) == -1 && !ahrefs[i].onclick) {
ahrefs[i].onclick = function () { var track = this.href + ”; urchinTracker (‘/outgoing/’+track.substring(7)); }
}
}
}
</script>

Simply replace http://www.jhuskisson.com with your own site URL and hey presto – all outbound links that aren’t internal are being tracked by Google Analytics.

Bookmark and Share

If you enjoyed this post:

Please take the time to leave a comment with any of your thoughts, follow me on Twitter or subscribe to my RSS Feed for automatic updates on all future posts.

There are 22 responses to this post so far

Feel free to use any of the following tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>