Brian Cray ··· Home > Blog > Javascript, Analytics > Google Analytics hack: Track outbound links in 2 easy steps

Google Analytics hack: Track outbound links in 2 easy steps

Inbound links answer a basic question: How do people find my site? But that's just half the equation. People come and go. Where are they all going? Are people leaving your website because they aren't engaged or because they are following one of your outbound links?

Those outcomes suggest two different things about the effectiveness of your website. In the first case your website isn't effective; in the second case your visitors read enough to at least follow your links.

Outbound links are not tracked by Google Analytics despite their potential value. That is to say without a little programming wizardry and Google Analytics hackery [insert geek smirk here].

Step 1: Programming wizadry with Javascript

The first step is to add some javascript magic to each of your webpages.

if(typeof jQuery == 'function') { /* use jQuery if it exists because it is a more elegant solution */
    jQuery(function () {
        jQuery('a:not([href*="' + document.domain + '"])').click(function () {
            pageTracker._trackPageview('/outgoing/' + jQuery(this).attr('href'));
        });
    });
}
else { /* use regular Javascript if jQuery does not exist */
    window.onload = function () {
        var links = document.getElementsByTagName('a');
        for (var x=0; x < links.length; x++) {
            links[x].onclick = function () {
                var mydomain = new RegExp(document.domain, 'i');
                if(!mydomain.test(this.getAttribute('href'))) {
                    pageTracker._trackPageview('/outgoing/' + this.getAttribute('href'));
                }
            };
        }
    };
}

The above code works by telling Google Analytics to record a page view for every outbound link. As a result, Google sees outbound links as another page view on your website under the subdirectory /outgoing/. So, for example, if someone clicks a link to http://briancray.com/ on your website, Google Analytics will record a page view of /outgoing/http://briancray.com/. See the example screenshot below taken from the Content Overview section of Google Analytics:

/outgoing/http://topfollowfriday.com/ has 9 pageviews

Step 2: Google Analytics hackery to filter outbound links

You may have already realized that the above script introduced a slight problem: Your page views now account for actual page views as well as outbound links. If this doesn't concern you, you can quit reading now and enjoy the script. If this does concern you, here's step–by– screenshots to explain how to filter outbound links in Google Analytics:

Click All Visits

Create New

Drag Page

Change Filter

Name It

Apply Filter

Once you apply your new custom segment in Google Analytics, you will only see traffic details for the outbound links. To filter out the outbound links and see only normal traffic, you would set the "Condition" dropdown to "Does not contain" instead of "Starts with."