Need to make Google Analytics GDPR-Compliant?

Make this one small change to your Analytics embed snippet:

(function(i,s,o,g,r,a,m){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)
})(window,document,'script','https://www.gdpr-google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXXXX-1', 'auto');
ga('send', 'pageview');

Done!

Does it work with other tracking services (eg. GTM, Mixpanel, Segment)?

Yes — with only a little re-jigging! Insert the following snippet prior to your other tracking snippets. Add the URL of each service as a dependency. To ensure the trackers don't load by themselves, you'll want to remove any X.src=XXXX; code in your original tracking snippets. That's it!

<script>
    (function(g,d,p,r){g[p]=g[p]||function(a,b,c){
    g[d]=g[d]||{};g[d][a]=g[d][a]||[];g[d][a].push([b,c]);};
    })(window,'GDPRGoogleAnalytics','gdpr');

    gdpr('dependency', 'https://www.googletagmanager.com/gtm.js?id=XXXXXXXX');
</script>
<script src="https://www.gdpr-google-analytics.com/analytics.js"></script>

Need to change something?

gdpr('vars', 'buttonColor', 'red');
gdpr('vars', 'buttonText', '&check; Accept!');
gdpr('vars', 'content', 'By clicking "Accept All Cookies"...');
gdpr('vars', 'policyURL', 'http://example.com/');
gdpr('vars', 'policyText', 'See Our Privacy Policy');

Need to know when the terms have been accepted?

gdpr('on', 'accept', function() {
    alert('Terms have just been accepted!');
});

Any CSS can be overridden in the .gdpr-google-analytics-notice element.

Don't want to trust our domain? Just include the analytics.js file that resides in this directory.

The full size of this library is ~4.66 kb.

Any questions? Look at this page's source.