I have a site, sandwich.example.com, that contains content about sandwiches. I have another site, hotdog.example.com, that contains content about hot dogs. I recently implemented Google Analytics 4 on hotdog.example.com and found out that the cookies were being set globally for the entire example.com domain. This was causing problems on sandwich.example.com. So, I followed the docs to specify a specific cookie_domain. However, once that version of the site was deployed, all the analytics stopped working. I can't find anywhere else, any other information about troubleshooting or additional steps that might be needed to get that to work. This is the snippet that is inside the <head> tag in index.html.
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<TAG_ID>">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<TAG_ID>', {
'cookie_domain': 'hotdog.example.com'
});
</script>
Has anyone else faced this problem? How can I get Google Analytics to be ONLY applied to a single subdomain.
place this js in your head html tag in the sub domain index hotdog.example.com
make sure you specify the right tag id, this should work, if it's not working review your tag and inspect the browser for any issue using f12 developer tool.
hope this helps.