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

By richardbaxterseo |

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:

content performance screen in Google Analytics

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:

<a onclick="javascript:pageTracker._trackPageview('/outbound/sidebar-logo/www.linkedin.com');" href="http://www.linkedin.com/in/richardbaxterseo"><img src="http://seogadget.co.uk/images/linkedin.gif" alt="linkedin - Richard Baxter" /></a>

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:

<a href="http://seogadget.co.uk/" onClick="pageTracker._trackEvent('Events', 'Exit click', 'SEOgadget');">http://seogadget.co.uk</a>

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.

24 Responses to “How to Count Your Outbound Click Stats with Onclick in Google Analytics”

Leave a comment
  1. Posted July 14, 2008 at 10:38 am | Permalink

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

  2. Posted July 14, 2008 at 10:59 am | Permalink

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

  3. Posted August 7, 2009 at 1:46 am | Permalink

    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. Posted August 7, 2009 at 1:48 am | Permalink

    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. Posted November 27, 2009 at 2:23 pm | Permalink

    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
    Posted December 7, 2009 at 5:28 pm | Permalink

    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. Posted January 1, 2010 at 4:19 pm | Permalink

    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. Posted February 2, 2010 at 9:54 pm | Permalink

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

  9. Posted February 2, 2010 at 10:00 pm | Permalink

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

  10. Posted February 2, 2010 at 10:37 pm | Permalink

    @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. Posted February 2, 2010 at 10:41 pm | Permalink

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

  12. Posted February 2, 2010 at 10:45 pm | Permalink

    @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. Posted February 2, 2010 at 10:47 pm | Permalink

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

  14. Posted February 2, 2010 at 10:54 pm | Permalink

    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. Posted February 2, 2010 at 11:20 pm | Permalink

    @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. Posted February 3, 2010 at 12:43 am | Permalink

    @Leandro @Les

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

    Thanks, Richard

  17. Posted March 4, 2010 at 7:14 pm | Permalink

    Cool, thanks so much for the detailed instructions

  18. bad tempered drive by
    Posted May 28, 2010 at 3:30 am | Permalink

    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

    • Posted June 1, 2010 at 5:02 pm | Permalink

      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…

  19. Posted July 6, 2010 at 8:20 pm | Permalink

    Well, let’s give it a try

  20. Posted July 15, 2010 at 7:56 am | Permalink

    Cool, thank you for information

  21. Posted July 20, 2010 at 10:40 am | Permalink

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

  22. Posted August 16, 2010 at 3:59 pm | Permalink

    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

  23. David
    Posted August 19, 2010 at 8:34 am | Permalink

    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.

Tagged as: ,