Google analytics is an easy way to track inbound links and on site page clicks. But there are also times when you need to track outbound links too. For example, I built a fairly basic website landing page where you could search for specific heating and cooling dealers by zip code. It can pull up the dealers address, phone number, a google map and the dealers website.
Standard google analytics track the inbound links, (i.e., they clicked a link on our local TV station’s website and were then directed to the landing page), but it doesn’t track the outgoing click (i.e. which dealers website did they click when they searched and where did they go when they left the website?).
Well here is a simple way to track outbound links and answer the question of where they went using some very simple (advanced?) features of google analyitics by simply formatting your websites links that you want to track outgoing clicks for, like this:
<a href=”http://www.example.com” target=”_blank” onClick=”recordOutboundLink(this, ‘Outbound Links’, ‘http://www.example.com’);return false;”/>Click Here To Visit Website</a>
Just to be clear, if you have multiple links like this:
Check out www.abc.com or www.123.com then you would format your links like this:
Link1 – www.abc.com
<a href=”http://www.abc.com” target=”_blank” onClick=”recordOutboundLink(this, ‘Outbound Links’, ‘http://www.abc.com’);return false;”/>Check out www.abc.com</a>
Link2 – www.123.com
<a href=”http://www.123.com” target=”_blank” onClick=”recordOutboundLink(this, ‘Outbound Links’, ‘http://www.123.com’);return false;”/>Check out www.123.com</a>
Once you have your links formatted properly on your site, just add this code directly below your current google analytics script in the footer of your website.
<script type=”text/javascript”>
function recordOutboundLink(link, category, action) {
try {
var pageTracker=_gat._getTracker(“UA-XXXXXXXX-X“);
pageTracker._trackEvent(category, action);
setTimeout(‘document.location = “‘ + link.href + ‘”‘, 100)
}catch(err){}
}
</script>
Notice the "UA-XXXXXXXX-X" on the fourth line of the code. You replace this with your own unique google tracking ID which can usually be found on the third line of you original google analytics script above where you just added this code.
<script type=”text/javascript”>
<code>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); <—Copy your number from here.
_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);
})();
</script>
<script type=”text/javascript”>
function recordOutboundLink(link, category, action) {
try {
var pageTracker=_gat._getTracker(“UA-XXXXXXXX-X“); <—then paste it here.
pageTracker._trackEvent(category, action);
setTimeout(‘document.location = “‘ + link.href + ‘”‘, 100)
}catch(err){}
}
</script>
Once your ID’s are set, save your work, and test your links a few times. Within a day or two, log into your google analytics account to view your outbound links.
