background shape
background shape

Disable google analytics when logged in as admin

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.

Oh hi there 👋
I have a FREE e-book for you.

Sign up now to get an in-depth analysis of Adobe and Salesforce Marketing Clouds!

We don’t spam! Read our privacy policy for more info.

Share With Others

Leave a Comment

Your email address will not be published. Required fields are marked *

MarTech consultant

Marcel Szimonisz

Marcel Szimonisz

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms.

Buy me a coffee