Google Tag Manager isn’t something new. Nope, in fact, it has been around since 2012, and while initial adoption was slow, there really is no excuse not to be using it now. If you want to get started with the basics, Google has put together a really good, lightning quick course on how to get started with the fundamentals of Tag Manager here.
How To Track Sub-domains With Tag Manager
Right, so you need to track traffic on your main domain, as well as on a sub-domain on your site, perhaps within a separate sign up process or something of the sorts. Regardless of how you are tracking your site, whether with the old school manual tracking code on your site, or you are using the Tag Manager container, this guide won’t make a difference. However, you will need to ensure that there is tracking code, using the same UA code across the main site as well as the sub-domain.
- Create Constant Variable
- Add sub-domain variable to tracking code
- Set CookieDomain to Auto
- Set AllowLinker Value to true
- Add Hostname Filter Within Analytics
Step 0: Create a Separate View Within Google Analytics
Create a separate view within Google Analytics to ensure that you don’t break anything ;) It does happen!
Step 1: Create A Constant Variable
This step creates the domain variable to track and as such you would enter example.com if you want to track all sub-domains, rather than maybe specifically admin.example.com. This step isn’t required, as you could simply enter in example.com within step 2, rather than save and use a variable. We just find this a little easier and allows you to use it in the future if you need to.
· Once you have selected your container, click on Variables on the left, then New down at the bottom.
· Name your variable, and in this case, I would keep it simple and call it “example.com”. Then choose Constant, and enter in your domain without any http:// or www, or any sub-domain; simply example.com
Step 2: Add sub-domain variable to tracking code
- Head over to Tags and select your tracking code.
- Scroll down to Cross-Domain tracking, and within the Auto Link Domains field, add your new variable that you have just saved, Example.com.
Step 3 & 4: Set AllowLinker Value to true & Cookie Domain to Auto
- Before you head anywhere else, under More Settings, Fields to Set, you are going to add the next two steps.
- allowLinker: true
- CookieDomain: auto
Step 5: Add Hostname Filter Within Analytics
Now that everything is setup with Tag Manager, you need to ensure that you are getting accurate information within Google Analytics. You need to go and apply a filter that allows you to see the full URL from all domains that you have now merged together. This would allow you to view the top traffic pages together, but with the ability to see which pages come from a sub-domain, or from the main domain.
- Within Analytics, head to Admin within the top left.
- Then Filters on the right
- Custom filter
- Field A -> Hostname: (.*)
- Field B -> Request URI: (.*)
- Output -> Request URI: $A1$B1
- Field A & Override Output Field are required fields so make sure you tick them!
Step 6: Publish Your Changes!
Head to the top right within Google Tag Manager and hit PUBLISH! Now don’t forget to test a page view within Analytics using the real-time view across your main domain, as well as a sub-domain.
What does your final configuration look like?
For a good guide on how to setup event tracking using Google Tag Manager, try this one out, or, if you are looking for a guide to supplement the Google Fundamentals course, then check this one out.