Google Analytics Without Cookie Popups
Get intelligence about user engagement without the poor user experience of intrusive cookie popups.

I wanted a way to track the performance of my blog posts without showing those annoying cookie popups which have been required in Europe for a while now.
I tried out a few analytics providers, but in the end I keep coming back to Google Analytics. Even without setting cookies, which obvously means losing some metrics, like retention, their other features still have the competition beat.
So how do I get the most out of Google Analytics?
Cookie Popups
I want to set a cookie when possible, and I want the graceful degradation when it's not.

By now we are all pretty exhausted with going through cookie popups, which are never standardised and which all look slightly different and have slightly different cookies settings that have slightly different purposes.
We either spend way too much time worrying about cookies to get to the actual site data, or we simply agree to everything. Neither of which are ideal.
So what if we just didn't set cookies?
Well, we loose out of tracking retention. Which is nice to know about.
So what if we just set the cookies when we can do so without a popup - i.e. outside Europe?
Getting location without tracking
Luckily, getting the client location from a client IP address is a solved problem. Doing so without sharing client data with anyone else is also a solved problem. See my (very old) blog post about a self hosted geo location service which neither shares nor stores any information about the client.

Putting it together
Combining Google Analytics and geo location, we can now track almost all the metrics that we care about with relative ease, and without being annoying to our users.
All we need to do is to match the client country against the list of European countries, and anonymize the client IP and disable setting cookies before sharing anything with Google, of the client is inside EU.
The key here is the 'anonymize_ip': true
and 'client_storage': 'none'
settings which are applied when the country is in EU.
function initializeGoogleAnalytics(eu) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
if (eu) {
gtag('config', '<INSERT TAG HERE>', {
'anonymize_ip': true,
'client_storage': 'none'
});
} else {
gtag('config', '<INSERT TAG HERE>');
}
}
fetch('/json/').then((response) => {
return response.json();
}).then((json) => {
let codes = [
'AT', 'BE', 'BG', 'CY',
'CZ', 'DE', 'DK', 'EE',
'ES', 'FI', 'FR', 'GB',
'GR', 'HR', 'HU', 'IE',
'IT', 'LT', 'LU', 'LV',
'MT', 'NL', 'PO', 'PT',
'RO', 'SE', 'SI', 'SK'
];
if (codes.includes(json.address.addressCountry)) {
initializeGoogleAnalytics(true);
} else {
initializeGoogleAnalytics(false);
}
});
There you have it.
Google Analytics without cookie popups.