Tracking OutBound Clicks through Google Analytics (Events) The Easy Way

Ok, so, I wanted to track when anyone clicked on an outbound link. I researched the Google analytics code, And found I had to edit the head, and update every single link. This will be a complete pain to do. It would mean every time I come in and add a link, I would have had extra code to that link. So I knew there would be a better solution.

I started looking online and found SEO hacker had a great solution. I copied one set of code and pasted it into the head, and everything is now tracking Google analytics. If you like to see the full article, click here. Otherwise the code is below.

 

<!–THIS IS YOUR GOOGLE ANALYTICS TRACKING CODE–>

<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push([‘_setAccount’, ‘UA-xxxxxxxx-x’]);
_gaq.push([‘_trackPageview’]);

(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
var a = document.getElementsByTagName(‘a’);
for(i = 0; i < a.length; i++){
if (a[i].href.indexOf(location.host) == -1 && a[i].href.match(/^http:///i)){
a[i].onclick = function(){
_gaq.push([‘_trackEvent’, ‘outgoing_links’, this.href.replace(/^http:///i, ”)]);
}
}
}
</script>

<!–END RECORD OUTBOUND LINKS CODE–>



Leave a Reply


%d bloggers like this: