简体   繁体   中英

How to set Google Analytics code or web settings for www subdomain (not naked domain)?

I recently came across a performance tip about setting cookies only on a subdomain (eg www ) and keeping the root domain (eg example.com ) cookie-free.

Using Chrome's cookie viewer, I noticed a Google Analytics cookie named _ga at my root domain.

I am wondering how to configure the Google Analytics code or Google Analytics web settings to ensure that any Google Analytics cookies are only set to my www subdomain.

In case it matters, my Google Analytics code is as follows, and it is placed as the last element before the ending HEAD tag (if there is a superior place in terms of performance, please let me know as well):

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-12345678-1', 'auto');
  ga('send', 'pageview');

</script>

Note that I did see Configuring cookieless domain while using Google analytics - but the code is old and the answer unsatisfying.

Of course I am aware of the idea of buying a whole new domain and keeping it cookie-free but I am wondering if I can do anything about the Google Analytics code in the interim.

If I am understanding you correctly, and please clarify if I am incorrect, you want to only track on your subdomain, www (ie. Google Analytics cookie are only set to your www subdomain). Is that correct? In that case, you only need to add the tracker your specific domain like this:

ga('create', 'UA-XXXX-Y', 'www.yourdomain.com');
ga('send', 'pageview');

If you already have your GA snippet placed before the tag, then that should be sufficient in terms of performance. Anywhere else and the performance impact may be negligible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM