Add Google Analytics to WordPress without a plugin

Here is a simple code snippet that you can use to add the Google Analytics tracking code or any other tracking code to your WordPress website without using a plugin.

To make this work all you have to do is copy the snippet below and paste your Google Analytics code just below the line where it says // Your Google Analytics Code Here and above the line // End Google Analytics code.

// Insert Google Analytics Code To WP Header
add_action('wp_head', 'head_google_analytics');

function head_google_analytics() { ?>
// Your Google Analytics Code Here

// End Google Analytics Code 
<?php }

To add the tracking code to your website’s header, use the code above. If you want to add the tracking code to your website’s footer, use the snippet below:

// Insert Google Analytics Code To WP Footer
add_action('wp_footer', 'footer_google_analytics');

function footer_google_analytics() { ?>
// Your Google Analytics Code Here

// End Google Analytics Code 
<?php }

To add this code to your WordPress site, you’ll need to make sure you have an active child theme installed. Once you’ve done that, you can add the code to your child theme’s functions.php file and update it. This will enable the code to take effect on your site.

If you don’t have an active child theme, it’s not recommended that you add the code to your parent theme’s functions.php file. This is because any future updates to the parent theme will overwrite the file, which will delete the code you added. Instead, it’s best to create and activate a child theme before adding the code to ensure that it remains on your site.

If you want to avoid having to create a child theme, you could create a custom plugin to hold your code snippets. Read our ‘How to create your own snippets plugin‘ guide, which provides a starting point for building your own WordPress plugin. This way, you can keep your code separate from your theme and add any other code snippets you need without worrying about them being overwritten by future updates.

WordPress Maintenance from £30.00/month

Experience unparalleled WordPress support and maintenance with our exceptional service plans. Entrust us with all your WordPress needs, guaranteeing the seamless operation and upkeep of your website.

Get Started