X

How to Disable Google Analytics for Logged in Users in WordPress

Snippets by IsItWP

Are you using Google Analytics for tracking your audience? Then, you probably only want to track real visitors, and not your authors and writers.

The easiest way to track real visitors is by using MonsterInsights on your site, which is the best Google Analytics plugin for WordPress.

If you’d rather use a code snippet, then follow the below instructions. Keep in mind that this snippet automatically adds the Google Analytics code to your footer area (in case “wp_footer()” is called), so you don’t have to manually insert the Analytics code to all your files.

Instructions:

Place this snippet in your functions.php file located in your theme’s directory or in a site-specific plugin. You have to insert your Google Analytics ID at “XXXXXXXX“. Have fun with it!

<?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
   }
}
?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments   Leave a Reply

  1. José Carlos Amaral October 9, 2022 at 2:32 pm

    Will work with GA4?

  2. Is there a way to make this apply only to logged in administrators, while tracking other logged in users?

    1. Line 5 would need to be changed to:

      if ( !is_user_logged_in() || !current_user_can(‘administrator’)) {

  3. Hello,

    I tried this but it didn’t work. I added added this code using the Code Snippets plugin, adding wp_header instead of wp_footer. I set it to run across the entire site:

    var _gaq = _gaq || [];
    _gaq.push([‘_setAccount’, ‘UA-XXXXXX’]); // 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);
    })();

    <?php
    }
    }

    Have I done something wrong? Please help.

    1. It looks like the Google Analytics id is missing. UA-XXXXXX is just a placeholder. You may want to check out: https://www.isitwp.com/install-google-analytics-wordpress-site/

  4. Can I use this code instead?

    My google analytics code.

    <?php
    }
    }

    1. Yes, you would use your own Google Analytics ID.

  5. Thanks, It worked!

    1. Shahzad Saeed May 27, 2020 at 8:02 am

      Glad it helped 🙂

  6. Misleading title. Where is the part where you show how to block logged in users ?

    1. Line 5 sets of the conditional to not run the Google Analytics code if the user is logged in.

  7. Thanks this is very useful 🙂

    I used to do it all with tag manager but this is a much faster option!

    There is a new script now from Google so you may want to update your post. Also, I changed wp_footer to wp_header as Google prefers the script in the header.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!