Disable google analytics when logged as admin

Wordpress
2 minutes read

As the title suggests, I have no intention of tracking my admin activity on the page. The idea is to disable Google Analytics when the admin is logged in.

To simply disable Google Analytics, you just need to set the ‘ga-disable’ window variable to ‘true’.

I suppose this can be added anywhere on the page, but in my case, I’ve added the ‘disable’ attribute to the header tag.

Following script checks if the current user is an administrator on a WordPress site. If they are, it sets a JavaScript variable (ga-disable-GA-IDENTIFICATOR) to true, effectively disabling Google Analytics tracking for that user’s session on the page. This can be useful to prevent administrators from skewing website analytics when they are logged in and working on the site.

  <?php if (current_user_can('administrator')):?>
    <script>
        window['ga-disable-GA-IDENTIFICATOR'] = true;
    </script>
  <?php endif;?>
  <?php wp_head(); ?>
  1. <?php if (current_user_can('administrator')):?>: This line is a conditional statement in PHP. It checks if the current user has the role of “administrator” in WordPress. If the user is an administrator, the code within the if block will execute.
  2. <script> window['ga-disable-GA-IDENTIFICATOR'] = true; </script>: This is a JavaScript code snippet wrapped in <script> tags. It sets a JavaScript variable named ga-disable-GA-IDENTIFICATOR to true. This variable is used to disable Google Analytics tracking for the current page.
    • window['ga-disable-GA-IDENTIFICATOR'] refers to a JavaScript global object named window. This object is used to store variables that can be accessed throughout the web page.
    • 'ga-disable-GA-IDENTIFICATOR' is the name of the variable being created. This name is used as a reference to disable Google Analytics.
    • true is the value assigned to the variable, indicating that Google Analytics tracking should be disabled.
  3. <?php endif; ?>: This line ends the conditional statement started with if. If the current user is not an administrator, the code within the if block is skipped.
  4. <?php wp_head(); ?>: This line is a WordPress function call. It is typically included in the <head> section of a WordPress theme and is used to output various scripts, stylesheets, and other elements that should be loaded in the <head> of the HTML document. In this context, it’s used to ensure that the script defined above is included in the website’s HTML.
Fade article effect on wordpress post
Building the blog, Wordpress

Fade poat article effect in wordpress custom theme

3 minutes read

Adding visual interest and engaging design elements to your WordPress website is an essential part of creating a great user experience. One way to accomplish this is by adding a fade effect to your post articles, which can create an attractive and engaging visual transition for readers. In this blog post, we’ll show you how […]

Continue reading
Comment form with bootstrap
Building the blog, Wordpress

Style WordPress comments with bootstrap 5.1 framework

4 minutes read

With creating your own WordPress theme you will have to style comment section. For that purpose you will have to add a bit of own styling. In my case I used bootstrap CSS framework that makes life easier for programmers that are not that proficient with front end styling. This will make sure that your […]

Continue reading
Programing editor
Building the blog

Building blog from “scratch”

1 minute read

This is the first post from the series on how I built, or more precisely on how I will build this blog. Before you will start to build your own blog, well right after you chose your hosting provider and buy your domain, you will have to decide technology, CMS or blogging platform. Of course […]

Continue reading