Photo by Clay Banks / Unsplash

Tracking Subdomains in One Profile for Google Analytics

Kris
Kris

A common confusion is between cross-domain tracking and tracking cross-subdomain tracking.

The analytics.js library uses a unique client ID to determine whether a user is new or returning. A user is considered returning visitor if a hit with a matching client ID has already been sent to the same property.

The client ID is stored in the browser’s cookies by default, which pages on the same domain can only access.

To share a client ID across domains, you can append it as a query parameter to URLs that point from the current domain (the source domain) to the destination domain that you want to track. The shared ClientID allows multiple domains to share a cookie and track the unique user across different domains.

Here is a link that allows you to look into this in more detail. Cross-domain tracking via Google.

Now for tracking subdomains, I recommend executing this through Gooogle Tag Manager.

Step 1:

Set-Cookie Domain to ‘auto.’ This is a default setting for Universal Analytics, but if you’re using Google Tag Manager, you have to set it within GTM explicitly. This setup allows the hits from different subdomains to share one cookie.

Also, if you’re using JavaScript Google Analytics, that is already taken care of.

Step 2:

Go to your Google Analytics Admin panel and set up your domain under the referral exclusion list.

When Google Analytics sees a new referrer different from a source and medium, it will start a new session with the latest traffic source info. The Referral Exclusion List prevents this and will let the original session continue.

Legacy ga.js

To track subdomains within the same profile as the primary domain, you must add an extra line to every page with the tracking code. Assuming your primary domain is “.your-site.com,” the following is the line you will need to add:

pageTracker._setDomainName(“.your-site.com”);For example, it will look like this…


<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._setDomainName(".your-site.com");
pageTracker._initData();
pageTracker._trackPageview();
</script>

As a result, Google Analytics will throw the following results for each of these URLs.

  • www.your-site.com/default.html –> /default.html
  • shop.your-site.com/info.html –> /info.html

If you want to distinguish between your subdomains, you’ll need to create an “Advanced Filter” for your profile.

Filter Type: Custom filter > Advanced
Field A: Hostname
Extract A: (.*)
Field B: Request URI
Extract B: (.*)
Output To: Request URI
Constructor: /$A1$B1

Now, the results for the above examples will look like:

  • www.your-site.com/default.html –> www.your-site.com/default.html
  • shop.your-site.com/info.html –> shop.your-site.com/info.html

Related Articles:
Google Analytics – Linking Across Different Domains
Analytics Tracking and Reporting Online and Offline Campaigns
Overview of Implementing Google Analytics Tracking Codes

Thank you so much for taking the time to read my content.  If you've liked what I've had to say please subscribe!

Analytics

Kris Twitter

As a data journalist, I enjoy curating and analyzing marketing trends, and data. The things that fascinate me the most are the transforming business landscape due to evolving marketing technologies.