sinanisler logo

Integrating Google Analytics 4 into WordPress

This snippet is essential for WordPress site owners who want to integrate Google Analytics without using a plugin. By hooking into the wp_head action, the snippet ensures that the Google Analytics tracking code is added to the <head> section of every page. Just remember to replace YOUR_TRACKING_ID with your actual Google Analytics Tracking ID in the provided code. This approach is clean and efficient, especially if you prefer to avoid additional plugins for script integrations

function snn_add_google_analytics() {
    <!-- Google Analytics -->
    <script async src=""></script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'YOUR_TRACKING_ID');
    <!-- End Google Analytics -->
add_action('wp_head', 'snn_add_google_analytics');

Leave the first comment