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 if a hit with a matching client ID has already been sent to the same property.
By default, the client ID is stored in the browser’s cookies, which means it can only be accessed by pages on the same domain.
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. This allows multiple domains to share a cookie and track the unique user across different domains.
Here is a link that allows you look into this into more details. Cross-domain tracking via Google.
Now for tracking subdomains, my recommendation is to execute this through Gooogle Tag Manager.
Set Cookie Domain to ‘auto’. This is a default setting for Universal Analytics, but if you’re using Google Tag Manager, you have to explicitly set it within GTM. This allows the hits from different subdomains to share one cookie.
Go to your Google Analytics Admin panel and set up your domain under referral exclusion list.
When Google Analytics sees a new referrer that is different from an original source and medium, it will start a new session with the new traffic source info. The Referral Exclusion List prevents this from happening and will let the original session continue.
In order to track subdomains within the same profile as the primary domain, you will need to add an extra line to every page with the tracking code.
Assuming your primary domain is “.your-site.com”
Following is the line you will need to add:
For example, it will look like this…
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
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
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
Increase Your Website Conversions
Get your FREE cheat sheet: "5 Steps to Building a Profitable Website Using Marketing Technology"