25

I use Google Analytics on some pages of my website. My entire site uses SSL. Is it possible to secure the cookies of Goole Analytics __umt*.

At least I would like to enable the secure flag on them. At best I would also like to set the HTTP only flag on them, but I don't think the latter is possible (because Google uses JS to use the cookies I think).

Is it possible to do this? And if so how to set it up?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

4 Answers4

22

Short of modifying the GA script and storing your own local copy, no, you're not going to be able to set secure or HttpOnly flags. I imagine Google has made a conscious design decision about this and certainty there can be advantages from being able to track the same user across both secure and insecure schemes.

You've got to ask yourself what you're trying to achieve with this though; what's the potential exploit if a man in the middle can intercept and read or manipulate the cookie due to lack of the secure flag? Same again with the HttpOnly flag; what's the upside for the attacker if they can retrieve this cookie via an XSS exploit?

I've seen this sort of feedback from automated security scanners before that are simply triggered by the missing flags without having the context of what the cookies are actually being used for. That would be my first guess at why a question like this would even come up.

Troy Hunt
  • 20,345
  • 13
  • 96
  • 151
17

There is a new option called cookie_flags when loading the GA library.

ga('create', 'UA-XXXXX-Y', {
    cookieFlags: 'max-age=7200;secure;samesite=none'
});
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
raik
  • 171
  • 1
  • 6
4

Addition to raik's answer:

Even more info about setting more cookie values on analytics may be found in this blog post

For example how to do it using gtag:

gtag('config', 'G-N2A3NNNNN', {
  cookie_flags: 'max-age=7200;secure;samesite=none'
});
damon
  • 14,485
  • 14
  • 56
  • 75
Erik Melkersson
  • 899
  • 8
  • 19
-2

The Google Analytics Cookies (yes, set via Js) are primary cookies, thus, only your domain is able to write those. So if it´s security your are looking for, that´s as secure as it gets.

Although, I´m not 100% sure about your question here, but if you are looking to enable Google Analytics only on HTTP pages, your can alter the GA code on your pages to do so in this way, as an example:

<script type="text/javascript">
  if(document.location.protocol != 'https:'){
     var _gaq = _gaq || [];
     _gaq.push(['_setAccount', 'UA-XXXXX-X']);
     _gaq.push(['_trackPageview']);

     (function() {
     var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
     ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') +   '.google-analytics.com/ga.js';
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
  }
</script>
Augusto Roselli
  • 220
  • 1
  • 4
  • 4
    OP is asking if the GA cookies can be set using the secure flag, as in http://en.wikipedia.org/wiki/HTTP_cookie#Secure_cookie – Yahel Feb 03 '12 at 16:36