I have a website on www.example.com and use Google Analytics. I've also set up static.example.com which serves all static content.
The problem is that the default behavior of GA is to issue cookies on ".example.com" but I don't want the static content traffic to be carrying the weight the GA cookies.
I tried pageTracker._setDomainName("none"); and it worked well for the cookies problem but it completely messed up the "Avg. Time on Site" report (from 5 mins average it went to 40 mins until the day I reverted the _setDomainName call).
Any idea why this is happening and how could I fix it?
GA4 also automatically handles subdomains. If you are working with a business that operates on multiple subdomains (e.g. blog.business.com, support.business.com, www.business.com, etc.), you will most likely want to track website visitors with Google Analytics across all these websites.
If you have the same tracking code installed on multiple domains and you want to track a single user as they visit those domains, you need cross-domain tracking. BUT if you have a single domain with multiple subdomains, you don't need cross-domain tracking, just subdomain tracking.
The solution is to do pageTracker._setDomainName("www.example.com")
and then GA will issue the cookies to ".www.example.com". That way analytics still works fine and the cookies do not leak into the other static subdomain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With