Disable google analytics when logged in as admin

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')):?>
        window['ga-disable-GA-IDENTIFICATOR'] = true;
  <?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.

#JavaScript #php #wordpress
Marcel Szimonisz
Marcel Szimonisz
MarTech consultant As a marketing automation consultant, I specialize in problem-solving, process automation, and driving innovation for clients' marketing platforms.

I hold certifications in Adobe Campaign v6 (3x certified) and Salesforce Marketing Cloud (5x certified), as well as 1x Salesforce Associate certified.

Moreover, I serve as a community advisor for Adobe Campaign, providing expert insights and guidance.

Beyond my professional pursuits, I explore various programming languages, CMSs, and frameworks, enhancing my technical expertise and staying at the forefront of industry advancements.
Take a look at our subscription offering in case you are looking for a Marketing Automation Consultant.

Leave a comment

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

Similar posts that you may find useful

Wordpress CMS
Building the blog, Wordpress

Adding SEO and site maps to search console

less than a minute read

I have found one plugin that meets all my requirements and those are All three were satisfied with The SEO Framework. But also with this plugin I had some struggles, later I found same problems one may have with all other SEO plugins. The problem I had was that I was not able to read […]

Continue reading
Fade article effect on wordpress post
Building the blog, Wordpress

Fade post article effect in wordpress 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
Computer workplace
Building the blog, Wordpress

Beware! Work in progress

1 minute read

Page is in the works and, therefore you might see it changing time to time or perhaps sometimes experiencing weird errors. It’s all good it’s part of this project and the journey to get a blog and I am on the road to document it all. February 2022 Recaptcha v3 updated as spam still happened […]

Continue reading
Building the blog, Wordpress

Add reCAPTCHA to wordpress page form

4 minutes read

As owner of a blog that is around for a while with any traffic, you will find sooner or later annoying amount of spam comments taking your blog by storm. If the akismet is not your cup of tea and you do not want to pay for possible license (commercial use), then I can show […]

Continue reading
Comment form with bootstrap
Building the blog, Wordpress

Style WordPress comments with bootstrap

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