Created
February 13, 2022 17:51
-
-
Save jinsley8/35f936ded7ecbad9a667749367976bdb to your computer and use it in GitHub Desktop.
Enqueue Google Analytics 4 script without tracking Admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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