Privacy is more and more important to visitors and especially in Europe, privacy laws are very strict. Did you know that you even have to offer a link to allow your visitors to opt out from Google Analytics tracking directly on your site? So, in this post I am going to show you how to implement and use such a link.
Opt-Out methods described in this article
- opt-out methods for visitors
- code in theme/php files
- code in WordPress (functions.php + hook)
- code when using Google Analytics for WordPress plugin by Yoast
- WordPress plugin without coding
How visitors can opt-out from Analytics
The easiest solution to not send your visitors data to Analytics is simply by not using it on your site. But, of course, that is not the solution I am going to discuss here. This leaves solutions either on the visitors or the webmasters end.
Opting-out using a browser-plugin
The solution to opt-out from Analytics as a visitor is offered by Google itself in form of a browser plugin. You only have to activate it in order to not get tracked on any site using Google Analytics. You find it here.
Also a lot of AdBlockers or other browser plugins might have this feature included.
However, this can not serve as a general solution, because some browsers are not compatible with such a plugin, especially those on mobile devices.
Opting-out on a specific site
The second solution is in the hands of the webmaster, who has to offer a link to opt out from Analytics on his site. As far as I found information on this topic, it is enough to offer such a link in your privacy policy, so tracking visitors when they come for the first time seems inevitable. I am not a lawyer and found very different arguments for very different (European) countries on this matter, so please leave a comment in case you know something specific.
For now, I will concentrate on the solution on how to offer this opt out link and how to implement it on your site. My tutorial is written for WordPress, but you can probably use a lot from it for other cms as well.
Add an opt-out-link to your privacy policy
Privacy Policy?
The correct place for an opt-out link is the privacy policy of your site. I still see many sites without one, but as far as I know, most countries bound you to have one. Your privacy policy page needs to be accessable from every page, so best include the link into your footer or sidebar navigation.
There are plenty of tutorials and services to create a propper privacy policy. For small sites and the basic features they are also for free or ask for a backlink.
Opt-Out Text
When you are using Analytics and have a proper privacy policy you will already inform your visitors about it. So there is only a short sentence to add to also inform visitors about the opt-out link. It can look like the following.
You can opt-out from Analytics tracking by clicking on this link: (Link).
I am open for a better wording, so please feel free to suggest something in the comments below.
Opt-Out Link
You can take the script for the opt-out link directly from the example on the Analytics developer forum.
<a href="javascript:gaOptout()">Click here to opt-out of Google Analytics</a>
Code language: HTML, XML (xml)
Make sure to edit your page in the text editor and not the visual editor so the html code is working.
Opt-Out Script
The function that is called with the Analytics opt-out link does currently not exist. You need to insert it into your website’s code before the actual Analytics code. This is very often the header, but can also be in the footer.
The base code and how to implement it into your theme files
The basic code for the opt out looks like this:
<script>
// Set to the same value as the web property used on the site
var gaProperty = 'UA-XXXX-Y';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script>
Code language: HTML, XML (xml)
To make it work you need to change the ua code in the third line into the one from your Google Analytics account.
The code will simply create a cookie that expires in 2100, so it practically works forever or until the visitor cleans the cookies in his browser.
If you implemented the Analytics code into your WordPress theme, you need to find the file where you put it first. Then simply copy the code from above and put it above the Analytics code. Don’t forget to change the UA code to the one from your Analytics account.
Implementing the code in WordPress’ functions.php
If you inserted the Analytics code in WordPress with the help of a plugin you can put the following code into the functions.php file of your theme (if you use Google Analytics by Yoast see the following section).
add_action( 'wp_head', 'my_opt_out', 1 );
function my_opt_out(){
?><script>
var gaProperty = 'UA-XXX-X';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script><?php
}
Code language: JavaScript (javascript)
This code will put the code into your header. Don’t forget to insert the correct UA code.
Opt-Out code with Google Analytics for WordPress by Yoast plugin
If you are using the Google Analytics for WordPress plugin by Yoast, you can even get the UA code from it. This is especially helpful when you run a multisite and every single site has its own tracking code. Simply include the following code into the functions.php file of your theme.
add_action( 'wp_head', 'wg_opt_out', 5 );
function wg_opt_out(){
if ( ! class_exists( 'Yoast_GA_Options' ) ) return;
$yoast_ga_options = Yoast_GA_Options::instance();
$code = $yoast_ga_options->get_tracking_code();
?><script>
var gaProperty = '<?php echo $code; ?>';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
}
</script><?php
}
Code language: JavaScript (javascript)
Just a word of caution: after I wrote the first version of this code the Analytics plugin was updated, a global variable I used was gone and my site crashed. This code works, but of course I can’t guarantee for how long.
If you use another Analytics plugin and know how it stores the UA code, you can manipulate the first lines accordingly. Use the third variable in the first line (“5” in my example) to play with the position of the code in case it is inserted below the Analytics code and therefore doesn’t work.
Is there a plugin for that?
For those of you who are not comfortable with hacking their theme files or who don’t have access to them, there is also a very powerful plugin that can help. It is simply called the Google Analytics Opt-Out WordPress Plugin.
The plugin also works with Yoast’s Analytics plugin and – what I like – doesn’t bother users who already clicked on the link.
Testing the code
Testing the Analytics opt-out is rather simple if you know how to check the cookies set in your browser. I am using the web developer toolbar for Firefox, but there are plenty of other ways to check.
Now, simply click on the link you set in your privacy policy and check for a ga-disable cookie like this one:
You can simply delete it if you want to enable tracking for yourself again.
Important notes
There are some limitations to the code given, but let me just copy what Analytics has to say about it:
This example code assumes that you are using a single web property on your site and are only using a single domain. It only provides an opt-out function which is based on a long-term cookie. If you require opt-in functionality or if your site uses multiple web properties or domains, you will need to modify this example code, write your own opt-out code, or use other opt-out tools.
The whole topic of data usage and privacy is very complex. However, I hope I was able to help you make your site more complient with local laws. If you feel like I forgot something or you want to add an important note, don’t hesitate leaving a comment below.
Hi Thomas,
thank you for the detailed guide. but i still have troubles to bind in the opt out link in the privacy policy. the link seems dead on my page 🙁
Hi, what do you mean by “dead”? Thanks, Thomas
Hey Thomas,
you’ve written the following:
“Opt-Out Script
The function that is called with the Analytics opt-out link does currently not exist. You need to insert it into your website’s code before the actual AdSense code. This is very often the header, but can also be in the footer.”
I think you mean: “(…) insert it into your website’s code before the actual ANALYTICS code (…)”
Is that possible?
With kind regards from Germany,
Vince
P.s. For f1lz’s Comment: I think he means “dead” because it appears that nothing happens, when you click the link. f1lz needs to know, that he should look for the placed cookie…
Hi Vince, thanks for catching this! I am indeed so much working with AdSense that it is often the only thing I think about 🙂
Thank you for the great article Thomas! It is a very helpful summary of the whole GA implementation process.
I recommendt to add the following command to the out-out link as well to give the visitor some feedback:
Click here to opt-out of Google Analytics
Hi Thomas,
is it possible to put more than one GA property in the opt-out script?
Daniel
Sorry, but I have no idea if that is possible.
I just wondered, since Google recommends using a 2nd property to track AMP versions. To provide an opt-out from that you would have to put that property into the code, too.
I’ll try…
As a user, I would rather want to block the ga.js script code from being executed in the first place than rely on any opt-out cookie or shady browser plugin from Google itself (why trust them to respect my wish not to be tracked if I don’t trust them at all regarding any privacy issue??). Any chance that one can do this in Firefox without having to disable JavaScript entirely?
Furthermore, you wrote: “The easiest solution to not send your visitors data to Analytics is simply by not using it on your site. But, of course, that is not the solution I am going to discuss here.”
That would be my preferred solution. Period. Why are so many website owners using this Analytics crap anyway? Because it makes things so much easier? Have the 2013 Snowden revelations really diminished that much in everyone’s minds? Forgot that Google is one of the major providers for Prism?
Hi Gabi, thank you for your thoughts on this. There are add-ons for Firefox that block ga.js from being executed. As an answer to your question, why many publishers are using Analytics: it is powerful, simple and doesn’t need much to be set up. Thomas
I’ve followed your instructions above , but am curious what should actually happen from a user perspective when they click the opt out link?
Hello are there any opt out codes for multiple properties
Hi Patrik, I am not completely sure, but I think you can only opt out of Analytics completely with one solution. I think there was a browser extension for this as well.
Hi –
Not too saving in updating javascript, but is there a way to add some code that will send the user to a confirmation page after clicking the button. When I click the button that I am testing it doesn’t show anything as far as a success.
Thanks!
Hi Jason, it should be possible to load another page. You’d just need to change the code accordingly. Unfortunately, I have other priorities to work on and can’t provide that change.