Google Analytics - How to manually Track Clicks on Outbound Links

You can use Google Analytics to track clicks on links that lead away from your site. Because links that lead away from your site are not automatically tracked, you will need to manually tag all outbound links you want to track. To do this, you'll add some JavaScript customizations to your page and to the links you want to track.

  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 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):
    <script type="text/javascript">
    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){}
    }
    </script>
    
  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:
    <a href="http://www.example.com" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">Trackable Link</a>
    

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.

For more information on using event tracking, see the Event Tracking Guide on Google Code.

Article Reference URL:
http://www.google.com/support/googleanalytics/bin/answer.py?answer=55527&cbid=-wlhzojb6oodb&src=cb&lev=index

3 comments for Google Analytics - How to manually Track Clicks on Outbound Links

Elizabeth's picture

Thanks for writing this up!...

Thanks for writing this up!

Tom Nygaard's picture

How do you get into the java...

How do you get into the java script or even java at all on a computer
I want to add "defer" as you instructed to fix explorer from locking but what are the exact steps starting from 'start'
on a pc

mike's picture

I need to use tracking on...

I need to use tracking on outbound links. However, I don’t know JavaScript that well. Is there any possible way you can re-post this article explaining in step by step detail on what code I should use and where I should add it to my pages?

For example on your steps:
1) I don’t know where to put this code.
2) I understand to put this in the head area of the web page but I don’t know what “assuming you will use your own tracking code ID” means
3) I don’t understand how to set this up. I don’t know what “Be sure to return false for the onClick handler” means.

My goal is to track a customer’s link or links and be able to have the customer go into Google Analytics and and see a detailed report by month of how many people are clicking on their links, on my site.

Any suggestions? Or are there any help forums you know of or people who are good offering detailed instructions to make this work?

Sincerely,

Mike

Also, I noticed that this blog entry is copied word for word here: http://www.anythingown.com/blog/index.php/2010/05/24/manually-track-clic....

(I'm not sure why it's copied on the other site).

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.