How to stop Google analytics from setting cookies on your website

You are likely on this page due to GDPR or the cookie consent policy. I am in no way an expert on law, GDPR or the like. As I understand it under GDPR you need consent before setting cookies and under the cookie policy you need to tell the user that you use cookies. Under both you need to describe what your cookies are used for. I decided to remove all cookies from my website - except the technical ones that cloudflare uses to loadbalance. I have also removed the ones that Google analytics set. However this has some side effects - more on that later.

In the below you can see how I initialize Google analytics for my site:

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);

ga('create', 'UA-69002404-2', {
    'storage': 'none' //Disable cookies
});
ga('set', 'anonymizeIp', true); //anonymizeIp

The first 5 lines are standard Google analytics code that you need in order to get Google analytics to work. Normally after this you would call ga this way:

ga('create', 'UA-69002404-2');

In order to stop Google analytics from setting cookies you can set storage to 'none'. As seen in the below code snippet:

ga('create', 'UA-69002404-2', {
    'storage': 'none' //Disable cookies
});

You will notice that I also anonymizeIp:

ga('set', 'anonymizeIp', true); //anonymizeIp

This I also do due to GDPR, Iubenda.com puts it well:

Google has provided this function since May 2010 to allow website owners to request that all of their users’ IP addresses be anonymized within Google Analytics. This feature is designed to help site owners comply with their own privacy policies, recommendations from local data protection authorities and legal regulations like the GDPR, which may prevent the storage of full IP address information.

If you are looking to disable cookies you will also want to anonymize IP, as this sets the last digits of the users' IP addresses to zero, making it anonymous and it can then not be used to identify a person.

What do you lose when you disable cookies?

As I warned earlier, you will lose some features when you disable cookies and do not provide another storage option.

Google will no longer be able to track if the requests are from the same user. Every request will be treated as a new visitor to the site and you will not be able to track users behavior flow. You will not be able see if the user first visits page A then B then C. This also impacts real time stats as it cannot track the same user as they navigate around. Every time a user visits a new page it will look like a new user visited your site.

However much of the functionality still works, such as geography, browser statistics, page visits etc. But you will not be able to track unique visits.

That is it

This is how I have disabled cookies for Google analytics, if you have any thoughts or comments on this, please leave them down below!