How to let Visitors Opt Out on Google Analytics Tracking

Cookie Monster
Image | CC0 pixabay

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

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.


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.


Share on facebook
Share on twitter
Share on linkedin

Related Posts

WooCommerce SQL

Howto Manipulate WooCommerce Metadata via SQL

With Germanized on board, WooCommerce has several ways to save article numbers. Has the intern spent two days entering the EAN in the wrong field for the 3000 articles in the WooCommerce online shop? Then an SQL superhero has to save the day who can copy content from one field to another in no time.

eCommerce Sales

5 eCommerce Lockdown Tips

eCommerce is booming because since Corona everyone understands the importance of online stores and digitization. We have been supporting online stores since the beginning of the millennium, when you still had to program them yourself, and we would like to tell you here how your eCommerce tools can make good sales and keep you afloat in a crisis.