Code to Disable Google Analytics for Logged In Users in WordPress

/* Disable Google Analytics for Logged In Users */ 
add_action('wp_head', 'wpflt_analytics_script_logged_out_users');
function wpflt_analytics_script_logged_out_users() {
 if ( !is_user_logged_in() ) { ?>		
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DUMMYCODE"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-DUMMYCODE');
</script>
  <?php
 }}

Add this snippet to your child theme’s functions.php to disable Analytics script for logged in users. We as admins spend a lot of time on our sites creating content. Analytics tracking our own data creates confusions as who are the real users.

With this code added, analytics runs only for users who are logged out, meaning, it runs only for our visitors.