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.

Comments

  1. Eduardo Cereto

    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.

  2. Eduardo Cereto

    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>

  3. Les

    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!

  4. tom

    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

  5. DSampaolo

    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.

  6. Leandro

    @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)

  7. Leandro

    @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.

  8. Leandro

    @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)

  9. bad tempered drive by

    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

  10. richardbaxterseo

    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…

  11. Newquay

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

  12. Mikejgill1

    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

  13. David

    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.

  14. Rajesh Mergu

    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

  15. Tess

    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!

  16. Tess

    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!

  17. Matt

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

  18. Valentine

    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

  19. mike

    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?