Saturday, October 3, 2020

Google Analytics - Easiest way of not counting your page views

There are many ways of how not to count your own pageviews (or page views) into the Google Analytics (GA) statistics of your web site. Most of them are described for example in 5 ways to exclude your own visits from Google Analytics.

Let's have a look at the way I find myself one of the easiest ones for individual users and for sites that you can't directly control - like this Blogger site. As a side effect, we will also learn some basics of the Tampermonkey browser extension.

How to do it generally

In order to block your own visits from GA, you don't need to setup special IP-based or cookie-based filters in the GA administration, nor you don't need to install any fancy extension to your web browser.

All we have to do is to create a simple JavaScript snippet for your browser that will set a page variable called "ga-disable-UA-XXXXX-Y" to "true" before the GA's JavaScript code is run within any visited page (see Opt-out of measurement for your site for documentation on this). The snippet basically needs to look like this:

window['ga-disable-UA-XXXXX-Y'] = true;

You only need to replace "XXXXX-Y" with the GA ID assigned to your site.

How to do it via Tampermonkey

In order to be able to run our code, we need to have an extension installed in our browser. We will use the well known Tampermonkey here - just follow the instructions on its home page if you don't have this extension installed yet.

We click the Tampermonkey toolbar icon and then select "Create a new script..." from the popup menu. An editor appears that lets us edit our new script (called "userscript"):

Tampermonkey: Create a new script.

Next, we change the "@name" and optionally also other metadata of the script to whatever we like. We also need to edit the "@match" line, so that the script is run on all pages of our site (note that an ending "*" is necessary!). And of course, we also add the line mentioned above. Then we save the script.

The resulting script looks for example like the following, for this Blogger site:


// ==UserScript==
// @name         Block GA on my pages
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  try to take over the world!
// @author       You
// @match        https://programmedbycoincidence.blogspot.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    window['ga-disable-UA-158769633-1'] = true;
    console.info('Disabled GA');
})();

Finally, we need to ensure the script is run before the page gets rendered. We click on the "Settings" tab and choose "document-start" from the "Run at" combobox:

Tampermonkey: Run our script at document start (the other fields shown are not relevant).

Now when we visit our site, we should see that our script was executed - both from the Tampermonkey toolbar icon indicating one userscript was executed, as well as from the console output:

Seeing that our script was run on a visited site page.

As the last step, we should also see our GA console and make sure our visits are no longer counted.

When we have more sites

If we need to block another site, we just add another "@match" line with an expression matching that site and another "window['ga-disable-UA-XXXXX-Y'] = true;" line to the script.

And that's all, folks. :)