12

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.

1

3 Answers 3

17
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

Sign up to request clarification or add additional context in comments.

2 Comments

Very important that consent is executed before config.
The guidance has consent line above js: developers.google.com/tag-platform/devguides/consent/…
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>

1 Comment

This is a good answer, but it omits mention of an important problem; creating that CLIENT_ID as the link in the answer describes..
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

3 Comments

This disables Analytics entirely, though. How do you enable Analytics, but not have it set a cookie? (Equivalent to ga's "storage:none")
I believe the below is what you are after. ‘gtag('config', '<TARGET_ID>', { 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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.