Are you sure you need to use Google Analytics? Consider Fathom Analytics or Simple Analytics as an alternative!
There's a way to use Google's analytics.js script in a GDPR- and cookie-friendly manner.
This script loads Google Analytics for you. It then disables all cookies set by Google Analytics & enables the IP anonymization built into GA. The unique client ID is generated by nanoid and stored in local storage using storee (if available) or in the history state object using histore.
Because you're now using a first party method instead of a third party cookie to track your user across their session, you might be able to get away without the dreaded cookie bar.
The ga
object is still exposed on window
, so you can still do any other event tracking. Be careful to keep your GDPR-compliance if you do so. There is currently no option to rename the ga
global.
<script src="anonymous-analytics.js"></script>
<script>
window.anonymousAnalytics('UA-XXXXXX-XX');
</script>
It works on every modern browser and has been tested in IE11. I don't see any reason why it should not work in any older versions of Internet Explorer, but I haven't tested it thoroughly.
I am not a lawyer and cannot be held accountable for any wrong advice