<?php

/*
Plugin Name: CodeWP Google Analytics
Plugin URI: https://codewp.ai
Description: This plugin adds Google Analytics GA4 tracking code to your WordPress website.
Version: 1.0
Author: CodeWP Assistant
Author URI: https://codewp.ai
License: GPL2 or later
Text Domain: codewp
*/

class CodeWPGoogleAnalytics {
    /**
     * Google Analytics GA4 Measurement ID
     *
     * @var string
     */
    private $measurement_id = 'G-XXXXXXXXXX'; // Replace with your Google Analytics GA4 Measurement ID

    /**
     * Initialize the class
     */
    public function __construct() {
        add_action('wp_head', [$this, 'insert_ga4_tracking_code']);
    }

    /**
     * Insert Google Analytics GA4 Tracking Code into the head of the document
     */
    public function insert_ga4_tracking_code() {
        ?>
        <!-- Google Analytics GA4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_js($this->measurement_id); ?>"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', '<?php echo esc_js($this->measurement_id); ?>');
</script>
        <?php
    }
}

new CodeWPGoogleAnalytics();