How to Track Outbound Links using Google Analytics

Outbound Links Tracking

For sure you have some outbound links in your website going to other websites for various reasons. We sometimes cite other websites as sources of fun, information, or attribution, etc. You can track which outbound links are clicked by your users by adding a short, simple code in your Google Analytics Tracking code.

Where are you Going?

For sure you’d like to see where your visitors are headed to after they’ve been through your website. Tracking where all your outbound traffic is headed can tell you which outbound links are doing well in your website and which are not. Here’s the code to track all outbound links in your website:

<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', 'outgoing_links', this.href.replace(/^http:///i, '')]);
}
}
}

</script>

What this code does is it gets all your <a> tags (which is what your links are made out of) and appends an onClick function to it. The onClick function will create an Event Category which will be outgoing_links, and the event action will be the URL.

Put this below your Google Analytics Tracking code like this (highlighted in blue):

<!–THIS IS YOUR GOOGLE ANALYTICS TRACKING CODE–>

<script type=”text/javascript”>

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement(‘script’); ga.type = ‘text/javascript'; ga.async = true;
ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js';
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
})();
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', 'outgoing_links', this.href.replace(/^http:///i, '')]);
}
}
}
</script>

<!–END RECORD OUTBOUND LINKS CODE–>

This should track all your outbound links. You can check this at the Content -> Events section when you go to your Google Analytics account.

Google Analytics Events Tracking

You will be able to see something like this:

Events Results
(Click the Image to Enlarge)

Event Category is the name of the event. In this example, it is the name of my outbound links – if you noticed, I customized my Events Categories giving them names like ‘SEO Services SB’, ‘SEO School SB’, ‘Facebook Group SB’, etc. You can also customize your Event Categories by using the onClick function for Google Analytics Events tracking here.

Total Events is the times the Event occurred – in this case, it’s the number of times my outbound link was clicked by a user.

Unique Events is the times the Event occurred for a Unique IP address – in this case, it’s the number of times my outbound link was clicked by a Unique user.

Event Value and Avg Value can be set on Goals Settings. As of now, we will not use these.

Where are You Going?

If you check out my Event Categories, I name my links to my understanding. For example, I append an ‘SB’ label to my sidebar links to see if they are clicked more often than their in-content or image counterparts. For this timeframe, it strongly suggests that my sidebar links are the most clicked links – particularly my SEO Services link and SEO School link.

Most Clicked Links

If you want to check your outbound links without the categories simply click the ‘outbound-article’ Event Category and it should take you to the raw URLs of your outbound links.

Outbound Article

The results should look like this:

Google Analytics Outbound Links

One More Trick

When you’re tracking site-wide outbound links, you can check which pages prove most effective in compelling your users to click-through. Simply add a secondary dimension like this:

Outbound Links Landing Page Dimension

Then filter the results such that it won’t show you anything but your site-wide links (you should manually put a customized onClick Events Tracking code in your Site-wide links to effectively track them.) First, click on the Advanced Filter link:

Google Analytics Advanced Filtering

Then Exclude all non site-wide links (Event Category) in my case it is labeled as an ‘outbound-article’ and an ‘outbound-menu’ :

Advanced Filter for Outbound Links

Your Google Analytics should show you data that looks like this:

Sitewide Links Landing Page

This data tells me on which landing page has any of my site-wide links have been clicked. This is especially helpful if you are testing sidebar ads, banners, links or email signup forms.

Tips for Keeps: Implementing this Google Analytics Outbound Links Tracking code is a piece of cake – paste the code beside your Google Analytics Tracking code to start gathering your outbound user activity data.

From Zero to a Thriving SEO Company, We're sharing everything on our Journey to success!

You'll want to get in. Promise.

We guarantee 100% privacy. Your information will not be shared.

Comments

  1. says

    Sean,

    Just looking at your script briefly, I think I can see a small improvement for you to make.

    The following test is going to mean that HTTPS links aren’t tracked I think:

    a[i].href.match(/^http:///i)

    Easily fixed but thought I’d mention it to improve the reporting!

    Al.

    • says

      Hey Al,
      Where should this code be placed? I don’t see any much difference with your code from this one: && a[i].href.match(/^http:///i)){
      a[i].onclick = function(){

      Can you please clarify?

  2. says

    Sean,

    Sorry I didn’t provide the code to improve it, just the fact I noticed a small bug.

    You’d replace your conditional check before you attach the onclick event with:

    if (a[i].href.indexOf(location.host) == -1 && (a[i].href.match(/^http:///i) || a[i].href.match(/^https:///i)))

    The above now says ‘if the URL location isn’t the current site and the URL begins with HTTP or HTTPS’ – so you get a small improvement in case you were linking out to websites using HTTPS.

    Al.

    • says

      Hey Al,
      Cool! Now it makes sense to me. Thanks for providing the code for this. I’m not really a coder – I’m more of a writer / marketer in our team. Things like this I really have to have someone explain it thoroughly to me.
      Thanks Al

  3. says

    Great info thanks Sean.
    Do you mind showing me how I would record these events as goals in analytics? I’m not sure how with so many category names.
    Thanks!

    Peter

  4. Zion says

    Wow, I didn’t even know that there was a feature like this in google anlytics. I only thought it was ALL ANALTYICS tracking, inbound links and all. LOLZ!

  5. says

    Thanks for your information.
    how to add name to event category I mean the label like you did “SEO Services SB”

    regards
    anwar

    • says

      Hi Anwar,
      That is entered in the category section in your code: onClick=”_gaq.push(['_trackEvent', 'Event Category', 'Event Action', 'Event Label']);”

  6. says

    Thanks for this help! Yours is the only site I’ve come across so far that offered a one-step solution to tracking outbound links.
    I was wondering – using the code exactly as above, would this affect In-Page feature of Analytics? Added it a few days ago & haven’t seen any Events either (though there are some, verified with WP Jetpack stats).

    • says

      Hi Tammy,
      Perhaps there’s a change in the code now? I know that Google Analytics has a new, updated code that they will be implementing soon. In any case, I’ve found a workaround plugin if you’re using WordPress. Just put in Yoast’s Google analytics plugin and it will automatically track outbound links for you. I assume that they do update their code with GA and WordPress at the same time.

  7. Ivana says

    This didn’t work for me, just killed all my traffic :(
    And it seemed like such a good solution.

    Here is how my code looked like after adding your part.
    Can you tell me if it looks good?
    What did I do wrong?

    var _gaq = _gaq || [];
    _gaq.push(['b._setAccount', 'UA-xxxxxxxx-1']);
    _gaq.push(['b._setDomainName', 'example.net']);
    _gaq.push(['b._setAllowLinker', true]);
    _gaq.push(['b._trackPageview']);
    (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript'; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://’ : ‘http://’) + ‘stats.g.doubleclick.net/dc.js';
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
    })();

    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', 'outgoing_links', this.href.replace(/^http:///i, '')]);
    }
    }
    }

    a

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" target=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>