Tracking Outbound Link Clicks

It is possible to track outbound link clicks by adding an onclick event to the link. You have to do this manually because you have to make up a label/ fake url to track for it.

UPDATE 2014-01-12

The syntax for tracking outbound links with the new Google Universal Analytics is like this:

<a href="http://www.google.com" onclick="ga('send','pageview','/outbound/google'];return true;">google</a>

If you change to use the new Universal Analytics main tags then you must update any tracking links that you have previously created in your content.

Legacy Information Below

Example for the older synchronous script

Lets say we want to link to google and we want to track in our google analytics when this link is clicked. We can construct the link like this:

<a href="http://www.google.com" onclick="mojoPageTracker._trackPageview('/outbound/google');window.open(this.href,'_self');return false;">google</a>

Note that we need to make up a label for tracking and it must be unique for each link you are tracking. In the above example we are recording the link click as 'outbound/google' and when we see results in google analytics this is the url it will show.

Keep in mind that this only works if javascript is enabled in the web browser, which is true of pretty much all google analytics tracking since it relies on the tracking javascript.

Example for the newer Async script

<a href="http://www.google.com" onclick="_gaq.push(['_trackPageview', '/outbound/google']);window.open(this.href,'_self');return false;">google</a>

Side Effects?

Recently I noticed in my Webmaster Tools reports lots and lots of 404 page not found errors because googlebot is crawling these page tracking links. These are virtual links only for tracking in analytics and the urls only exist in javascript that does the page tracking so they are not really 404s. I was very concerned about this, but after some searching I found Unexpected 404s in the Webmaster tools help, which says we can safely ignore these bogus 404 reports and that they don't impact SEO. Hopefully that is true, but it would be nice if googlebot was smart enough to not tag these as 404 errors. Having hundreds or thousands of these bogus 404 reports makes it more difficult to find actual broken links.

Last Updated 2011-05-28 by Joe Audette