Skip to content

Instantly share code, notes, and snippets.

@jinsley8
Created February 13, 2022 17:51
Show Gist options
  • Save jinsley8/35f936ded7ecbad9a667749367976bdb to your computer and use it in GitHub Desktop.
Save jinsley8/35f936ded7ecbad9a667749367976bdb to your computer and use it in GitHub Desktop.
Enqueue Google Analytics 4 script without tracking Admin
/**
* Enqueue Google Analytics 4 script
*
*/
/* Add Google Analytics 4 Site Tag as close to
the opening <head> tag as possible
=====================================================*/
define("GA4","G-XXXXXX", false); // Replace GA4 ID
function add_google_analytics() {
// disabled for Admin users
if ( !current_user_can( 'manage_options' ) ) {
?>
<!-- Start Google Analytics Script -->
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo GA4 ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', '<?php echo GA4 ?>');
</script>
<!-- End Google Analytics Script -->
<?php
}
}
add_action('wp_head', 'add_google_analytics');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment