27 Jan 2016
Reading time: 4 mins.

Google Analytics filters for cross-domain tracking

We know that in order to improve on-page SEO for our website, and to ensure that Google does not consider a domain with and without www to be a duplicate, the best approach is to use a 301 redirect (and to do so from the server), or at least use the rel=canonical. However, sometimes neither option is possible due to technical limitations, a lack of time or some other reason. In Google Analytics, not redirecting the domain with and without www will have the effect of reducing the analysis capacity via the hostname dimension, because each domain will be considered separately:

We do not normally pay attention to this aspect, because, by default, what is shown in Google Analytics’ Behavior Reports / Content / Page is the URI dimension (i.e. the part of the URL that follows immediately after the domain or hostname):

As a result, the page views for both URLs are combined, as they both share a common URI. At first glance, this is fine, and perfectly valid when we only have one domain in the Google Analytics property we are analyzing.

But if we include several hostnames in cross domain tracking, we will need more actions to see contents correctly at reports.


Filters for tracking multiple domains and subdomains in the same Google Analytics web property

When we track domain and subdomain/s or multiple domains in the same property, it is not enough to simply view content broken down by URI. We need to view the full URL (including hostname), as otherwise the data will be incoherent (e.g. visits to pages with identical URIs, such as index, contact, etc. will be lumped together, even though they are in different domains). Therefore, we need to use the following:

Google Analytics filter for viewing the full URL

Google Analytics filter for viewing the full URL

Source: Set up cross domain tracking


  1. In Field A → Extract A ($A1) we obtain the hostname, and in Field B → Extract B ($B1) we obtain the URI value.
  2. Send results to → Constructor: URI >> $A1$B1.

This way, we unite the two parts of the URL and display it in the URI of the reports.

Important: by using this filter we overwrite the URI with the full URL, in all the reports in which it is displayed. This step cannot be undone. I recommend doing it at a test view first and checking it in the reports in real time, in order to ensure the results are as expected. Now is when we see the true importance of the hostname dimension, because if the domain with and without www is not redirected correctly from the server, we will find that these two URLs are displayed on different lines when they should be one and the same, which in turn makes analysis more difficult.

To prevent this from happening, unify the hostname with and without www and make sure Google Analytics combines the data for each, before applying the filter indicated above we must first apply another filter (see below). Thanks are due to my colleague and Aukera web developer Aitor García, who created it and provided the explanation.

Google Analytics filter for unifying the hostname with and without www

Filter for unifying hostname with and without www


  1. In Field A -> Extract A ($A1)($A2) we obtain the hostname, although we divide it into two parts:
    • $A1 → ˄(www.)? → We obtain the www and the dot (if they form part of the domain).
    • $A2 → (.*) → We obtain the rest of the domain name.
  2. Send results to → Constructor: Hostname >> $A2.

This way, we are only left with the second part of the domain, i.e. the part that comes after the www., and therefore it does not matter to us whether it includes a www. or not.

It is important to remember that when applying the filters, the second one (i.e. the one to remove the www.) must be applied before the first (i.e. the one to view the full URL). If they are not applied in this order, the results will not make sense.

Filters order


Welcome to the exciting world of regular expressions in Google Analytics!

If you have several interrelated websites and you want to track them and measure them jointly using Google Analytics, you can find out how in this post on Cross-domain tracking in Google Analytics using Tag Manager.