A budget friendly way to disable Google Analytics on your site (Should your visitors want to)

19 Feb 2018
10 min read

So for those of us who are either companies based in Europe, or companies outside of Europe, we all know that on May 25th, 2018, the GDPR went into effect. If you’re not sure what GDPR is, just throw it quickly into Google and you’ll be greeted by an absolute plethora of hits on the topic, I’m guessing most of it will be some form of complaining about it. That is certainly where my thought process was.

I’m guessing most of it will be some form of complaining about it. That is certainly where my thought process was.

In a nutshell, the EU enacted its largest privacy enforcement act in the last twenty years. Some argue that nothing actually changed with the GDPR. Meaning that the laws were actually always there, but with the GDPR the fines for not being compliant were now much higher, sometimes in excess of 20 Million Euros. When you start throwing the phrase 20 Million Euros at people, they tend to start paying more attention. Other than that, it standardized the privacy protocols basically throughout the world. Until now in the EU, data collection and privacy online were a mess. Each country had its own definition of what should be done online in regards to privacy, and it was even worse for companies outside of the European Union, who tended to just blatantly ignore these regulations. Now the entire EU is in line with one governing protocol, and it is said that countries outside of the EU will also be held accountable for their actions, if they have dealings with customers inside of the EU.

(How I felt trying to prepare for the GDPR)

In my humble opinion, this is actually a huge win for everyone. Yes, the GDPR dumped a lot of sudden work on my palette, and those of you who follow my personal social channels know that preparing for this initiative brought me to the brink of insanity, but in the end it is all for the better good. People’s privacy and data should be respected, if they so choose for it to be. The best part is, if you’re following a true inbound marketing strategy I doubt you were actually heavily effected by the GDPR. You probably had to update some wording on your sites and channels, but otherwise you probably were already doing everything properly the first time around.

A small, but ever so necessary disclaimer

I’m not a lawyer. I wanted to be one at some point in my life, but as you can see I’m writing this blog article so that plan didn’t really come to fruition. Nothing in this article can be taken as any sort of legal advice to prepare for any sort of regulation or rulings in any part of the world. It’s simply a technical guide on how to do some cool things with Google Tag Manager and Google Analytics. Furthermore, I have only tested the upcoming process with Google Analytics. It make work with other tracking entities such as Facebook Pixel, but as of writing this guide I cannot promise it will function as desired.

All the steps you need to take to let users disable Google Analytics on your site

Why in the world would I want to disable Google Analytics (GA) on my site? Well, it’s a question I also myself frequently. I’m a performance and technical-based marketer, so it means I am a big fan of analytics and data. I don’t like to make marketing decisions without sufficient data, and GA is one of those tools, ESPECIALLY in the startup world, that is paramount in getting good user data from your site. In my opinion, every company in the world should be running GA on their sites.

But like I said, I’m also a big fan of user experience, and giving the user the option to do whatever they want. With all the new regulations coming out, some people have been tending to exaggerate the situation and desire things like this, so it’s about giving them the option.

Oddly enough, when it comes to regulations, I can’t actually find anything credible online (as of writing this article) that actually says this functionality is required per the GPDR, but it certainly cannot hurt it. Some people consider some of the information Google Analytics collects to be personal information (Such as IP, you should have your Analytics setup configured to anonymize all IP addresses) so going the extra distance here can help. I searched for a lot of tutorials online to accomplish this technical setup, but either none of them worked, or used old versions of the Google tools, or just plain sucked. Therefore we teamed up with our class 5 alumni flynex to see if we could develop our own solution. It seems to be working, so we decided to share it with the world.

This guide is intended for moderate to intense users of Google Analytics and Google Tag Manager, who have a basic grasp of debugging lines of code. If you’ve got questions, leave them in the comments below and I’ll be happy to take a look.

1. Setup Google Analytics with Google Tag Manager

This is the first step. Well actually I guess the first step would actually be using Google Analytics, but I had basically assumed that everyone reading this article would already be doing so. Anyways, I wrote a great article on why startups should be using Google Tag Manager (GTM) back in 2017, feel free to check it out if you wanna read about the benefits of it. If you want to check out a great guide on how to implement GTM, you can visit the official Google Documentation.

Once you’ve installed GTM, you can follow this tutorial to actually install GA via GTM. Don’t forget, once you deploy GA with GTM, you’ll want to remove the old code you had installed on your page otherwise you’ll have GA firing twice on every page load. That’s bad, don’t do that.

2. Start using Chrome and install Google Tag Assistant

Even if you don’t want to use it as your primary browser, I’d highly recommend downloading and installing Chrome just for troubleshooting purposes with the Google tool sets. It just works better, plus you can use the official extension Google Tag Assistant, which makes debugging a lot easier.

3. Create a new variable in GTM

Navigate to your variables in GTM, and scroll down to the user-defined variables and click the red “NEW” button. Name the variable “optoutstatus” and insert the following Javascript (JS) code into the box.

function() {
  return localStorage.getItem('optout')

The end result should look something like this:

How your variable setup should look in GTM

How your variable setup should look in GTM

4. Create a new trigger in GTM

Navigate to your triggers in GTM, and click the red “NEW” button. Give your trigger a name that will be very concise, I recommend something along the lines of, “All pages tracked, but user opt out for GA is allowed.” For the trigger type select “Page View and click the radial button for “Some Page Views.” Click on the first drop down menu, and within it you should see your new variable you just created in step 3, “optoutstatus.” Select it, and in the second drop down select “does not contain” and in the text box type in the word ”true” once you’re done, the setup should look like this:

The trigger settings for your new GTM trigger.

The trigger settings for your new GTM trigger.

5. Now we’ve got to install some code on your privacy policy page

This is where things may get a bit technical, but if you’ve got a basic understanding of HTML and access to your website’s CMS you’ll be just fine! But now we need to access your privacy policy page, we’re going to be installing the following code on it:

You can prevent Google Analytics from collecting your data by clicking on the following link:

<p>You can prevent Google Analytics from collecting your data by clicking on the following link:</p>
<p><span id="opt-btn">The webtracking on this page is
<script>// <![CDATA[
if (localStorage.getItem('optout')) {
  document.write('<span> deactivated. </span><button onclick="deleteOptOut()">Activate Now</button>');
} else {    
  document.write('<span> activated. </span><button onclick="setOptOut()">Deactivate Now</button>');
function setOptOut() {
  localStorage.setItem('optout', 'true');
function deleteOptOut() {
// ]]></script>

You can insert this code anywhere on your privacy policy, but there are two things you need to be aware of:

  1. The language can be customized. If you have a site that is not operating in English, you can edit the text in between the spans or paragraph tags in to the language of your choice. Just save the English version before you start going too crazy, so you have a backup you can revert to.
  2. This will install a button on your page.So you’ll need to place this code somewhere on the page where it makes sense. As an example, here is what it looks like on the SpinLab Privacy Policy.
This step will introduce a fully functioning button on to your privacy policy page.

This step will introduce a fully functioning button on to your privacy policy page.

6. Head back to GTM, and modify your GA Tag

Now we’ve gotta go back into GTM and change the trigger for your GA Tag. If you’ve had this previously installed, you are probably just using the default “All Pages” trigger for GA, because as we said, it makes sense for each page to be tracked, all the time.

But with privacy becoming more of an issue, we’re going to disrupt that a bit. Once you’re back in GTM, navigate back to your tags and choose to edit your GA Tag. Scroll down to the bottom, and you’ll see the trigger section. Remove the trigger that is currently there, and replace it with whatever trigger you setup in step 4.

Replace the trigger in the GA tag with whatever trigger you created in step 4.

Replace the trigger in the GA tag with whatever trigger you created in step 4.

7. Save everything in GTM, BUT DO NOT PUBLISH yet! We’ve gotta test it in preview mode

Preview mode is one of the greatest tools GTM can offer, if you’re not familiar with it yet, get familiar with it. Like I said, save, BUT DO NOT publish, and get into preview mode and then go visit your privacy policy page. If you followed all steps correctly, your page should look something like this:

Fresh reload, all analytics tags firing as normal.

Fresh reload, all analytics tags firing as normal.

Now click the deactivate button you’ve installed, the page should reload itself, and if everything is installed correctly, the analytics tags should no longer fire. Upon reload, the page should now look like this:

Notice the small differences in preview mode.

Notice the small differences in preview mode.

If your site’s performance is matching my setup here, at this point you can go ahead and publish your changes in GTM to take them live. Make sure you deactivate preview mode, otherwise you’ll still have that debugging module open whenever you visit your website.

8. Verify the functionality of the setup on the live site

I’m a sucker for double checking thing, so here is how you can verify that the production implementation is actually functioning, with Google Tag Assistant! Turn on Tag Assistant and load up your website again, and you should notice in the top right hand corner of Chrome your Google Tag Assistant should be displaying a little 2.

Just like with GTM preview, you can see what's happening with your Google scripts when you use tag assistant.

Just like with GTM preview, you can see what’s happening with your Google scripts when you use tag assistant.

Now just like with preview, click your deactivate button we’ve installed, and watch the behavior inside of Tag Assistant.

Yayyyy everything works

Yayyyy everything works

Wrapping it all up

A pretty technically heavy article this time around, but also important in our changing day and age. We’ve implemented this on all three sites we’re operating, and while I really don’t think anyone has ever utilized it, the option is at least there, and that is the main thing, we’re putting data back in the hands of the user, and no longer assuming consent from them online.

I hope this has helped you guys implemented this on your own sites, if you’ve got any questions at all, please let me know in the comments below.

About the author

Shawn Segundo is the marketing manager at the SpinLab. He’s worked extensively with all company sizes from startups to enterprise clients while working as an online marketing consultant in both the United States and the European Union. Follow him on Twitter @shawn2nd

Follow the SpinLab on Social

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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