Cookie Monster

Image | CC0 pixabay

How to let your Visitor set an Opt-Out-Cookie for Google Analytics Tracking

Learn

Keep reading for an explanation on how to go on about it.

Keep reading

The button will take you to a step by step guide through the process.
Continue

Watch

Take a look at what this is all about and see the feature in action.

Go there

The button will take you to a project were we implemented this feature.
legal

Download

Get the files from GitHub immediately and for free.

From GitHub

The button will take you to GitHub were the files you need are available.
GitHub

We have very strict privacy regulations in Germany. You have to monitor that your contractors adhere to regulations concerning personal data. Which for example is that personal data is not allowed to leave the EU. Furthermore you need to inform your visitors on the use of cookies.

How is using Google Analytics even possible in Germany?

you might ask. The answer is: It's difficult! First, you are supposed to have a special contract signed with Google. Secondly, you need to anonymize your visitors IPs because IPs are considered personal data here. Thirdly, you are supposed to give your visitors the opportunity to object and opt-out by simply not using your site or informing them about Google Analytics Opt-Out Browser Add-On.

But we would not be the overachievers that we are if we'd not be aiming even higher, right? We want our visitor to opt-out on Google Analytics with just one click of a button, don't we? Here's how to do that.

JavaScript File

Let us now take a look at the required JavaScript in case you are in a hurry.

You can copy the code above into a file, exchanging the UA-XXXXXXXX-Y to the proper gaProperty for your site. Load the file with every page of your site then. To perform the opt-out you'd need to call a javascript:gaOptout(); in the href of a link.

Walkthrough

We build the gaProperty and disableStr vars first for later use. We then check if the document has a specific cookie set with

document.cookie.indexOf(disableStr + '=true') > -1

If this is the case, we disable tracking for this particular page and this particular property with

window[disableStr] = true;

We had to do this because the ga-optout-property only works on pages, not on sites.

Next we have the gaOptout-function. This is what executes when the visitor opts out of Google Analytics Tracking i.e. clicks the respective link or button. Inside the function we

  1. write the cookie we had been checking for earlier
  2. disable tracking for the page
  3. alert the visitor that tracking has been successfully disabled

The rest is Google Analytics Business as usual. The closure loading analytics.js, creation of the ga-object with the correct property and sending the pageview to analytics for tracking. Worth mentioning is that we send a please-anonymize-ip-request alongside the pageview to Google because that's how we roll here in Germany.

WordPress Implementation

When you are planning to use this with WordPress, you should save it as /js/ga.js into your child theme. You then need to enqueue to load the script in your child theme's functions.php with

wp_enqueue_script( 'ga-js', get_stylesheet_directory_uri() . '/js/ga.js', array(), '1.0', true );

That should set you up for super Germany compatible Analytics Tracking without even installing a plugin.

Leave a Comment

Your email address will not be published. Required fields are marked *