#GTMTips: Auto Link Domains With Regex

Use Google Tag Manager to add load listeners to your asynchronously loading script elements. The load listener will inform GTM once the script has completely loaded, helping you avoid race conditions.

Update 5 March 2019 due to GTM not supporting negative lookbehinds any more.

Google Tag Manager makes it fairly easy to do cross-domain tracking. Basically, you list the hostnames you want to automatically decorate with linker parameters in the Auto-Link Domains field of your Page View tag, and that takes care of decorating the URLs with the necessary parameter. It’s dead easy, even if there are a bunch of traps you need to watch out for (see my post on troubleshooting cross-domain tracking issues).

However, for some reason, GTM doesn’t support regular expressions in the Auto-Link Domains field. The autoLink method supports both strings and regular expression literals, but GTM only uses the first. Hopefully, at some point this will be fixed and this article will become obsolete. The reason we want regular expressions is to be able to do things like negative lookbehinds, where we specify that a subset of domains of any given hostname be excluded from decoration. In the image below, you can see an example of this. The regular expression specifies that all other domains except www.simoahava.com should be decorated with cross-domain parameters.

To make this work in GTM of today, we’ll use the amazing customTask feature (see here for all my customTask solutions).

The trick is to use a Custom JavaScript variable as the value of the customTask field. This is what the variable looks like:

function() {
  return function(model) {
    var domainList = [
      /^(?!dev)[^.]*\.?hostname\.com$/,
	  'somedomain.com',
	  'www.domain.com',
	  /^example\.com$/
    ];
    var ga = window[window['GoogleAnalyticsObject']];
    var name = model.get('name');
    ga(name + '.require', 'linker');
    ga(name + '.linker:autoLink', domainList);
  };
}

In the domainList array, you need to add all the domains you want to automatically link separating each value with a comma. You can specify both strings (wrap them in quotes) and regular expression literals. When you use strings, the auto-linker uses an open-ended pattern match. Thus if you have 'example.com' as one member of the array, GTM will decorate example.com and all its subdomains.

Regular expressions give you the power of zero-length assertions such as negative and positive lookbehinds. This makes it easy to match, for example, all subdomains of example.com except for test.example.com.

/^(?!test)[^.]*\.?example\.com$/

This regular expression reads out as: match any string that doesn’t start with test, but contains example.com or any subdomain other than test.example.com. The clumsy positive lookahead is necessary because GTM doesn’t currently (as of March 2019) support negative lookbehinds.

The rest of the variable basically invokes the Universal Analytics tracker object used in the current hit, loads the linker plugin, and then decorates the domain list with cross-domain parameters.

All you then have to do is add this variable to the value of the customTask field in your Page View tag’s Fields to set.

This simple fix will make auto-linking domains for cross-domain tracking purposes much more flexible with Google Tag Manager. But, as I mention in the beginning of this article, I really wish that the Auto-Link Domains field in the tag settings will soon support regular expressions, just like the linker plugin regularly does!