Surf the Dream A discourse of links and articles from Justin Avery

How to track external links on google analytics

Last Friday I set up a single page website for Facebook Influence members to keep track of all the tips, tricks and resources available on the site.

I you haven’t already checked out the site, the basic function is that it lists a series of resources in thumbnail form with a title and description associated with them. Each resource is tagged with one or more categories for sorting, and these categories appear as navigation at the top of the page. Clicking on one of the categories re-orders the list of resources to show only those items.

To get an idea of the most popular categories I set up Event Tracking on the top navigation elements…. and out of interest it seems most people are looking for inspiration.

I also wanted to understand which of the resources were being clicked on the most, but unfortunately these resources are all external outbound links and therefore are not captured by regular analytics. I applied the same event tracking to these elements but found out that because they were outbound there was not enough time to register the click via the conventional Event Tracking method in Google.

Tracking External links with google analytic events

This page is where I got started working out how to track these click events

Step One

Include the following code after your Google Analytics code inside the </head> (only if you’re using the Asynchronous Code)
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
_gat._getTrackerByName()._trackEvent(category, action);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>

This adds a 100 millisecond delay to the click function which gives the analytics enough time to register the external link has been clicked before the browser reloads.

Step Two

Step two is really a step 1.2.  I found that when I implemented my usual tracking code (category, action, opt_label, opt_value) I wasn’t getting any results back from the test clicks.

If you want to use those additional features you need to make a basic adjustment to the code above and include these options in the function;
<script type="text/javascript">
function recordOutboundLink(link, category, action, opt_label, opt_value) {
_gat._getTrackerByName()._trackEvent(category, action, opt_label, opt_value);
setTimeout('document.location = "' + link.href + '"', 100);
}
</script>

Step Three

Now that you have the function in place you need to update the links for all of your external resources and apply an onclick event.  This is when running a CMS makes it much easier to get things done.

<a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com', <youropt_label>, <youropt_value>);return false;">

For me this was

<a href="http://www.noupe.com/showcases/40-great-examples-of-facebook-fan-pages.html" onclick="recordOutboundLink(this, 'Outbound Links', 'Noupe – Awesome FB pages', 'http://www.noupe.com/showcases/40-great-examples-of-facebook-fan-pages.html', 1);return false;" >

Conclusion

As you can see it only takes a few lines of code to get a much better idea about how people are using your website.  This data provides an excellent insight into your cusotmers movements and trends and means that you can plan your content to go along with what your customers trends are at any time.

As always, if you are looking to get more out of your website through understading the usage patterns a little better get in touch with me to have a chat.

  • Many thanks for sharing such an informative blog. Waiting for more…

  • Very Helpful Post, I am setting up Event Tracking to track clicks on my client website. But my client need one more thing is he need information like to have goals if some one clicks the links one goal has to be recorded. If you could help much appreciable.

    Thanks,
    Sudheer

  • David

    Hi

    Thanks for the article. Very helpful.

    I was wondering if you could let me know where I can see this stat in Google Analytics now that I have followed your instructions above?

    Cheers
    David

    • Sorry David, this got lost with a range of spam.

      You can find the statistics under Content -> Events.

  • Hi Justin!

    I have set it up exactly in this way on one of my clients web sites, but it doesn’t seem to work.

    He has some affiliate links set up and that affiliate program tracks clicks.

    But for some reason, I don’t get the same clicks in my Analytics. I get much less. I have the same link on several places on the website. So it basically has the same event tracking on all those places. Do you think that might be the problem? Or do you think it has to do with the fact that the affiliate links also use some kind of javascript to check the links as well?

    What do you think?

    Happy for any thoughts on this problem. Need it to work, because he doesn’t want to use affiliate anymore, but would like to be able to track clicks as leads to other costumers.

Surf the Dream is a blog that has been running since the mid 2000's when it started on BlogSpot. Over the years it's been rebranded as justinavery.me (now my resume) and JaveryDesign.com (which now redirects back to this site).

I offer consultation services through Simple Things, produce a range of educational pocket notebooks, write about the Universe and run a responsive design and a RWD Weekly Newsletter