Tracking an outbound link as a goal with Google Analytics
- June 28th, 2010
- Posted in Code
- Write comment
As part of some SEO I’ve been doing, I’ve been wanting to track an outbound link (specifically, to the app store) as a goal in Google Analytics.
The roundabout explanation is that the Google Documentation for tracking outbound links is wrong, because they want you to track it as an event (and events cannot be goals). Here’s the right way:
//<!--Normal GA code in header-->
function recordLink(link,newname)
{
try {
var pageTracker = _gat._getTracker('UA-8841539-3');
pageTracker._trackPageview(newname);
setTimeout('document.location ="' + link.href + '"',100)
} catch(err){window.alert(err)}
}
</script>
Then just format your links like this:
<a href="http://real-url.com" onClick="recordLink(this,'/fake/goal/url');return false;">URL text</a>
This will redirect the user to real-url.com, and log it to GA as http://currentdomain.com/fake/goal/url enabling you to treat it as a goal.
No comments yet.