<?php
/**
 * Analytics and cookie consent.
 *
 * GA4 placeholder — replace G-XXXXXXXXXX with a real Measurement ID
 * when deploying to production.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

/**
 * Output GA4 placeholder script in the head.
 *
 * Only loads if cookies have been accepted (GDPR compliance).
 * Replace the measurement ID with a real one in production.
 */
function northfield_ga4_placeholder() {
	// Only load analytics if cookies accepted.
	if ( ! isset( $_COOKIE['northfield_cookies_accepted'] ) ) {
		return;
	}

	// Don't track logged-in admins.
	if ( current_user_can( 'manage_options' ) ) {
		return;
	}

	$measurement_id = 'G-XXXXXXXXXX'; // Replace with real GA4 Measurement ID.
	?>
	<!-- Google Analytics 4 -->
	<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( $measurement_id ); ?>"></script>
	<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());
		gtag('config', '<?php echo esc_js( $measurement_id ); ?>');
	</script>
	<?php
}
add_action( 'wp_head', 'northfield_ga4_placeholder', 5 );
