Hey there, I’m Benjamin from Loves Data.
In this video, you will learn how to correctly configure Google Analytics for subdomain tracking.
The way to configure subdomain tracking will depend on two things.
First, it will depend on the subdomain you want to track.
For example, do you want to track people on a subdomain of your main website, or do you
want to track people on the subdomain of a different website?
Secondly, the way you configure Google Analytics subdomain tracking will also depend on how
Google Analytics is installed on your website...
Are you using the Google Analytics tracking code?
Are you using the current version of the tracking?
An old version?
Or are you using Google Tag Manager?
We will cover all of this and more.
Let’s get started!
The first thing you need to do is check how Google Analytics implemented on your website.
Okay, so I’m going to begin by viewing the source code for my website...
And I’m going to search for ‘GTM’...
We can see this highlights the Google Tag Manager container code that I’ve added to
my website.
So we immediately know that I can configure subdomain tracking using Google Tag Manager.
If you didn’t find the Google Tag Manager code, then try searching for ‘gtag’, ‘analytics.js’,
or ‘ga.js’.
If you find one of these, then you’re probably using the Google Analytics tracking code.
Today we’re going to focus on using Google Tag Manager.
However, you can modify the Google Analytics tracking code to reflect the different options
we will be covering in this video, so keep following along with us and then check out
the extra resources in the description below this video.
You might want to consider migrating to Google Tag Manager to streamline the way you manage
your implementation.
The next thing you need to establish is where the subdomain is located.
For example, if my main website is ‘lovesdata.com’ and I also want to track ‘welcome.lovesdata.com’,
then this is a subdomain of my website.
However, if my main website is ‘lovesdata.com’ and I want to track people as they navigate
to ‘welcome.analyticsacademy.com’, then this is cross-domain tracking.
We’re going to focus on tracking the subdomains of our main website.
So if you’re looking to track people from one domain to a subdomain on an entirely different
website, then I recommend you also watch my video covering cross-domain tracking.
You can find a link to it in the description below this video.
Now let’s head to Google Tag Manager...
Okay, so today we’re going to use my demo website, along with Google Tag Manager, to
understand how subdomain tracking works.
This is a very simple Google Tag Manager setup.
I’ve added a Google Analytics tag and I’m using the built-in ‘All Pages’ trigger.
This trigger will add Google Analytics to every page that is using the Google Tag Manager
container code.
Let’s select the tag...
We can see it will send a pageview to Google Analytics...
And we can see that it is using the ‘All Pages’ trigger...
Okay, let’s close the tag...
Now let’s select ‘Variables’...
And let’s open the Google Analytics settings variable...
We can see that I’ve added the tracking ID, but all of the other options use the default
settings...
You will notice there is a ‘Cookie Domain’ option and the default value is set to ‘Auto’...
We’ll come back to this in a moment.
Now let’s head to my demo website...
I’ve created a simple page that included the Google Tag Manager container code.
Let’s view the source code for the page by right-clicking...
And selecting ‘View Page Source’...
We can see it’s very basic and we can see the container code...
Now let’s head back to the page...
And let’s right-click again, and this time let’s select ‘Inspect’...
This opens Developer Tools.
And let’s select ‘Application’...
And let’s make sure we select ‘Cookies’ on the left...
We can now see the cookies that have been set by Google Analytics...
These are ‘underscore’, ‘gid’, and ‘underscore’, ‘ga’...
We can also see the cookie domain, this indicates the domain that the cookie belongs to, and
this is the same as the top-level (or ‘root’) domain of my demo site.
We can see it’s ‘dot’, ‘analyticsbeta’, ‘dot’, ‘com’...
Let’s delete these cookies...
And let’s head back to my example page...
And if we select the URL bar...
We can see that I entered ‘www’, ‘dot’...
So let’s remove this...
And let’s reload the page to see if this change the cookie domain...
Now let’s open Developer Tools again...
Okay, we can see that Google Analytics has created the cookies again...
And they’re also set to ‘dot’, ‘analyticsbeta’, ‘dot’, ‘com’...
This means if we were to travel between the two versions of the site, so with and without
the ‘www’, ‘dot’, Google Analytics would still know it’s me because it would
be using the same cookies.
Let’s delete the cookies again...
And let’s close Developer Tools...
And head to a subdomain I’ve created...
It’s ‘example’, ‘dash’, ‘one’, ‘dot’, ‘analyticsbeta’, ‘dot’,
‘com’...
Now let’s open Developer Tools...
We can again see the Google Analytics cookies have been created on the top-level domain,
so ‘dot’, ‘analyticsbeta’, ‘dot’, ‘com’...
This means that by default, Google Analytics will automatically set the cookies at the
top-level.
And people visiting our top-level domain and subdomains will be automatically tracked across
those domains.
Let’s delete the cookies...
And to check that we’re using the default settings, let’s head back to Google Tag
Manager...
We still have the Google Analytics settings variable open and we can see that ‘Cookie
Domain’ is set to ‘auto’.
This is the default setting and this is what’s automatically setting the cookie for our website.
So if you haven’t already, I recommend heading to your own Google Tag Manager container and
checking your Google Analytics settings variable.
In most cases, you probably want Google Analytics to track people across your top-level domain
and subdomains as we’ve seen.
But what if you don’t track your subdomains and top-level domain into the same Google
Analytics property?
What if we wanted each subdomain to be tracked into a different Google Analytics property?
For example, what if each subdomain was owned (or controlled) by different people, like
in the case of a franchise website.
In this scenario, we would need to modify the cookie domain.
One way to do this is to set the hostname as the ‘Cookie Domain’.
So let’s edit the variable...
And let’s remove ‘Auto’...
And let’s select ‘Page Hostname’...
This will mean the cookie domain is set based on the hostname of the page someone is viewing.
We will see how this works in a moment, but basically it will mean that if someone is
viewing a subdomain, then the cookies will be set for the subdomain, not the top-level
domain.
Now if we want to track the subdomains into different Google Analytics properties, we
will also need to set different Google Analytics tracking IDs for the different subdomains.
I’ve already created a lookup table variable to do this.
So let’s remove the current tracking ID...
And let’s click the icon to add our variable...
I’m going to use the variable called ‘Subdomain Google Analytics’, but before we add it,
let’s click on the information icon to see how it’s configured...
We can see the lookup table will use the hostname...
And then check this against the list of subdomains I’ve defined...
It will then output the corresponding Google Analytics tracking ID for the subdomain...
And I’ve also set a default value, which will be used when a subdomain hasn’t been
identified in the lookup table...
Let’s close the variable...
And let’s select it to add it to our Google Analytics settings variable...
Now let’s click ‘Save’...
And let’s publish our changes...
Okay, now we’re going to head back to my demo site in just a moment, but before we
do I want to show you how you can make this change if you’re not using Google Tag Manager.
So let’s head to Google Developers...
This page outlines modifications we can make to the ‘gtag’ version of the Google Analytics
tracking code.
We can see there is an option to set the ‘cookie domain’...
So if you’re using the tracking code, then you do have the option of modifying the code
you’ve added on your website to set the cookie domain.
This is similar to what we’ve just done in Google Tag Manager, but this change needs
to be made to the actual tracking code you’ve added to your website.
Okay, now let’s head back to the subdomain on my demo site...
And let’s refresh the page...
And open Developer Tools...
We can now see that the cookies have been set for the subdomain instead of the top-level
domain.
Now let’s head to another subdomain I’ve created...
Let’s open Developer Tools...
We can see the cookies have again been set for the subdomain...
So by changing the ‘cookie domain’ from auto to the particular subdomain (or subdomains)
we can control where the cookies are set.
Before we wrap up, I want to show you one more thing.
Let’s head to Google Analytics...
If you are tracking multiple domains and subdomains into one Google Analytics reporting view,
then you might want to consider creating a filter to modify your reports.
Let’s select ‘Filters’...
And then ‘Add Filter’...
I want to emphasis, that this is optional...
I’m going to create a filter that adds my domain to the ‘All Pages’ report.
I’m going to name the filter ‘Add Hostname’...
And I’m going to select ‘Custom’...
Then ‘Advanced’...
I’m going to select ‘Hostname’ for ‘Field A’...
And I’m going to enter ‘open bracket’, ‘period’, ‘asterisks’, ‘close bracket’...
This will grab the hostname (which is the domain name) for the page someone is viewing
on our website.
For example, if someone is viewing a page on ‘welcome.lovesdata.com’, then it will
grab this value.
Now I’m going to select ‘Request URI’ for ‘Field B’...
And again, I’m going to enter ‘open bracket’, ‘period’, ‘asterisks’, ‘close bracket’...
This will grab the URL for the page someone is viewing our website.
Then we’re going to select ‘Request URI’ for the ‘Output’...
Then I’m going to enter ‘dollar sign’, ‘a’, ‘one’, ‘dollar sign’, ‘b’,
‘one’...
This will combine the hostname with the page URL and modify the page URL in our reports.
I can then click ‘save’ when I’m happy.
Please remember this is optional and it will permanently modify what appears in your ‘All
Pages’ report moving forward.
So if you’re not sure about adding this filter, then consider adding it to a testing
view before modifying your primary Google Analytics reports.
That’s it!
So that’s how you can configure Google Analytics for subdomain tracking.
Remember you will need to establish two things...
You will need to determine the subdomain you want to track...
Is it simply a subdomain of your main website, or is it a subdomain on a different website?
Then you will need to identify how to implement Google Analytics...
Are you using the Google Analytics tracking code?
And if so, which version are you using?
Or are you using Google Tag Manager?
And don’t forget, that once you’ve implemented Google Analytics on your subdomain, then you
might want to consider adding a filter to Google Analytics so that it’s easier to
see the subdomain in your reports.
Are you going to implement subdomain tracking?
I’d love to know!
Let me know in the comments below.
And if you found this video helpful please subscribe, share it with your friends, and
hit the like button so I know to make more videos like this.
See you next time!