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:
![Clicks just links screenshot - contains http](https://backend.clarity.global/wp-content/uploads/2023/07/TRigger-configuration.png)
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.
![Clicks just links screenshot - starts with http](https://backend.clarity.global/wp-content/uploads/2023/07/Clicks-just-clicks.png)
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.
This post was created in 2017 and some of the content may no longer be relevant.
If you’re interested in learning more about analytics and data, take a look at our services.
Stay up to date
Receive all the latest news, events, and insights on B2B tech, marketing, and communications with Clarity’s free monthly newsletter.
Share this
MORE
INSIGHT
Fearless tactics to achieve your strategic success
As a consultancy, our full-funnel marketing and communications solutions are designed to fearlessly deliver business results across multiple industries and service areas.
![](/_next/image?url=https%3A%2F%2Fbackend.clarity.global%2Fwp-content%2Fuploads%2F2023%2F03%2FScreenshot-2023-03-30-at-16.52.31.png&w=1200&q=75)