[UA] Measure outbound links with gtag.js

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

To set up Analytics Event measurement, you'll need to be comfortable editing HTML and coding in JavaScript, or have help from an experienced web developer.

Learn more about setting up your basic Google Analytics code setup.

If you use gtag.js for your Google Analytics installation, you can copy and paste the script below into your own pages to measure outbound links.

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", an Action of "click", and a Label = the URL . (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.

* Function that registers 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 getOutboundLink = function(url) {
  gtag('event', 'click', {
    'event_category': 'outbound',
    'event_label': url,
    'transport_type': 'beacon',
    'event_callback': function(){document.location = url;}

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://www.example.com" onclick="getOutboundLink('http://www.example.com'); return false;">Check out example.com</a>

Related resources

Was this helpful?

How can we improve it?
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!

Clear search
Close search
Google apps
Main menu
Search Help Center