28 July 2019
Programming Web
1 min.
16

Services used to track visitors to your site, such as Google Analytics, are essential tools to get an idea of the performance of your web pages.

The problem that arises is that administrators, content creators or any other profile that manages the content of a site are among the most active users. In the long run, this activity, which Analytics catalogues as “visitor”, without distinguishing between users and administrators, falsifies the numbers of real users, i.e. those who have no interest in the growth or decrease in numbers of a site.

This is very important, especially if the numbers of visitors will be the basis for the choice of marketing strategies.

How to do that?

To track visitors Analytics uses a short Javascript code like this one, included on every single page of the site that each visitor’s browser runs:

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-X');


Quite simply, we can define a cookie to be set in the administrator’s browser (for example no-tracking) and, with a conditional command, tell the code to execute the previous instructions only if the cookie is not present. The value of the cookie is therefore not relevant.

// indexOf(str) returns -1 if there is no occurrence of string str,
// the index of the first occurrence of str otherwise
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXX-X');
}