BrowserBugs.

How to hide from your own Analytics

A simple way to make your visitor data drop.
by Tim Bridges.

I thought I’d keep up with my one a year article by tackling an age-old problem that often I don't see being addressed, the elephant in the room, and that’s our own site visits trampling all over the analytics data. From SEOs to Developers, Site Admins, Sales Reps, Site Owners and more, anyone working with a live website will always be inflating the data.

You might be thinking we can filter this out by IP, and you’d be right, so long as we know all the possible IP addresses that are being used so we can filter, but if the pandemic has taught us anything it’s that people can work from anywhere.

Here’s an example, Ann is in Sales, and part of her job is to check the products on the live site. Whilst working in the office we know the IP address and can filter her out. However, Ann decides to take the laptop to a local coffee shop to work, or maybe goes to stay with a family member and work from there for a few days, or has a meeting with a client to showcase the new products on the website.

So how do we filter Ann in these situations? Does anyone know the IP address for Best Beans Coffee Shop or her Mums house? Probably not, but there is a way we can tell it's Ann, and so I present Stealth Mode!

Klingon Bird of Prey Ship
Stealth: Disengaged

How does Stealth Mode work?

Stealth Mode is a simple concept that revolves around 1 persistent cookie and a Boolean variable. For this demo I’m using the following code with a simple form to set a cookie called ‘Stealth’, to hide you I set this to ‘engaged’, or to become visible again I set it to ‘disengaged’, and yes, I like my Sci-Fi.

It's worth noting that the toggle form is optional. There are many opportunities to set cookies via another route, like when an admin signs in, or via a link with a parameter. The choice is yours, at the end of the day we just need a cookie to read in order to know someone wants to be hidden.

Setting Cookies with a simple Toggle Form (PHP)
<?php
$stealth_mode = false; // Default we're not in stealth.
if($_COOKIE['Stealth']=="engaged") { // If our stealth cookie is set to engaged.
    $stealth_mode = true;
    // Optional: Extend the engaged stealth cookie for another 1 year.
    setcookie("Stealth", "engaged", time() + (86400 * 365), "/", "browserbugs.co.uk", true, true);
}
// Simple form to engage or disengage stealth.
if($_SERVER['REQUEST_METHOD']=="POST") { // If the form was posted.
    if($_POST['stealth']=="engage") { // We want to engage.
        // Set the stealth cookie to 'engaged' from now + 1 year.
        setcookie("Stealth", "engaged", time() + (86400 * 365), "/", "browserbugs.co.uk", true, true);
    } else { // Else set the stealth cookie to 'disengaged' for 1 second.
        setcookie("Stealth", "disengaged", time() + (1 * 1), "/", "browserbugs.co.uk", true, true);
    }
    // Redirect back to this page so I can read the new cookie (if you have them enabled).
    header("Location: /how-to-hide-from-your-own-analytics");
    exit();	    
} ?>
<!DOCTYPE html>
<html>
<head>
<title>How to hide from your own Analytics | BrowserBugs</title>
</head>
<body>
<form method="post">
<?php if($stealth_mode) { 
// Stealth cookie is 'engaged', show the disengage button to turn it off.?>
<button name="stealth" value="disengage" type="submit">Disengage</button>
<?php } else { // Else the Stealth cookie is not 'engaged', show the engage button. ?>
<button name="stealth" value="engage" type="submit">Engage</button>
<?php } ?>
</form>
</body>
</html>

PHP Resources

  • setcookie() - Defines a cookie to be sent along with the rest of the HTTP headers. (PHP 4, PHP 5, PHP 7, PHP 8)
  • Booleans - This is the simplest type. A bool expresses a truth value. It can be either true or false.

Utilising Stealth Mode

If cookies are enabled we should now have ourselves a freshly baked stealth cookie, now what?

Let's take a prime example with Google Analytics, in the Set up the Analytics global site tag support article, step 2 says Copy and paste the global site tag after the opening <head> tag on each page you want to measure, however I think they forgot '… for the people you want to track'.

To utilise Stealth Mode, we simply use the following code on all the pages we would normally implement analytics on, or any other tracking we might want to hide from. By default, we want to track as normal so we set $show_analytics to true and load analytics as normal.

However, when we find that the browsers Stealth cookie is set to engaged, we no longer want to track, so we switch $show_analytics to false which will prevent it loading in the first place. And, if we're not loading it then the person, like Ann, won't be counted in our data, regardless of what IP they are on.

Cookie Check with Boolean to Show Analytics (PHP)
<?php
$show_analytics = true; // Default we want to include analytics.
if($_COOKIE['Stealth']=="engaged") { // If stealth cookie is set to engaged.
    $show_analytics = false; // Stealth mode is engaged, so we want to hide analytics.
    // Optional: Extend the engaged stealth cookie for another 1 year from now.
    setcookie("Stealth", "engaged", time() + (86400 * 365), "/", "browserbugs.co.uk", true, true);
} ?>
<!DOCTYPE html>
<html>
<head>
<title>Any page on your website where you normally track | BrowserBugs</title>
<?php if($show_analytics) { // If $show_analytics is true we load the your usual head tracking. ?>
<!-- All your lovely tracking stuff in the head might go here -->
<?php } ?>
</head>
<body>
<h1>Any page on your website where you normally track</h1>
…
<?php if($show_analytics) { // Might have some tracking here, $show_analytics is still true! ?>
<!-- Some cool footer tracking stuff you've made lazy goes in here -->
<?php } ?>
</body>
</html>

Final Thoughts

This is just one way that we can keep our data as accurate as possible when remote working and agency collaboration can impact our analytical data. Yes, there's the possibility they clear their cookies, however cookies can be reset, and at least it's not IP dependent when switching networks. The demo is a simplified approach, written so that you can see the theory, have a play, adapt for your own use, and take it further.

Will your traffic reports drop? I hope so. Through implementing this approach on projects I have seen drops, especially smaller businesses for dwell time, repeat visits etc. But then again, the traffic remaining doesn’t include Ann working from her favourite coffee shop, so we have a clearer picture of our real visitor data.

I hope this helps some of you explore your options, to boldly hide so you don't fluff your own data.