WordPress is a wonderful website platform with unlimited flexibility and endless possibilities. However, sometimes all those possibilities can lead to analysis paralysis and make a simple task, like adding Google Analytics to WordPress, confusing. That’s why we’ll tell you the good, better, best way to add Google Analytics to WordPress.
After you’ve set up a Google Analytics account, this guide will walk you through each step on how to add Google Analytics to WordPress and we’ll share some expert advice along the way. Here are our 3 recommended ways to add Google Analytics to your WordPress site.
- Good: Add Google Analytics To WordPress Manually Without A Plugin
- Better: Add Google Analytics To WordPress Using A Plugin
- Best: Add Google Analytics To WordPress Using Google Tag Manager
No matter which method you choose, after you’ve read this guide you’ll know exactly how to set up Google Analytics on a WordPress site so that you can start to take advantage of all the useful information this tool provides.
Want help? Contact our Google Analytics Setup experts and we’ll add Google Analytics to your WordPress website for you. Not sure about Google Analytics? Check out this introduction to Google Analytics to get an understanding of what it can do and the benefits of using Google Analytics for business.
Good Method:
Add Google Analytics To WordPress Manually Without A Plugin
Let’s get started with how to add Google Analytics to your WordPress site manually. In order to keep your WordPress website lightweight (read: fast), you can add your Google Analytics tracking code to either your header.php file or your functions.php source file. Using either of these files, you’ll be able to add Google Analytics directly to your source code without the need to use a WordPress plugin.
When you use this method, you’ll only need to edit one of these files, not both. For most people adding the Google Analytics code to the header.php file is a little bit easier, so we’ve listed that first.
Pro Tip: When editing your theme files, it is highly recommended that you make a backup of your current code before making changes. That way, it is easier to revert back to a working version in case you accidentally break something.
To find your WordPress source files, log into your WordPress admin dashboard.
- Click Appearance
- Then click Theme Editor
- Find your Theme Header (header.php) or your Theme Functions (functions.php)
Add Google Analytics to Header.php
Once your header.php file has loaded in the WordPress Theme Editor, paste your full Global Site Tag javascript as high in the <head> as you can and before the </head>. It will look something like this:
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-123456789-1');
gtag('config', 'G-XXXXXXXXXX');
</script>
<!-- End Global Site Tag -->
</head>
<body>
Don’t forget to click Update File at the bottom and if you used the code above, replace the UA codes in line 9 and the Measurement ID in line 10 with your own unique codes.
Take a look at this information on where to find your full Global Site Tag.
After you click Update File, that’s it, you’ve installed your Google Analytics code on your WordPress website without using any plugins – Easy!
Pro Tip: One huge caveat with this method is that switching themes or even updating your theme files can overwrite your header.php and functions.php file. For that reason, it’s actually recommended that you create a child theme for your WordPress site then edit your child theme’s header.php or functions.php file.
Add Google Analytics to Functions.php
Another option to add Google Analytics to WordPress manually is using the functions.php theme file.
To add your Google Analytics code to your functions.php file, you have to paste it inside what’s essentially a miniature function that gets pushed into the wp_head so that it runs on every page.
Below is the PHP code you’ll use to contain your Google Analytics tracking code script. Simply replace all of line 5 with your Google Analytics tracking code and place this entire code at the bottom of your functions.php theme file.
<?php
add_action('wp_head', 'add_googleanalytics');
function add_googleanalytics() { ?>
// Paste your entire Google Analytics tracking code here
<?php } ?>
After you’ve pasted the code in the appropriate spot, be sure to click Update File, and that’s all you need to do. You’ve now installed Google Analytics on your WordPress site.
Pro Tip: You can add any Google Analytics code to your header.php or functions.php files. You can even use this method to add a Google Tag Manager container which we will get to later.
If that sounds complicated, the next method, using a WordPress plugin to install Google Analytics, is less code-heavy and may be easier for those who are not developers, which is why we’ve marked it as a better method.
Better Method:
Use A Google Analytics WordPress Plugin
If you’d like to add Google Analytics without having to edit your theme files and have persistent data even if you switch WordPress themes, a better option would be to use a plugin. Here, we’ll cover how to add Google Analytics to WordPress using a plugin, or rather, a few different plugins.
Please note that we are not associated in any way with any of these plugins.
Header & Footer Scripts
Google recommends adding your Google Analytics Global Site Tag to the head of your website. By downloading & activating a free WordPress plugin called Header & Footer Scripts, you can place Javascript, like the one used to install Google Analytics, or in the CSS of your whole website with ease.
After activating the plugin, go to Settings > Header and Footer Scripts > Paste your Google Analytics script (including the tag in the first box) labeled “Scripts in header.” Then click Save Settings.
A very similar alternative to this plugin is Insert Headers and Footers. It works in basically the same way.
After pasting your code and hitting Save Settings – you are good to go! Google Analytics has been added to your website.
While the Header & Footer Scripts plugin can be used to add any script to your header or footer, that’s about all it can do so some people prefer a dedicated Google Analytics plugin with more options.
MonsterInsights Lite
If you’re looking for a free, dedicated Google Analytics plugin for WordPress, MonsterInsights Lite is the closest you’ll find. It comes with an in-WordPress Google Analytics dashboard and allows you to quickly and easily set up Google Analytics Enhanced Ecommerce for Woocommerce and other advanced features of Google Analytics.
After you’ve installed MonsterInsights, the plugin will walk you through how to set up Google Analytics for your WordPress site with next to no coding required. Since this one will walk you through the setup after installation, there’s no need for us to cover exactly how to add Google Analytics to WordPress using the MonsterInsights Lite plugin.
The one downside of this plugin is that it can feel quite pushy to get you to upgrade to the Pro version. If you don’t like that, maybe our 3rd recommended WordPress plugin for Google Analytics will do the trick.
Google Site Kit
Google Site Kit is more than just a Google Analytics plugin. The versatility of this plugin is its main advantage over the other two WordPress plugin options. It lets you install Google Analytics, verify your website in Google Search Console, check your page speed, and more. Since it was created by Google, it is a sort of one-stop-shop for all things Google, which is a good thing if you want to SEO optimize your website or run Google Ads.
After installing and activating the Google Site Kit plugin, each user that wants to edit its settings will need to connect to his or her Google Account by visiting the Site Kit menu item near the top. The plugin will then walk you through setting up or connecting to each of its services in just a couple of minutes.
Once you’ve gone through the Google Site Kit set up, you’re done! Google Analytics is installed on your WordPress site.
Best:
Upgrade To Use Google Tag Manager For WordPress
Installing Google Analytics manually in your theme files or using a plugin is great, but what if you also want to install other scripts or tracking solutions, like the Facebook Pixel or Google Ads conversion tracking? Using the previous methods, you’ll have to edit your theme code or install a new plugin every time you want to add a new tag.
This is why upgrading your tracking to use Google Tag Manager (GTM) is the best way to add Google Analytics to WordPress (along with many other tracking tags).
Google Tag Manager (GTM) is what’s known as a container tag that houses multiple tracking tags, scripts, and pixels and reduces the need to edit your website code or hire a developer each time you want to place a new tag on your website. Once GTM is installed, you’ll be able to use a web interface that allows you to add tracking codes by clicking instead of coding.
To start, you’ll first need to create a GTM container and add your Google Analytics tag(s) to it. We’ve outlined how to do just that in our introduction to Google Tag Manager.
Once, you’ve created a GTM container, you can add your GTM code to your theme files (see the “Good” option above), but we’ve found that the best way to add Google Tag Manager to WordPress is by activating the conveniently named Google Tag Manager For WordPress (GTM4WP) plugin.
After installing the GTM4WP plugin, navigate to the plugin’s settings page by clicking Settings in your WordPress dashboard, then Google Tag Manager. You can then enter your GTM container ID on the General tab and click Save Changes.
Using this amazing plugin will not only ensure that your Google Tag Manager container is installed even if you update or switch WordPress themes. It will also create what’s called a “dataLayer” automatically. This dataLayer allows you to access different variables via the GTM interface which makes creating tags and tracking events much easier.
Add Google Analytics To Google Tag Manager
Once GTM is installed on your WordPress website, you still need to add Google Analytics to Tag Manager. The process is a little different depending on if you’d like to add Universal Analytics or add Google Analytics 4 to GTM so for more in-depth instructions, we have another blog post to show you exactly how to add Google Analytics to Google Tag Manager.
You can also use the Google Tag Manager web interface to add other tags like the Facebook pixel to your WordPress website without knowing how to code.
What’s Next?
Once Google Analytics is installed on your WordPress website, you’ll begin collecting basic data on how your website visitors are interacting with your site. As always, don’t forget to test and verify your tracking to make sure your tags are firing and you’re tracking what’s important for your business.
Every website and business is different so what is important to you depends on your business goals. If you are running an ecommerce business, you may also want to enhance your Universal Analytics with the Enhanced Ecommerce plugin. Other businesses may want to set up Google Analytics Events to track form fills, downloads, click-to-calls, email signups, and other website actions. Depending on your website setup, not all of these actions will be automatically tracked for you in Google Analytics, so you may need to do some additional setup.
If you need help coming up with a scalable measurement strategy for your website or setting up Google Analytics events, feel free to reach out to our tracking & tagging experts. We’re here to help you make better data-driven decisions to maximize your business’s potential.