Tracking external links with Google Tag Manager

William Julian-Vicary

Strategy & Innovation Director

Just a heads up, this article assumes the reader has a prior knowledge of Google Tag Manager. If you're unfamiliar with the terminology used below then I'd start with the Google Analytics Academy - it has a great course that introduces the fundamentals of Google Tag Manager.

Tracking external links is a common use case for Google Tag Manager and its built-in auto-events allow for this functionality to be implemented with ease. Generally, there's also little need to provide additional meta information when tracking external link clicks. This means, as long as Google Tag Manager is deployed to your website, that this can be achieved without the involvement of your developers. Although, I would strongly suggest they are given at least a "heads up" of any changes you're making!

Most implementations of external link tracking within Google Tag Manager expects a user to define the expected (i.e non-external) domain within the trigger setup. This has a few failures in our opinion:

  • Requires the user to modify the trigger - failure to do so will result in erroneous events being created in Google Analytics.
  • When a website is in development mode it is likely that the domain will differ and events will be created for every page click.
  • All domains need to be added as exclusions to avoid inappropriate events triggering.

The above example would usually have a Google Tag Manager trigger that looks similar to this:

This has the logic that for each link click the trigger checks that the link click happened on a http protocol and that the hostname of the click does not match the defined hostname.

Our solution works in a very similar manner, however it has one key difference: the logic to determine whether a click has happened on the same hostname is moved to a custom javascript macro. By utilising a macro we are able to identify whether the clicked URL's hostname matches the current page's hostname. If it differs then we're clicking an external link otherwise the link is internal. This macro simply returns true or false based on that logic.

function() {
return ( == ) ? false : true;

Note: and are variables that have been created for this purpose. It is essential that the "Strip 'www'" checkbox for these URLs is consistent among the variables. We prefer this to be ticked - whatever your choice, ensure you are consistent otherwise this will not work as you expect.

The trigger can then be updated to make use of this new macro:

This trigger will now work on any deployment of Google Tag Manager and track external clicks without any modification needed. The only instance where you wouldn't want to track the external clicks will be when you have cross-domain internal links (such as a link to a third party checkout) that you don't want to track - in those cases you will need exceptions (out of scope of this post).

We've created a Google Tag Manager recipe that you can import into your container and get started right away. Click here to download it. If you're unfamiliar with this, Google has a good guide on how this can be achieved.

Related Posts

Extracting Query Parameters in Google Analytics - Free Tool

Auditing your Google Analytics account for query parameters can be tedious but helps keep page reports clear of unwanted row duplication. We talk about the effects of collecting unwanted queries on your reporting and how to fix it. We’ve also built a free tool to make the process easier.

7 months ago

Tracking HubSpot Forms with Google Tag Manager

When a form has been successfully submitted to HubSpot, HubSpot propagates an event that we can listen to and use to trigger marketing tags within Google Tag Manager. Below we create a Custom HTML Tag that listens for a form success, trigger a dataLayer event which can then be used to trigger a marketing tag to push this data into Google Analytics.

8 months ago

How to get Service Provider back in Google Analytics

Service Provider and Network Domain dimensions are no more. Let’s get Service Provider data back online. We’ll show you how to re-enable ISP data using Google Tag Manager and an IP Lookup Service.

8 months ago

Measuring Content Engagement in Google Analytics - Part 3: Social Site Exits

Track when users leave your site to visit your social media pages. Understand when your content is driving positive move to your social assets.

8 months ago

Measuring Content Engagement in Google Analytics - Part 2: Social Share Click Tracking

The use of social sharing widgets is commonplace. But who’s actually tracking interactions with these? Let’s make a count of how many people are clicking these buttons and record a social share rate to see how many readers amplify content on social media.

10 months ago

DoubleClick Floodlight Tags and Google Tag Manager - Lean up Your Containers

Anybody who uses DoubleClick Floodlight knows the slow process of implementing a new tag for each activity for a campaign interaction or conversion. If you’re using Google Tag Manager to add a tag, trigger and maybe some new variables for every new Floodlight – it won’t take long for your container to overfill with almost identical tags. While there’s nothing wrong with this style of implementation, there is another way.

10 months ago