Lorenzo Vainigli

Software developer, passionate about programming

Disable Google Analytics only for site’s administrators

28 July 2019
  1 min.     19

Questo articolo รจ disponibile in italiano

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?

Using cookies

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
if (document.cookie.indexOf("no-tracking") === -1){
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-XXXXXXXX-X');

The cookie can also be set manually in the administrator’s browser.

Once this operation has been carried out, Google Analytics will only track the real users who visit the site, making the collected data much more reliable.

Hi ๐Ÿ‘‹, I'm Lorenzo ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป

๐Ÿ‡ฎ๐Ÿ‡น Italian
๐Ÿ’ผ Software developer
๐ŸŽ“ Master's degree in computer science

Tech interests
๐Ÿค– Artificial intelligence
๐Ÿง  Machine learning
๐Ÿ“Š Data Analysis
๐ŸŒ Web development
๐Ÿ“ฑ Android development
Other interests
๐Ÿช Astronomy
๐Ÿ‡ฏ๐Ÿ‡ต Japanese culture
๐Ÿฅ‹ Martial arts
๐ŸŽฎ Videogames
๐Ÿ“ธ Photography

More about me