Track outbound links with gtag.js

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

To set up Analytics Event tracking, 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 tracking code.

If you have added the gtag.js tracking code to your site, you can copy and paste the script below into your own pages to set up Event tracking for 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 tracks 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 trackOutboundLink = 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="" onclick="trackOutboundLink(''); return false;">Check out</a>

Related resources

Was this article helpful?
How can we improve it?