Last updated on September 13, 2023

Add Google Analytics without a plugin

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add Google Analytics scripts without a plugin.

If you want to add Google Analytics to your WordPress site without using a plugin, you can use this snippet:

<?php

add_action( 'wp_head', 'wpsnippets_google_analytics_scripts' );

function wpsnippets_google_analytics_scripts() {

   // Add your Google analytics scripts here, like so:
   ?><script>...</script><?php
    
}

If you need to add code after the <body> opening tag as well, you can use the snippet below:

<?php

add_action( 'wp_body_open', 'wpsnippets_google_analytics_body' );

function wpsnippets_google_analytics_body() {

   // Add your Google analytics scripts here, like so:
   ?><script>...</script><?php
    
}
Last updated on September 13, 2023. Originally posted on May 8, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *