I know this post is super old, but none of the solutions met my needs. Not only did I want to remove dev work from GA (and FB), but I also wanted to have some folks within the company not be counted in GA and FB. So I wanted a relatively easy method for those folks to exclude themselves from analytics without a plugin, or ruling out a domain ip (as folks with laptops wander).
I created a webpage that users can go to and click a link to opt out of the GA and FB tracking. It places a cookie for the site. Then I check that cookie to determine if we should send data to GA and FB.
I originally set this up on a site for called Dahlia, which is a boutique maker of items for Greek Orthodox Weddings and Baptisms.
Here's the code:
I put the following code in the header for all web pages:
<script>
//put in your google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';
// Disable tracking if the opt-out cookie exists.
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
window['ga-disable-UA-7870337-1'] = true; //This disables the tracking on Weebly too.
} else {
//put in your facebook tracking id below:
fbq('init', 'YYYYYYYYYYYYYYY');
fbq('track', 'PageView');
}
</script>
Be sure to add your GA and FB tracking IDs in the spaces provided. This was originally written for a Weebly (shopping CMS) site. So if you are not on Weebly you can remove the line that mentions weebly.
Then I created a new webpage called "do-not-track" with the following code in the header:
<script>
//put in your own google analytics tracking id below:
var gaProperty = 'UA-XXXXXXXX-X';
var disableStr = 'ga-disable-' + gaProperty;
// Opt-out function
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
gaOptoutCheck();
}
// Check Opt-out function
function gaOptoutCheck() {
var name = "ga-disable-"+gaProperty+"=";
var ca = document.cookie.split(';');
var found = "false";
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) found = "true";
}
if (found == "true") alert("Cookie is properly installed");
else alert("COOKIE NOT FOUND");
}
</script>
And the following code in the body:
<a href="javascript:gaOptout()">Click here to opt-out of Google and Facebook Analytics</a>
<br><br>
Please visit this page on every computer, laptop, phone, tablet, etc. that you use;
and for all browser you use on each of those devices.
<br><br>
If you ever remove cookies from browser, you will need to repeat this process for that browser.
<br><br><br>
<a href="javascript:gaOptoutCheck()">
Click to check if cookie is set</a>
<br><br>
Here is my full writeup for the Weebly site
Hope this helps somebody!