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:
The user arrives at http://www.example.com/
They click on the article link and navigate to http://blog.example.com/
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 navigates 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.
The first step in setting up subdomain tracking is configuring 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.
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.
Step-2: Now select your page view tag, which is set to fire on all pages.
Step-3: A new overlay will appear like below. Click on the pencil icon to edit the tag configuration.
Step-4: Now click on ‘More settings’.
Step-5: Now click on ‘Fields to set’.
Step-6: Now click on ‘Add Field’.
Step-7: You will get a screen like below. In the input box available under ‘Field Name’, type ‘cookieDomain’.
Step-8: Now, in the input box available under ‘Value’, type ‘auto’.
Step-9: Click on ‘Save’.
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
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.
Select the ‘Analytics configuration variable’ under ‘User-defined Variables’.
It will open the ‘Variable configuration panel’. Now under the input box named ‘Cookie Domain, ‘ type ‘auto’ and save the variable.
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.
Step-2: Under the ‘Property Settings’ column, click on ‘Tracking Info’.
Step-3: Now click on ‘Referral exclusion list’.
Step-4: An overlay will appear like below. Now click on ‘+ Add Referral Exclusion’.
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’.
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.
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:
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:
Step-4: Click on the ‘View Settings’ link under the ‘View‘ column:
Step-5: Click on the ‘Copy View’ button:
Step-6: Give your view a new name and then click on the ‘Copy View’ button:
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:
Step-8: Click on the ‘+ADD FILTER‘ button:
Step-9: Name your new filter ‘View Full Page URL In Reports’:
Step-10: Click on the ‘Custom‘ button and then on the‘Advanced‘ button:
Step-11: Set up your custom advanced filter like the one below:
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:
Step-13: Click again on the ‘+ADD FILTER‘ button:
Step-14: Set up a new filter with the following similar configuration:
Use your hostname instead of the one you see in the screenshot.
Your final filter set-up should now look like the one below:
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:
Your final filter set-up should now look like the one below:
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.
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 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
Learn and Master Google Analytics 4 (GA4) - 126 pages ebook
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.