Add Google Analytics only for customers

<?php
function site_inline_scripts() {
	global $current_user;
	$property_ga4 = "CONTAINER";
	$staging = false;
	if($_SERVER['HTTP_HOST']!="domain.com"){
		$property_ga4 = "CONTAINER2";
		$staging = true;
	}
	if ( !isset( $current_user->roles[0] ) || $current_user->roles[0] == 'customer' || $staging == true) {
		<!-- Google Tag Manager -->
		<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
	j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
	'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
	})(window,document,'script','dataLayer','<?php echo $property_ga4; ?>');</script>
		<!-- End Google Tag Manager -->
	}
}
add_action('wp_footer', 'site_inline_scripts', 17);

Leave a Reply

Your email address will not be published. Required fields are marked *