How to Count Your Outbound Click Stats with Onclick in Google Analytics
This evening I’ve set up my side bar “Find me on” links to track outbound clicks using a javascript onclick event. I’ve set this up mainly out of a curiousity to find out how much of my traffic exits via my WordPress sidebar, and of course an interest in how this type of outbound link tracking works!
My post builds on a great article (and plugin) from Joost De Valk. His article, Checking your outbound click stats is a fantastic run through of how to use the Google Analytics “content drilldown” report to work out where you’re sending traffic (providing you’re tracking the clicks). If you’ve correctly installed his plugin, your comments, downloads, comment author link, article links and blogroll link clicks are all tracked automatically. Those outbound clicks should look like this:
But what if, like me, you’ve created a bespoke sidebar, that isn’t quite dynamic enough to talk to a plugin like this? Basically, I’ve hard coded my sidebar links and I’d like to be able to track what exit clicks they’re generating.
Tracking your (hardcoded) sidebar link outbound clicks with a javascript onclick event
I’m going to use my “Find me on” links on the right hand side of this blog as the example. In short, I have worked through the HTML in this code and replaced the ordinary link anchor (<a href=) with the onclick event that will trigger the Google Analytics outbound link click tracking:
The first snippet of code is my Linkedin profile link. It’s an ordinary bit of code for an image that carries an outbound link in the HTML anchor.
Below is our next snippet. By using firebug in inspect mode, I grabbed the default onclick code from an outbound link from one of my recent articles and carefully replaced the href= url to the one I want to track. You will notice (If you look at an onclick in another article post on this blog) that I have changed the code “/outbound/article/” to “/outbound/sidebar”. This change will separate out click data coming from my sidebar – and will appear neatly in the content drilldown report above.
Finally, I need to update the first code snippet with this new onlick event. All that needs to be done here is replace the old HTML anchor link with this entire line of code:
This image link will now track just like the rest of the outbound links on my site. Take a look by inspecting the code in my sidebar with firebug. Enjoy! ![]()
Popularity: 10% [?]







4 Comments on “How to Count Your Outbound Click Stats with Onclick in Google Analytics”
Hi Srinivas,
The method mentioned above has a side effect, for each outbound link additional pageviews will count in your analytics account, which is not correct because these pageviews are not for your website, these are for those links which are sending visitors out from your website.
To correct this there is one updated method using Event Tracking which can be used to track outbound link. To enable this follow below steps
1. Set up Event Tracking in your Analytics Tracking code.
This is a simple matter of adding the following line to the tracking code for your pages after the page tracking object is set up:
var pageTracker = _gat._getTracker(‘UA-XXXXX-X’);
pageTracker._trackPageview();
2. Add a JavaScript method in the head of your document to delay the outbound click by a fraction of a second.
This delay will hardly be noticeable by the user, but it will provide the browser more time to load the tracking code. Without this method, it’s possible that a user can click on the outbound link before the tracking code loads, in which case the event will not be recorded. Here’s what the JavaScript code in the section should look like (assuming you will use your own tracking code ID):
function recordOutboundLink(link, category, action) {
try {
var pageTracker=_gat._getTracker(“UA-XXXXX-X”);
pageTracker._trackEvent(category, action);
setTimeout(‘document.location = “‘ + link.href + ‘”‘, 100)
}catch(err){}
}
3. Update your outbound links to call the new function without first following the link. For example, to log every click on a particular link to www.example.com, you would use the _trackEvent() method in the link’s tag:
The example above uses the category label Outbound Links. This is a useful way to categorize all outbound links in the Event Tracking reports. It sets the specific name of the website as the second parameter in the call. With this structure in place, you could then see Outbound Links as one of the event categories and drill down to see which particular outbound links are the most popular. Be sure to use return false for the onClick handler, because without that statement the browser will follow the link before the recordOutboundLink method has a chance to execute.
Refer below URL for more clarification
http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55527
Lets see the Tracking outbound links http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=72712 We have implemented this method.
Srinivas
I am agree with you.
I I have also implemented the old method of tracking outbound link. But if you implement the new method then i am sure you will get the correct view of outbound links.
ur phone number is not working sir