28 June 2010 by Published in: Code No comments yet

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.


Want me to build your app / consult for your company / speak at your event? Good news! I'm an iOS developer for hire.

Like this post? Contribute to the coffee fund so I can write more like it.

Comments

Comments are closed.

Powered by WordPress