Set up (web)

Track outbound links

Find out when users click a link to leave your site.

You can customise your Google Analytics tracking code to find out when users click outbound links, or links that take users to a website other than your own.

This article gives you an example of how to set up outbound link tracking. This is a two-step process, and you need to follow both steps complete the process.

You must have Google Analytics account and the web tracking code set up before you can track outbound links. You should have a basic knowledge of HTML and JavaScript or work with a developer to complete the set up.

Step 1: Set up an Event to track outbound links

Event tracking is a way you can track user interactions that aren’t automatically collected by the Google Analytics tracking code snippet, including clicks to outbound links. Learn more about Event tracking.

You can copy and paste the example below into your own pages to set up Event tracking for outbound links. We recommend that you put this script in your page headers, but not within the basic Google Analytics tracking code snippet.

When you set up an Event, you must define values for the Event components. The Event components define how the data appears in your reports. In this example, the Category, Action and Label are defined (in bold). You can use these values, or change them and define your own values. Learn more about Event components or refer to our Developer Guides for more technical information on the Event tracking.

The changes you need to make depend on which tracking code you’re using. See if you have Classic Analytics (ga.js) or Universal Analytics (analytics.js).

This example uses Event tracking for Universal Analytics. If you’re using Classic Analytics, refer to our Developer Guides for more information on how to track outbound links with Events using the ga.js JavaScript library.

<script>
/**
* Function that tracks a click on an outbound link in Google Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label.
*/
var trackOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
     function () {
     document.location = url;
     }
   });
}
</script>

Step 2: Add the onclick attribute to your outbound links

After you have Event tracking set up (Step 1), you must also add (or modify) the onclick attribute to your links. This is how data from a specific link gets sent to Google Analytics.

Use this example as a model for your own links:

<a href="http://www.example.com" onclick=”trackOutboundLink(‘http://www.example.com’); return false;">Check out example.com</a>

Additional resources (for developers)

This example includes the hitCallback field, which tells Google Analytics when the user interaction is complete, and uses the trackOutboundLink() as the JavaScript function. This makes sure that you collect the interaction data before the user leaves your site.

For more information on how this works, refer to the hitCallback reference in our Developer Guides.