# Google Analytics Cross Domain Tracking - Excluding unwanted domains

Tracking multiple websites with Google Analytics is easy. Just add the same tracking code snippet to your websites' pages (or use GTM) and you can see data for two websites in your GA reports - check the Hostname dimension if you don't believe it!

Google Analytics Cross-Domain Tracking comes to the rescue. You need to tell Google Analytics that the customer experience on these two hostnames needs to be consolidated into a single Session and treated accordingly.

#### Cross-Domain Tracking setup checklist

• Make sure your cookieDomain field is set to auto. If you are not sure - just set it to auto and let GA handle it (see Cross-Domain Tracking for subdomains section below).
If you do know what this is for - then I am sure you are using it the right way!
• Make sure you are using the autolinker plugin. This Google Analytics plugin provided by Google adds a few characters to the link to make sure Google Analytics "recognises" and consolidates sessions between websites
• The plugin needs to be explicitly told which domain names to use, for example:
ga('require', 'linker');ga('linker:autoLink', ['www.mybusiness.com','checkout.mybusinessshop.com','shop.mybusiness.com']);
• GTM configuration follows similar logic
• Make sure you list all the hostnames in the Google Analytics Property settings: Referral Exclusion List
• That's it!

Google Analytics Cross-Domain Tracking for subdomains

If you would like to consolidate customer experience on two subdomains, for example: www.mybusiness.com and shop.mybusiness.com, the good news is that it is done automatically for you! You just need to use the recommended Google Analytics code snippet which sets the cookieDomain field to auto. If you have modified the GA code snippet or are using GTM to tag your website, please make sure you manually set the cookieDomain field to auto.

### A little bit more technical spice

Above, I have briefly explained what Google Analytics Cross-Domain Tracking is and how to use it. The implementation is pretty straightforward and widely covered on the Internet. In most cases, you would use the autolinker plugin to work its magic. The core of the Cross-domain Tracking is a parameter and its value added to the destination URL (query string). The parameter may look like this: _ga=1.199239214.1624002396.1440697407. Once the user reaches the second domain, GA code on the page will read the parameter, "recognise" the user and consolidate the session. If the link domain (hostname) matches the pattern specified in the plugin configuration, the autolinker plugin automatically adds this parameter.

ga('linker:autoLink', ['www.mybusiness.com','checkout.mybusinessshop.com','shop.mybusiness.com']);

For those of you who know JavaScript (and I encourage everyone to do this!), there is an array of hostnames which is sent to the autolinker plugin configuration. When a user clicks on the link on the page, the autolinker plugin checks if the link points to one of the hostnames specified in the configuration and, if it does, adds the _ga parameter. Important to note: this parameter is only valid for two minutes, so if anyone copies the link and uses it later, sessions won't be joined in GA.

A little trick here is that hostname matching is done using the substring, so specifying the mybusiness.com would match both www.mybusiness.com and shop.mybusiness.com. Which is convenient - less typing.

### Excluding hostnames from cross-domain tracking

Recently, I had a client with lots of subdomains and requirements, mature enough to implement the Cross-Domain Tracking for all of them except for a few. Data still had to be sent to the same GA property, but user experience on these subdomains was not be consolidated and left as separate sessions. There were 50+subdomains and an obvious solutionwasto list them all explicitly (remembering that autolinker plugin matches hostname by substring) and NOT listing hostnames which need to be excluded on every page. However this solution had lots of drawbacks. Another option was to implement manual linkage or in terms of the Cross-Domain Tracking to decorate links (generate and append _ga parameter to the link URL). The hacker inside me insisted on injecting my code (installing a hook) into the autolinker plugin with a custom hostname matching logic.

But before doing so, I did a little digging, actually read the documentation and paid proper attention. Guess what? I discovered that the autolinker plugin actually expects Regular Expressions and not just a string as a parameter!

All that was left was to create a negative match Regular Expression to tell Google Analytics Cross Domain Tracking NOT to track specific hostname. Consider the Regular Expression below:

^(?!ignore\.)(.*)mybusiness\.com\.au|(mymegabusiness\.com\.au)|(checkout\.mybusinessshop\.com\.au)

ga('linker:autoLink', [/^(?!ignore\.)(.*)mybusiness\.com\.au|(mymegabusiness\.com\.au)|(checkout\.mybusinessshop\.com\.au)/gi, 'destination.com.au');

Works like a charm. Lesson learned - now I pay more attention to the documentation!