How to track outbound links through Google Analytics ?

Updated on September 2, 2017

From years am using Google Analytics for detailed statistics about my website’s traffic and traffic sources. Am always excited to see the “Real Time” monitoring of my website in Google Analytics. Recently i could able to find few advertisers through fiverr for putting their adverts on my website. But unfortunately, i couldn’t find any statistical information of how many clicks those adverts got in Google Analytics ? From a Google Help page i found few things to my surprise : “Links that lead away from the site are not automatically tracked by Google Analytics, you need to manually tag all outbound links you want to track” ! Googling around found few solutions, which i would be sharing among you.

How to track Outbound Links in Google Analytics
How to track Outbound Links in Google Analytics

Solution :

  1. Track all outbound links
  2. Track clicks to particular link
  3. Using Outbound link tracking tool by CueBlocks

To do this we shall use  _trackEvent method to record outbound links through some custom JavaScript code. The below JavaScript code, needs to be put in the <head> section along with the already existing traditional synchronous or the asynchronous tracking code in your website pages.

<script type="text/javascript">
var a = document.getElementsByTagName('a');
for(i = 0; i < a.length; i++){
    if (a[i].href.indexOf(location.host) == -1 && a[i].href.match(/^http:\/\//i)){
           a[i].onclick = function(){
                  _gaq.push(['_trackEvent', 'Outbound Links', this.href.replace(/^http:\/\//i, '')]);
           }
    }
}
</script>

The above code works on monitoring all the <a> tags and comparing with the respective domain. If the domain name is same, then no need to track as it is an Inbound link and if it is not same, then track using _trackEvent for outbound links.

If you have the requirement to track a particular outbound link like in my case “Outbound click on Adverts” only then follow the below procedure which uses  _trackEvent() method in the link’s <a> tag as shown below

<a href="http://www.example.com" onclick="_gaq.push(['_trackEvent', 'Outbound Links', 'Click', 'Advertisement']);>Advertisement</a>

Similarly one more example to track your particular pdf file downloads :

If the download opens in new window :

<a onclick="_gaq.push(['_trackEvent','Download','PDF',this.href]);" href="download.pdf"  target="_blank">PDF Download</a>

If the download opens in current window : You need to add a delay to allow time load for tracking code.

<a onclick="var that=this;_gaq.push(['_trackEvent','Download','PDF',this.href]);setTimeout(function(){location.href=that.href;},400);return false;" href="download.pdf">PDF Download</a>

CueBlocks a company involved in e-commerce design, development and marketing brings you this simple great tool to automatically generate a script which includes to track all outbound links on the page as events in Google Analytics. All you need to do is just enter your Google Analytics Tracking ID and it will generate a script. If you already have the analytics code installed on your website, then remove it and paste the code generated for you via this tool.

CueBlocks Tracking Tool
CueBlocks Tracking Tool

Please note that once you have followed above procedures, it will take some time for the statistics to start showing in your Analytics Account. After a day in your Google Analytics reporting, navigate to : Behaviour -> Events -> Overview and you find the outbound links get tracked as shown highlighted in the below image :

Outbound Links tracking GA
Outbound Links tracking GA

Let us know does this helps you ? Please share in the comments below.

Was this article helpful?

Related Articles

Leave a Comment