NOTE 30 Oct 2019: I’ve published a new article on outbound link tracking in Google Tag Manager, which makes the whole process much, MUCH simpler. I strongly recommend you read that article instead.
Tracking outbound links is important for many. Identifying the exit paths is almost as important as tracking entrances. In this simple #GTMtips post, I’ll show you how to track outbound links with a simple Trigger + Auto-Event Variable combination in the new Google Tag Manager interface.
For more information about triggers, variables, and auto-event tracking, here are some of my previous articles on these topics:
The Simmer Newsletter
Follow this link to subscribe to the Simmer Newsletter! Stay up-to-date with the latest content from Simo Ahava and the Simmer online course platform.
Setting up the Variable and the Trigger
Tracking outbound links hinges around two things: a Variable which captures the hostname of the clicked link, and a Trigger, which fires your Event Tag when the hostname is not your domain.
To set up the Variable, create a new Auto-Event Variable, and call it Click URL Hostname. You can choose whatever name you want, of course, but this mimics the pattern of the existing Built-In Variables.
The Variable should have the following settings:
Type: Auto-Event Variable
Variable Type: Element URL
Component Type: Host Name
If you want, you can choose to “Strip www.”, meaning the Variable will return the same, www-less value for both www.mydomain.com and mydomain.com.
For the Trigger, you’ll need to create a new Click Trigger which has a filter for your site’s hostname.
Create new Just Links trigger
Check “Wait for Tags” and “Check Validation”
For the “Enable this trigger when…” condition, set Page URL contains /
Select the radio button for Some Link Clicks
Set the last condition to: Click URL Hostname does not contain mydomain.com
Substitute mydomain.com with your own domain. You can choose another operator, such as RegEx matching, or the exact match “does not equal” operator, depending on how accurate you want the pattern match to be.
To finish off the Trigger, check my Auto-Event Tracking guide if the options confuse you. The most difficult bit of the new Trigger-based listener is the “Enable When” setting, which determines when the Trigger is actively listening for link clicks. Just add a blanket match to this: Page URL contains /, since it’s quite likely that you want to track link clicks on all your site’s pages, right?
Finally, to make your Event tag fire with this new Trigger, just add it to the Tag:
If you’re wondering how this Trigger works with links that have relative targets, don’t worry. The Trigger works for these as well, since it doesn’t access the href attribute of the A element. Rather, it grabs the value from the DOM property named
href, which will always have a fully-formed URL value with the hostname and all.