Knowledge |

Tracking external links with Google Tag Manager

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 ({{Click URL Hostname}} == {{Page URL Hostname}}) ? false : true;
}

Note: {{Click URL Hostname}} and {{Page URL Hostname}} 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.