Sunday, February 5th, 2012

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

Published on April 28, 2010 by   ·   7 Comments

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

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: 16% [?]

Share

Readers Comments (7)

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

  2. admin says:

    Lets see the Tracking outbound links http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=72712 We have implemented this method.

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

  4. koti reddy says:

    ur phone number is not working sir

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

  6. win her back says:

    Hi! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Many thanks for sharing!

  7. Swissgear backpack says:

    Didn’t know the forum rules alolewd such brilliant posts. Real brain power on display. Thanks for that awensr! SwissGer Backpacks: Take 30%-50% off and enjoy FREE Shipping over $100, Ends 8/21/2011 @11PM PST




Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

RSS SiliconIndia News

  • 11 Powerful Women of Silicon Valley's Best 100
    Here are the eleven women who made it to the best 100 of Silicon Valley […]
  • Want to Have Sex on First Date? Go for an Android User
    Do you want to fall in love or seduce someone a little faster, then better throw away your iPhone's and BlackBerry's and get a brand new Android. […]
  • Pakistani Child Prodigy's Stamp Issued
    Pakistan's postal department Thursday issued a commemorative postage stamp as a tribute to Arfa Karim Randhawa, who became the world's youngest Microsoft Certified Professional. […]
  • In One Year-Indian Facebook Users Double
    Popular social networking site Facebook finds India a major growth market, where the number of users have more than doubled in the past one year, as against near-zero penetration in the neighboring China. […]
  • 3 IITs to make World's Cheapest Aakash-Cheaper
    The government plans to involve three more IITs in the world's cheapest Aakash tablet PC project to make it more indigenous and further lower its price. […]
  • Mark Zuckerberg's Base Salary to Dip to $1 at Facebook
    Mark Zuckerberg may be founder and CEO of Facebook, but it is the second-in-command and Chief Operating Officer Sheryl Sandberg who gets a fatter salary package at the IPO-bound social networking giant. […]
  • Where are First 20 Facebook Employees?
    Employees who joined Facebook as a start up in 2004 have either remained committed to the company till date and become millionaires, or moved on to start their own start ups, or joined successful startups like Twitter or are missing from the radar. […]
  • The Forgotten Hero Behind Facebook IPO
    Meet the prime mover behind Facebook's Historical IPO, who never appeared on the spot light. […]
  • Six Things to Know about the 'Facebook IPO' Tour
    Market, investors and rivals waits to watch what makes Mark Zuckerberg's social network go public, so let's know the about some questions which are popping from all sides about this eight year old tech giant. […]
  • Facebook's Road Trip to IPO
    'Facebook' started from a Harvard room and entered Silicon Valley in just eight years and now filled the initial public offering (IPO) for $100bn. Let?s have look at Facebook?s path to know what all happened in eight years. […]

Follow me

Partner Sites

Categories

Meta

Google Analytics Alternative