How do I track clicks on my referral (outbound) links with Google Analytics

Tracking clicks on your referral links or any other links that lead to another site with Google Analytics is not an automated process. It is still an easy job to do.
What you need to do is manually add a onClick event to all <a> tags for the links you would like to track clicks for. Please follow the steps bellow:

  • Make sure you have Google Analytics tracking code loaded on the pages where your links are.
  • Add the following code after Google Analytics tracking code, before </head> tag. Make sure you replace UA-XXXXX-X  with your own in the script.

This function will delay the outbound click by a fraction of a second. This delay will provide the browser enough time to load the tracking code. It will not be noticed by the user. Without this method, it’s possible that a user can click the outbound link before the tracking code loads, in which case the event will not be recorded.

<script type="text/javascript">
function recordOutboundLink(link, category, action, label) {
    var pageTracker = _gat._getTracker("UA-XXXXX-X");
    pageTracker._trackEvent(category, action, label);
    setTimeout(‘document.location = "’ + link.href + ‘"’, 100)
  } catch(err) {}

Here is a short description of the function’s parameters:

category – The name you supply for the group of objects you want to track. In the example I call it Referral Links since I want to group all referral links in a category and I keep that name for all referral links.

action – A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.In the example I call it Referral Site name since I want to know how many click gets every different site I refer to. The name is different for every site.

label – An optional string to provide additional dimensions to the event data. I call it Referral Product Name since I want to know how many clicks I have for a specific product. I put this link on many pages,  but all clicks will be grouped in my report under the same label.

  • Add calls to that function on all the links you would like to track. Here is an example:
<a href="" onClick="recordOutboundLink(this, ‘Referral Links’, ‘Referral Site name’, ‘Referral Product Name’);return false; ">