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="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'YOUR_TRACKING_ID'); </script> <!-- End Google Analytics --> <?php } add_action('wp_head', 'snn_add_google_analytics');