add_ga_head.php

CodeWP Google Analytics Integration

The CodeWP Google Analytics plugin provides a seamless way to add Google Analytics GA4 tracking code to your WordPress site. It leverages the WordPress action hook system to insert the necessary scripts into the header of your website, thus enabling GA4 tracking on all pages.

<?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();

Frequently Asked Questions

This plugin automatically integrates Google Analytics GA4 with your WordPress site.