How to Set Up Subdomain Tracking in GTM and Google Analytics:
How to Set Up Subdomain Tracking in GTM and Google Analytics:
Subdomain tracking is an often-forgotten aspect of Google Analytics tracking because some users assume that it happens automatically, and others confuse it with cross-domain tracking. As many websites use subdomains to organise separate functions of their sites it’s an important element of tracking to configure correctly.
What is a subdomain?
A subdomain is an additional part of your main or root domain. The subdomain is shown as a prefix to your main domain. For example, dev.examplesite.com is a subdomain of examplesite.com. Subdomains are often used as staging sites to the main domain and so many large sites have a series of subdomains such as DEV, SIT and UAT to ensure changes can be fully tested before they go into the live environment.
However, subdomains also help organise different parts of a website, such as a blog, an ecommerce store or a logged in area of a site. This might be because separate teams manage each and this fits in with the organisation’s structure. Ecommerce stores are also more complex and need greater security than a blog or a brochure ware site.
These are all subdomains of the site examplesite.com:
How does Google treat subdomains?
Google and other search engines treat subdomains as separate websites. There are few clear advantages from an SEO perspective to creating subdomains because ‘link juice’ from backlinks to your root domain are not shared with your subdomains. It may be beneficial to create subdomains if you need each site to rank for different keywords, target a geographical location or language or target a different market.
What about cross-domain tracking?
In Google Analytics, tracking across subdomains does not require cross-domain tracking. Domains are entirely different websites with URLs that have no connection to each other. These sites may use some of the same words, but they are all unique domains.
If you have sites or content (e.g. via an embedded application or iframe) held on separate root domains, you need cross-domain tracking to accurately measure users going between the domains. That’s because Google Analytics will begin a new session when a visitor lands on different domain and the cookie value will be unique to that site. Please see my guide to cross-domain tracking in Google Analytics for step-by-step instructions.
For subdomain tracking in Google Analytics there are six steps to undertake to ensure your data is accurately capturing users going between subdomains.
1. Set Cookie Domain to Auto in GTM
If you are not already using Google Tag Manager for Google Analytics, learn how to migrate to Google Tag Manager using my step-by-step guide. As many organisations treat each subdomain as a separate site it’s normal practice to have individual GTM containers for each subdomain. However, this does not prevent you implementing subdomain tracking and measuring user behaviour from one subdomain to another.
Go to your GTM container for each subdomain. Select ‘Variables’ and go to your GA Settings variable. The Settings variable allows you to automatically apply specific settings to all your tags. Here you should check that the Cookie Domain is set to ‘auto’. Repeat this for all your subdomain containers.
This will help ensure the cookie value for the domain is identical across all subdomains. We can check if this is the case later.
2. Send Data to the Same Google Analytics Property for all Subdomains:
To track users across multiple subdomains you must configure all GTM containers to send their data to the same Google Analytics property. This is best done by going to each GTM container and creating a ‘GA Tracking ID’ variable as follows:
- Variable Type: Lookup Table
- Input Variable: Page Hostname
- + Add Row:
- Input: domain URL (ignoring https://www.)
- Output: GA Property ID
- Input: subdomain A URL
- Output: GA Property ID
Repeat this for all your subdomains and ensure you use the same GA Property ID as the ‘Output’ for every row. You can of course use a separate GA property for say your staging sites as you wouldn’t want internal testing to be sent to the same property as you use for your live subdomain. For good measure I normally click ‘Set Default Value’ to the same ID as the main domain.
If you don’t already use a GA Tracking ID variable you will need to go to your GA Settings variable. Simply replace the GA Tracking ID with the new GA Tracking ID variable you have just created. Save the variable and test that it works by going to ‘Preview’ mode in each container. You will need to test each subdomain and check the data is going to the ‘Realtime’ unfiltered view of your GA Property.
Once you are happy all the data is going to the same GA property you can publish each container.
3. Add Domain to Referral Exclusion List
Now go to the Google Analytics property where all your data is going to be collected. Select the Admin icon and under the ‘Property’ column select Tracking Info > Referral Exclusion List > +ADD REFERRAL EXCLUSION. Enter the main domain name here as Google uses this as a ‘contains’ filter to exclude the domain from referrer sites. This means you don’t need to include ‘www’ or subdomains in the referral exclusion list.
4. Prepend hostname to page path:
The danger with sending data from different websites to the same GA property is that some pages from different subdomains may be wrongly merged or that it may not be clear which site they come from. To avoid this happening, we need to prepend the full URL to each page path. We can do this by configuring a filter in GA.
This is done at the view level of GA and so go to Admin and in the View column choose Filters. Then select + ADD FILTER and use the settings below:
- Filter Type: Custom > Advanced
- Field A -> Extract A: Hostname: Enter: (.*)
- Field B -> Extract B: Request URL: Enter: (.*)
- Output To -> Constructor: Request URL: $A1$B1
- Check Field A Required
- Check Override Output Field
5. Check the cookie value is the same for all subdomains:
For subdomain tracking to work it is important to check that the cookie value is the same across all subdomains. It is possible to set a cookie which all subdomains will be able to access. For example, if the cookie is set by .examplesite.com all subdomains of examplesite.com will be able to access it.
However, if the cookie was set by blog.examplesite.com, then only dev.blog.examplesite.com or any subdomain at a lower level than blog.examplesite.com, will be able to access the _ga cookie. This would mean, for example, that www.examplesite.com and other subdomains at the same level wouldn’t be able to access the cookie.
If you are using GTM you should already have set your GA Settings variable cookieDomain to ‘auto’. This will prevent a cookie being set by an individual subdomain which would result in a new session being started when a user goes between subdomains at the same level.
To see the cookie value is set by the correct subdomain, go to each website and open Google’s developer tool. You can do this by pressing the F12 key on your keyboard or right-click on the website page and select ‘Inspect’. Go to Application > Cookies > Subdomain > _ga.
If you hover over the domain name, this will display the value and the full domain setting the cookie. Using my example, if it showed .examplesite.com then all subdomains of examplesite.com will be able to access the cookie. If it shows another subdomain you will need to check your GTM or GA configuration.
6. Create views for each subdomain:
Imagine you have data from all your subdomains in a single GA view and then you get a request to analyse data for a single subdomain. It will be much easier if you have a view only with data for that subdomain. This is good practice and so let’s create a separate view in GA for each subdomain.
Go to GA and select an appropriate view (e.g. Reporting View). Then go to Admin > View Settings > Copy View. Name the new view after the subdomain you want to focus on here and then go to Filters > Create new Filter.
Follow the instructions below to set the hostname of the subdomain you want this view to only show data for.
- Filter Type: Predefined – Include only > traffic to the hostname > that are equal to
- Hostname > Enter the appropriate hostname
- Click ‘verify this filter’ to check it is working and ‘Save’
Repeat this process for each subdomain so that you will have a separate view for each site. You will still want to retain a reporting view with data from all subdomains, but this will make it easier to analyse data for individual subdomains when required.
There is a lot of focus on cross-domain tracking with Google Analytics, but it is equally important that you get your subdomain tracking configured correctly. It’s essential that you maintain a high degree of confidence in your data and subdomain tracking is an important element of this because so many sites use subdomains.
Set your cookieDomain to ‘auto’ in GTM
Send data from all subdomains to the same GA Property using a GTM Lookup table.
Add your main domain to the referral exclusion list in GA.
Show the full URL in Google Analytics by prepending hostname via a filter.
Check that all the subdomains can access the _ga cookie.
Finally, create a separate view in GA for each subdomain.