Capture outbound links

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

Note: This article applies only to sites tagged with the analytics.js snippet. (It does not apply to sites tagged with gtag.js.)

You can use Analytics Events to capture clicks on links that take users to a website other than your own.

In this article:

Google Tag Manager

If you use Google Tag Manager to manage your Analytics installation, follow the instructions in Analytics Events to implement events.

Add the tag directly to your site

If you have added the Analytics tag directly to your site, you can copy and paste the script below into your own pages to set up Event measurement for outbound links. You should add this script in your page header, but not within the basic Analytics tag.

If you copy and paste this script exactly as it appears here, your outbound clicks will appear in your Analytics Events reports with a Category of "outbound" and an Action of "click". (In the snippet, these are shown in bold.) You can use these values, or change them and define your own values. Learn more about Event components.

This example assumes that you are using the analytics.js tag. See if you are using Classic Analytics (ga.js) or Universal Analytics (analytics.js).

<script>
/**
* Function that captures a click on an outbound link in Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label. Setting the transport method to 'beacon' lets the hit be sent
* using 'navigator.sendBeacon' in browser that support it.
*/
var captureOutboundLink = function(url) {
   ga('send', 'event', 'outbound', 'click', url, {
     'transport': 'beacon',
     'hitCallback': function(){document.location = url;}
   });
}
</script>

You'll also need to add (or modify) the onclick attribute to your links. Use this example as a model for your own links:

<a href="http://example.com" onclick="captureOutboundLink('http://example.com'); return false;">Check out example.com</a>

Related resources

google-developers-svg

gtm-logo-sm-svg

Analytics Events

 

Was this helpful?

How can we improve it?
true
Choose your own learning path

Check out google.com/analytics/learn, a new resource to help you get the most out of Google Analytics 4. The new website includes videos, articles, and guided flows, and provides links to the Google Analytics Discord, Blog, YouTube channel, and GitHub repository.

Start learning today!

Search
Clear search
Close search
Google apps
Main menu
9129744736841478271
true
Search Help Center
true
true
true
true
true
69256
true
false