Google Analytics Subdomain Tracking Tutorial 2022

Last Updated: August 8, 2022

In this article, I will talk about how to track subdomains in Google Analytics. 

What is a subdomain, and why should you track them in Google Analytics?

A subdomain is an additional part of the main domain. For example, if you have ‘example.com’ as the main domain, then ‘blog.example.com’ and ‘shop.example.com’ will be called subdomains. 

Even though these subdomains are subsets of your website, you still want to track the users across all these websites. 

Let’s understand it with an example. Let’s suppose you have the website ‘example.com’, and the following is the sample user journey:

  1. The user arrives at http://www.example.com/
  2. They click on the article link and navigate to http://blog.example.com/
  3. After reading the article, they click on a product link and navigate to http://shop.example.com/ 

Now, if you haven’t set up subdomain tracking, then every time the user navigates to a different subdomain, he will be considered a new user and a new session will begin. So, in the above example, we would have three users and three sessions for each website.

However, if you have set up subdomain tracking, there will be only one user and one session for the above example. 

When a user visits your website for the first time, Google Analytics sets cookies to identify unique users and unique sessions. When the user is navigating between the website subdomains, this cookie value is shared so that Google identifies the user across the subdomains and does not set a new cookie or new session. 

To learn more about cookies, you can read this post: Google Analytics cookies: _ga cookie, _utmb, _gat – Tutorial

To set up subdomain tracking in Google Analytics, there are two major steps, as below:

  1. Setting up a cookie domain
  2. Setting up a referral exclusion list

Now let’s look into these steps in more detail.

The first step in setting up subdomain tracking is to configure cookie domain parameters to ‘auto’. 

When you set the cookie domain parameter to ‘auto’, Google analytics will set the ‘_ga’ cookie on the main domain, and it will be accessible by all the subdomains for reuse. 

For example, if the user lands on a website, say ‘blog.example.com’, the cookie will be set on ‘.example.com’ and can be reused by other subdomains such as ‘shop.example.com’ or ‘checkout.example.com’.

Setting up a cookie domain depends on how your analytics is implemented.

For Google Analytics Tag (analytics.js)

If you are using analytics.js for analytics implementation, you must have the below line of code in your main tracking code.

ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’);

Your final code will look like below:

<!– Google Analytics –>

<script>

(function(i,s,o,g,r,a,m){i[‘GoogleAnalyticsObject’]=r;i[r]=i[r]||function(){

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)

})(window,document,’script’,’https://www.google-analytics.com/analytics.js’,’ga’);

ga(‘create’, ‘UA-XXXXX-Y’, ‘auto’);

ga(‘send’, ‘pageview’);

</script>

<!– End Google Analytics –>

By default, the cookie domain is always set to ‘auto’; if you see this code in your main tracking code, then you are fine, meaning the cookie will always be set on the main domain.

For Global Site Tag (gtag.js)

If you are using ‘gtag.js’ for analytics implementation, then you do not need to modify the code. 

‘gtag.js’ by default has the cookie domain parameter set to ‘auto’.

But in the case that your main tracking code is modified and sets a cookie on a particular domain only, then you need to remove the code line which sets the cookie:

gtag(‘config’, ‘UA-XXXXX-Y’, {

   ‘cookie_domain’: ‘blog.example.com’ // remove this code line to set cookie domain parameter to ‘auto’.

});

For Google Tag Manager

If you are using Google Tag Manager for analytics implementation, then the cookie domain parameter is not set by default to ‘auto’, and you need to do the configuration settings in the Tag Manager console. 

Follow the below steps to set cookie domain parameters in Google Tag Manager.

Step-1: Navigate to your Google Tag Manager account and click on ‘Tags’ in the left-hand side menu.

tags 1

Step-2: Now select your page view tag, which is set to fire on all pages.

pageview tag

Step-3: A new overlay will appear like below. Click on the pencil icon to edit the tag configuration.

pageview tag pencil icon

Step-4: Now click on ‘More settings’.

moresettings

Step-5: Now click on ‘Fields to set’.

fields to set

Step-6: Now click on ‘Add Field’.

add field

Step-7: You will get a screen like below. In the input box available under ‘Field Name’, type ‘cookieDomain’.

field name

Step-8: Now, in the input box available under ‘Value’, type ‘auto’.

field value

Step-9: Click on ‘Save’.

save 2

Congratulations! You have successfully configured your analytics pageview tag to set the cookie domain parameter to ‘auto’. Your final tag configuration will look like below

tag configutration

Another way to set the cookie domain parameter to ‘auto’ is by defining it in the analytics variable settings, which you use for every tag while sending data to Google Analytics.

Just navigate to ‘Variables’ in the Tag Manager console.

Variables tab 1

Select the ‘Analytics configuration variable’ under ‘User-defined Variables’.

analytics setting variable

It will open the ‘Variable configuration panel’. Now under the input box named ‘Cookie Domain, ‘ type ‘auto’ and save the variable.

Google Analytics Subdomain Tracking

Setting up a referral exclusion list

The second step in setting up subdomain tracking is to update the referral exclusion list in the Google Analytics admin console. 

Now let me tell you why this is necessary. When the user navigates from one subdomain to another, the analytics tracking code will pass the referrer details. If these details are available, Google Analytics will start a new session with the referrer source information. 

To avoid this, we have already set the cookie domain to auto, and now we need to add all the subdomains to the referral exclusion list. Once added to the list, Google Analytics will not consider any of your subdomain’s referrers as new and will continue with the original session.

To set a referral exclusion list, follow the below steps:

Step-1: Navigate to your Google Analytics account and click on ‘Admin’ on the left-hand navigation menu.

admin

Step-2: Under the ‘Property Settings’ column, click on ‘Tracking Info’.

tracking info 1

Step-3: Now click on ‘Referral exclusion list’.

refrerral exclusion list

Step-4: An overlay will appear like below. Now click on ‘+ Add Referral Exclusion’.

add to list

Step-5: A new configuration panel will appear, like below. Now under ‘Domain’, add the domain name you want to exclude and then click on ‘Create’.

domain and create

Note: You do not need to specify the individual subdomains here. Since we have set our cookie domain to ‘auto’, just adding the primary domain (highest domain) will be sufficient here. 

You can see the domain list excluded in the admin panel.

domain list 1

And that’s it! You are done setting up subdomain tracking in Google Analytics. 

How to filter a subdomain or subdirectory in Google Analytics

Follow the steps below to filter a subdomain or subdirectory in Google Analytics:

Step-1: Ensure that your primary domain and subdomain use the same Google Analytics tracking code.

So if your primary domain is www.optimizesmart.com and one of the sub-domains is learn.optimizesmart.com, then they both should be using the same Google Analytics tracking code.

Step-2: Login to your GA account and then navigate to your main reporting view.

Step-3: Click on the ‘Admin‘ link at the bottom left-hand side of your screen:

admin google analytics

Step-4: Click on the ‘View Settings’ link under the ‘View‘ column:

view settings 1

Step-5: Click on the ‘Copy View’ button:

copy view google analytics

Step-6: Give your view a new name and then click on the ‘Copy View’ button:

copy view google analytics 2

As soon as you click on the ‘Copy View’ button, you will automatically be redirected to the new duplicate reporting view.

Step-7: Click on ‘Filters‘ under the ‘View‘ column:

filters

Step-8: Click on the ‘+ADD FILTER‘ button:

filter a subdomain or subdirectory in Google Analytics

Step-9: Name your new filter ‘View Full Page URL In Reports’:

view full page urls in reports

Step-10: Click on the ‘Custom‘ button and then on the Advanced‘ button:

custom advanced filter

Step-11: Set up your custom advanced filter like the one below:

set up custom advanced filter google analytics

This custom advanced filter is set up to append the hostname to the request URI.

By default Google Analytics only show the request URI.

For example, if the URL is:

https://www.optimizesmart.com/best-excel-charts

Then the request URI would be:

/best-excel-charts

So by default, you can not see the traffic from sub-domains separately in GA. That’s why we need to append the hostname to the request URI.

Following are examples of hostnames:

  • www.optimizesmart.com
  • learn.optimizesmart.com
  • training.optimizesmart.com

Step-12: Click on the ‘Save’ button to save your filter:

save filter

Step-13: Click again on the ‘+ADD FILTER‘ button:

add filter

Step-14: Set up a new filter with the following similar configuration:

add filter to view

Use your hostname instead of the one you see in the screenshot.

Your final filter set-up should now look like the one below:

final filter set up

Step-15: Let us suppose you want to filter only a particular sub-directory of your sub-domain. In that case, create a new filter with the following configuration:

ga filter to include only subdirectory traffic 1

Your final filter set-up should now look like the one below:

final filter set up 2

If you want to filter only the subdirectory of your website but not the subdomain, then you just need to do two things:

#1 Create a copy of your main reporting view.

#2 Create and apply the filter that filter out traffic from your subdirectory.

Here is what I meant:

ga filter to include only subdirectory traffic 2
  1. Cross Domain Tracking in Google Analytics – Complete Guide
  2. How to check cross-domain tracking in Google Analytics
  3. Cross Domain Tracking in GA4 (Google Analytics 4) Setup Guide
  4. Cross Domain Tracking with Google Tag Manager (GTM)
  5. Setting up Sales Funnel across websites in Google Analytics
  6. How to use two Google Analytics codes on one page
  7. How to correctly use referral exclusion list in Google Analytics
  8. Tracking Google Analytics Paypal Referral and other payment gateways
  9. Google Analytics Subdomain Tracking Tutorial
  10. Self-referral Google Analytics 4 – Referral exclusion GA4
  11. How to fix self-referrals in Google Analytics
  12. PayPal.com and the referral exclusion list

Frequently asked questions about Google Analytics Subdomain Tracking Tutorial

What is a subdomain?

A subdomain is an additional part of the main domain. For example, if you have example.com as the main domain, then blog.example.com and shop.example.com will be called subdomains. Even though these subdomains are subsets of your website, they are essentially tied to your primary domain.

What is the difference between subdomain and cross-domain?

A subdomain is always attached to your primary domain. For example, if your primary domain is’ .example.com’, then ‘blog.example.com’ or ‘shop.example.com’ are the subdomains. Whereas a completely different primary domain will be considered as cross-domain, such as ‘xyz.com’. 
To know more about cross-domain tracking, visit: 
Cross-Domain Tracking in Google Analytics – Complete Guide
Cross-Domain Tracking in GA4 (Google Analytics 4) Setup Guide

What is the referral exclusion list?

A referral exclusion list is the list of domains whose incoming traffic will be treated as direct traffic (instead of referral traffic) by Google Analytics. This way, you don’t see the referral traffic from certain domains (like your own subdomain or from a payment gateway like Paypal) in your Google Analytics reports.
To know more about referral traffics, visit:
Referral traffic vs Direct traffic in Google Analytics
How to correctly use referral exclusion list in Google Analytics

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."



Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.



   

My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder, OptimizeSmart.com
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade
error: Alert: Content is protected !!