How to Count Your Outbound Click Stats with Onclick in Google Analytics

Updated May 2010

This is an old post referencing a version of this blog that no longer exists. No matter though – thanks to a number of requests I’ve added the actual code snippet (copy / paste) for outbound click tracking and added a section on using event tracking too. Scroll down to the bottom to get the code snippets or read on!

Counting Your Outbound Clicks with Onclick in GA

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:

code example - ordinary html anchor image link

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.

code example: java onclick outbound link click tracking

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:

Onclick tracking snippet intergrated with old HTML link anchor

Here’s the copy / paste code snippet:

Click tracking with Events in Google Analytics

Event tracking was actually meant for tracking where a pageload may not occur, but it’s useful for nearly anything where an action occurs on the site. It makes for a pretty handy way to track any kind of click though – largely thanks to Google analytics categorisation system for types of event. Here, in the event tracking section for Google Analytics you can see categories, actions and labels.

event tracking in Google Analytics

Events are categorised and assigned actions and labels in the JavaScript. This simple example shows event tracking in action on a “post to Facebook” action:

The category in this example is “events”, the action, “exit click” and the label “SEOgadget”.

Categorisation in event tracking is enormously powerful as soon as you get a good handle on it – for more information check out this resource over at Google code.



Stay Updated: Sign Up for Webinar & New Blog Alerts

36 thoughts on “How to Count Your Outbound Click Stats with Onclick in Google Analytics

  1. Good stuff! Guess you don’t mind if I reference this from my plugin page?

  2. Hi Joost, thanks for dropping by! I’d be honoured if you referenced this post from your plugin page!

  3. It would be better if you used an Event to track this instead of a Pageview, it goes like this for an event

    This way you can better track where you’re users went and from where they clicked the external link. Plus it won’t mess you’re usual pageviews.

  4. Oops my code was stripped (damn you WordPress), hopefully it will get through now.

    <a href=’http://externalsite.com.br’ onClick=’pageTracker._trackEvent(‘outbound’,this.href,location.href)’ ><img src=’myimage.jpg’/></a>

  5. Les says:

    This is awesome. I have been fiddling with this for an hour and cannot get the code to work. I had forgotten that Yoast’s plugin tracked these events. Then I realized that I was hardcoding in the sidebar and things wouldn’t track properly anyways!

    Super duper!

  6. tom says:

    Hi Guys, anybody know if there is a way to stop outbound links diluting your overall number of uniques/pv’s etc. I’m experimenting with adding this in…any help appreciated. Tom

  7. DSampaolo says:

    Hey Tom, your answer has been given by Eduardo Cereto: use Events instead of Pageviews (code to be found in the comments). Thank you all for this very helpful post and comments.

  8. Leandro says:

    Hello, Any idea on how to track clicks on swf flash files? Maybe a plugin for WP would help too. Tks

  9. Les says:

    @Leandro: This article may help you with tracking Flash in GA: http://www.webfuel.ca/qct

  10. Leandro says:

    @Les from what I understood I’d use that to build the flash file. In my case it’s given, I just want to know how many people click on it (an ad on my blog)

  11. Les says:

    @Leandro. Can you please post a link to what you want to track?

  12. Leandro says:

    @Les it’s the banner on the bottom of my site (link to my site on my nick here) I’m negotiating an important one to the header, an tracking it would be very nice.

  13. Leandro says:

    @Les I don’t think the absolute path to the file is important, is it?

  14. Les says:

    Simple enough: http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55526

    Read the entire article.

    For a Flash Banner Ad — See below:

    The equivalent code for a Flash banner is provided below: on(release) { getURL(“javascript: pageTracker._trackPageview (‘/bannerads/advertisername/bannername’);”); getURL(“http://http://www.feiradelogistica.com/”); }

  15. Leandro says:

    @Les Thanks a million! I’ll test it in a test site and as soon as I check it works OK I’ll put on the main site as well. BTW, I suppose you’re also based in Canada (although I run a brazilian site)

  16. @Leandro @Les

    Thanks guys for clarifying that (very useful) addition to this post. Your comments are much appreciated!

    Thanks, Richard

  17. Cool, thanks so much for the detailed instructions

  18. bad tempered drive by says:

    yes your instructions are clear and helpful, but you can’t copy the code because of the image – I want to be able to copy the code

  19. Hi there – I’ve updated the post with copy / paste code and I’m planning to rewrite the post soon. I’ve also included an updated run through of event tracking – which may be a better solution in some cases.

    Regardless, I can’t let you know about these wonderful changes because you didn’t leave your email address…

  20. Abdul hakeem says:

    Well, let’s give it a try

  21. Divland says:

    Cool, thank you for information

  22. Newquay says:

    Thanks for the Google Events information. This information can be understood easily and quickly – better then Google Analytics Help!

  23. Mikejgill1 says:

    Hi there, this may be completely irrelevant but I was wondering if anybody knew of any entity one can use to count out bound links on another’s website.. Would be great if anyone did. Thanks. Mike

  24. David says:

    I have added the onclick javascript code to track affiliate links on my site. When I look at the entrance keywords for tracked links in the content drilldown report, does this show the keywords the visitors used to land on the site? I assume it must, because the affiliate links I am tracking are not landing pages which visitors can find in the search engines. Some confirmation would be nice.

  25. daddydesign says:

    where in the backend of analytics would you see the result?

  26. Sanchi says:

    I tried it but not working for me . Do i have to add any other code too .

  27. Rajesh Mergu says:

    hi richard, i have a question for my onclick= situation, my website contact form, visitors have to fill in this contact details and there is send button with this code:

    i want to track how many times this send button is clicked by visitors, i am looking for event tracking rather than pageview tracking, can you please suggest any solution for my situation. cheers, Raj

  28. Simon Cooper says:

    Quick question. Where do I find the code to edit a sidebar them widget, not a core WP widget.

  29. Tess says:

    I can’t seem to get the onclick to work on my site. Either putting it in the code makes everything go all wonky, or if it doesn’t mess up the linking, GA doesn’t pick up the event category, action or label. I’m trying to have it up so I can track the clicks on links of advertisers (and/or potential advertisers) but it doesn’t seem to work. The base of my website is wordpress.org, is that the problem? It should work, right? Thanks for your help!

  30. Tess says:

    Sorry, I forgot to add this the last time. Here is a testing page I’m working on to see if the onclick works. If you see anything wrong in the source code please tell me! Thanks!

  31. Tess says:

    And I keep forgetting to ADD the page itself. Wow. http://www.hardciderinternational.com/?page_id=555

  32. Matt says:

    For event tracking to you need to add additional Google code to the anyaltics tracking code to get it to work?

  33. Swati says:

    hi I can’t see the outbound links…where should I search for them????

  34. Valentine says:

    Hi Guys, I need your help seriousluy and urgently. I’m creating a WEB Application using J2EE, now I want my application to send an email to the user after registration(fine it does that part),But the major thing that I need now is to know if the link sent in the email is clicked. If the lik is clicked, then something needs to be done in my java application.

    I’ve tried the goo.gl url shortner but I just can’t figure out how to make use of it in my coding.

    Please help Thanks

  35. Thanks for the tip – it’s very useful!

  36. mike says:

    Hi Richard,

    I have tried to implement what you teach above but getting no results. Any chances you take a second away from your super busy schedule to help a dude out. pls pls?

Comments are closed.