9

I am looking for a way to disable the cookies set by Google Analytics. I found some infos in Google's devguides: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#disabling_cookies

Here it says that I should add the following code:

ga('create', 'UA-XXXXXXXXX-X', {
  'storage': 'none'
});

But where exactly? I already tried to add it inside the tracking code:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXXX-X');


  ga('create', 'UA-XXXXXXXXX-X', {
  'storage': 'none'
});
</script>

I'm grateful for every clue.

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
Khaleen
  • 103
  • 1
  • 4
  • Possible duplicate of [Is there a setting on Google Analytics to suppress use of cookies for users who have not yet given consent](https://stackoverflow.com/questions/10668292/is-there-a-setting-on-google-analytics-to-suppress-use-of-cookies-for-users-who) – Linda Lawton - DaImTo Nov 11 '19 at 13:28

3 Answers3

15
gtag('consent', 'default', {
    'ad_storage': 'denied',
    'analytics_storage': 'denied'
});

Note that it must be called before any other commands that send measurement data.

https://developers.google.com/tag-platform/devguides/consent#set_consent_defaults

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
4

storage: 'none' is for analytics.js https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#disabling_cookies

For gtag.js, I think client_storage: 'none' is what you're looking for. It's referenced in a Medium article titled How to use Google Tag Manager and Google Analytics Without Cookies

<!-- Global site tag (gtag.js) - Google Analytics with out cookies -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'GA_MEASUREMENT_ID', {
       client_storage: 'none',
       client_id: CLIENT_ID,
  });

</script>
ProfX
  • 236
  • 2
  • 6
  • This is a good answer, but it omits mention of an important problem; creating that CLIENT_ID as the link in the answer describes.. – Peter Dec 02 '21 at 09:47
0

Two things, you have confused two versions, the ga create and gtag are different versions. Use the gtag one. Your code for this to work is below:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async     src="https://www.googletagmanager.com/gtag/js?id=UA-    XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

 window['ga-disable-UA-XXXXXXXXX-X'] = true;

 gtag('config', 'UA-XXXXXXXXX-X');

});
</script>

See reference here https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out

Jesse
  • 156
  • 5
  • 1
    This disables Analytics entirely, though. How do you enable Analytics, but not have it set a cookie? (Equivalent to ga's "storage:none") – M Somerville Mar 10 '20 at 09:13
  • I believe the below is what you are after. ‘gtag('config', '', { cookie_expires: 0 });’ If you set the cookie_expires value to 0 (zero) seconds, the cookie turns into a session-based cookie and expires once the current browser session ends. – Jesse Mar 11 '20 at 09:23
  • 3
    Thanks for the reply, but that still sets a cookie (which given browser session time nowadays could actually be longer than a timed-expire cookie); I would like a way that doesn't set a cookie at all (which you can do easily with "ga"). Doesn't look like there is a way. – M Somerville Mar 11 '20 at 09:49