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:
– Auto-Event Tracking In GTM 2.0
– Variable Guide For Google Tag Manager
– Trigger Guide For Google Tag Manager
Setting up the Variable and the Trigger
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 Name: Click URL Hostname
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 Trigger
- Step 1: Choose “Click” Event
- Step 2: Choose “Link Click” Trigger Type, and check “Wait for Tags” and “Check Validation”
- Step 3: For “Enable When”, set Page URL matches RegEx .*
- Step 4: For “Fire On”, set 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 matches RegEx .*, 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, such as:
Back To Home
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.