Marketing Blog AZ

Exclude WordPress Admin Users from Google Analytics Tracking

Published: Updated: Author:
Contents

You may well want to use Google Analytics to measure your website visitors and collect valuable traffic data. You certainly do not want to measure your own visits, nor that of your WordPress authors and editors and contributors.

To exclude all WordPress admins from Google Analytics tracking when they log into your WordPress admin dashboard, you cannot depend solely on the default IP filter offered by Google Analytics. This is because some ISPs do not use static IP addresses. Instead, your IP address may change every time you connect, sometimes daily.

For that reason, the best way of excluding admin traffic is to use a plugin that includes this feature (or custom code if you prefer).

Disable Google Analytics for WordPress Admin Users
Disable Google Analytics for WordPress Admin Users

Disable Google Analytics for Logged-in Users in WordPress Using Code

First delete the standard Google Analytics code you placed in the blog/website. This avoids ending up with two tracking codes, which would result in incorrect analytics data. When you use the code snippets below your Google Analytics tracking code will be added to the footer via wp_footer when visitors hit your website.

You can use any of the following code snippets.

<?php
// function for inserting Google Analytics into the wp_head
add_action('wp_footer', 'ga');
function ga() {
   if ( !is_user_logged_in() ) { // not for logged in users
?>
    <script type="text/javascript">
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXXX']); // insert your Google Analytics id here
      _gaq.push(['_trackPageview']);
      _gaq.push(['_trackPageLoadTime']);
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
<?php
   }
}
?>

For the two code snippets below place before the closing tag </body>

<?php if ( !is_user_logged_in()) { ?>
<script type="text/javascript">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-26575989-48"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-xxxxxxxxx');
</script>
  <?php } ?>
<?php if ( !current_user_can('edit_posts') ) { ?>
<script type="text/javascript">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-26575989-48"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-xxxxxxxxx');
</script>
<?php } ?>

Add code above to your theme’s functions.php file of your WordPress theme.Then, replace UA-XXXXXXXX with your Google Analytics tracking ID.

Disable Google Analytics JS in WordPress When Admins Are Logged In Using a WP Plugin

If you do not want to use code to disable Google Analytics for logged-in users in WordPress follow the steps above and use a plugin instead.

Below are some of the plugins that have this helpful feature.

There’re a lot of different plugins that help you exclude all WordPress admin users from GA tracking for if/when they login to the WordPress admin dashboard, but usually not the only feature these type of plugins have. As well as this, they can have other features that help better view statistics, and make it easier to analyze a website.

Plugins like MonsterInsights a plugin that bring analytics reports, data tracking, and basic performance insights from yours directly into your WordPress dashboard from Google. The best thing is they also have a feature to filter out admin traffic, however MonsterInsights can be a bit heavy and slow your site down on low-performance hosting, and plus you have to pay for the premium first with at least $99 a year.

See the MonsterInsights guide: How to Stop Google Analytics from Tracking Logged In Users in WordPress.

If all you need is the feature that disable GA JS in WordPress when admin is logged in then I prefer Shooting – Flying Analytics by WP Speed Matters Settings plugin, it can help optimise your site speed for better loading of 3rd party JS like Google analytics js. Just select Disable for Logged in admins and this will exclude all WordPress admin users from Google analytics tracking if/when they login to the WordPress admin dashboard.

I’m using Flying Analytics by WP Speed Matters Settings at the moment (and loving it), don’t forget to give it a 5-star rating if you like it!

Reference source code: Shared by the IsItWP blog.

Share

Share this blog
Alex Min

Author

Alex Min

WAlex Min is a Digital Marketing specialist with strengths in website development, sales advertising campaigns, and online growth strategy. I am currently working as a marketing specialist for a B2B company in the industrial sector, focusing on improving communication effectiveness with factories, attracting potential customers, and supporting sales growth.

Keep reading

Related Posts
How to add h1 tag in wordpress
WordPress How to add h1 tag in wordpress​ (homepage​ , post...)

Adding an H1 tag to a WordPress homepage makes it easier to optimize the homepage SEO for websites built with WordPress core. In this article, I’ll show you how to add an H1 tag in WordPress when any type of page is missing this tag.