Add Google Analytics Script to WordPress through functions.php

/* Analytics Script for WordPress */
add_action('wp_head','wpflt_ga_analytics', 20);
function wpflt_ga_analytics() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<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 code to your child theme’s functions.php to install the Analytics script for all users. Replace DUMMYCODE in the above code with your measurement ID.

If you want to disable Google Analytics for Logged in users, follow this link to run analytics script only for visitors.