Disable google analytics when logged in 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 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
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
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
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