Many WordPress users struggle when it comes to connecting Google Analytics to their WordPress site and they miss out on collecting data about their users.
Fortunately, there are several methods available for adding Google Analytics to WordPress. Once you know what you’re doing, these techniques are fairly simple and can prove useful in a variety of situations.
In this post, we’ll discuss what Google Analytics does and why it matters to your site. Then we’ll share four ways you can connect it with WordPress to start tracking activity.
Let’s get right to it!
Prefer the video version?
A Quick Introduction to Google Analytics
Before you go about connecting Google Analytics to your WordPress site, it’s best to understand what it is and how it works. In short, this platform monitors your website’s activity and provides you with key data to help you improve it:
By using a code snippet inserted into the header of each of your site’s pages, Google Analytics gathers a wide variety of information, which can be divided into five categories:
- Realtime: Displays what’s happening on your site at the specific moment you’re viewing the report.
- Audience: Includes information about who is visiting your site, including demographics.
- Acquisition: Explains how your site acquires traffic.
- Behavior: Shows what visitors do on your site, including buttons clicks and which of your pages are the most popular.
- Conversions: An advanced report to be used in conjunction with Google Analytics Goals, to track activity such as email list subscriptions or e-commerce sales.
Some examples of specific information you might find in your Google Analytics reports include:
- The number of active users on your site at any given moment.
- How many people visit your site over time.
- Your visitors’ ages, genders, and locations.
- Where your visitors come from (e.g., organic search, social media, etc.).
- Which pages visitors view and for how long.
- Bounce, click-through, and conversion rates.
There’s a lot to see and learn in your Google Analytics dashboard. This can help you improve your site in many ways.
To find out about Google’s latest version of Google Analytics, check out our guide to GA4.
Why Analytics Matter to Your WordPress Site
If you know how to put them to good use, analytics can be valuable to many different aspects of your website. Four key areas that are relevant to any type of site include:
- Content marketing strategy. By considering your most popular posts, pages, products, or other content, you can determine what your visitors like most. Increasing pageviews and traffic is a matter of creating more of the type of content that resonates best with your audience (here’s how to get started with keyword research and how to optimize for SEO).
- Inbound marketing. Knowing where your traffic is coming from tells you which marketing strategies are working and which ones are falling flat. You can then focus the bulk of your efforts and budget on the methods that produce the most results.
- Audience segmentation. Understanding your audience is key to effectively communicating with them to encourage conversions. With analytics, you can create subgroups for targeted marketing.
- User Experience (UX) auditing. User behavior flows can reveal problems with your navigation or site design. Pinpointing poorly-performing content and pages with high bounce rates may help you improve problem areas.
If you’re running an ecommerce site, the Conversions report is also helpful for tracking sales and revenue.
As you can see, it’s not always the numbers themselves that are important. How you apply the knowledge you gain from Google Analytics is where the value is. Fixing problem spots and applying the best marketing techniques for your site can lead to increases in traffic, conversions, and revenue.
How to Add Google Analytics to WordPress (4 Possible Methods)
If you’re trying to gather data about your users, here are different ways you can connect Google Analytics to your WordPress site.
In each case, we’ll assume you already have a Google Analytics account set up. If you don’t, head over and create one first, then come back for your next steps.
1. Use a Google Analytics WordPress Plugin
The simplest way for most WordPress users to add Google Analytics to their sites is with a Google Analytics WordPress plugin. This method saves you from having to edit your website’s code.
To do this, we recommend two plugins:
Site Kit by Google
If you’d like to add Google Analytics to your WordPress site, Google has got you covered with a simple yet powerful plugin: Site Kit by Google.
This free plugin connects Google Analytics to your WordPress site in just a few clicks but that’s not all. On top of analytics, this plugin pulls data from other services provided by Google, like Google Search Console, Google AdSense, and PageSpeed Insights allowing you to build a nice dashboard based on Google as your data source.
Here’s how to get started:
Install and activate the Site Kit by Google plugin, then click on the Start Setup button.
When done, you’ll be required to connect your WordPress site to your Analytics account.
As you might expect, you’ll need to go through a few steps to verify you’re the owner of the domain, grant access to Google to its data, and set up each service you’d like to add to WordPress.
When successfully connected with your Google account, you’ll be then able to see data from Google Analytics along with all other services activated directly in your WordPress dashboard. Here’s how it looks:
All this data will always be reachable directly within WordPress in your left-hand sidebar.
If this plugin is too much for you and want to keep things focus primarily on Google Analytics, you could use the next plugin.
GA Google Analytics
The free GA Google Analytics plugin by Jeff Starr has over 400,000 active installs and a five-star rating:
After you install and activate the Google Analytics WordPress plugin, all you have to do is add your Google Analytics tracking ID in its settings. To retrieve your tracking ID, go to your Google Analytics account and click on Admin in the sidebar:
Under Property, select Tracking Info, then click on Tracking Code:
Your tracking ID will start with UA and be at the top of this section:
In your WordPress dashboard, navigate to Settings > Google Analytics. Scroll down to Plugin Settings and add your tracking code in the relevant field:
This plugin allows you to also choose whether you’d like the tracking code to be placed in your header vs footer, add custom tracking code, force SSL, disable tracking for your admin users, and a few other useful options.
Once tweaked to your needs, scroll down and click on the Save Changes button. Google Analytics should now be active on your WordPress site.
2. Manually Connect Google Analytics and WordPress with Code
If you’d prefer not to use a plugin, it’s also fairly simple to add the Google Analytics tracking code directly to your WordPress theme via your functions.php file.
If you go the manual route, it’s essential that you use a child theme so that your tracking code won’t be overwritten every time you update your WordPress theme. Before you make any changes, though, it’s always good practice to make a backup.
Once your child theme is active, you’ll need to paste your Google Analytics Tracking Code right before the closing </head> tag in your header.php file.
To find your Google Analytics tracking code, go to your account and access your tracking info as we described in the previous method. You can then copy the script from the box under Google Site Tag:
To add it to your WordPress site, go to Appearance > Theme Editor. If your child theme is inheriting template files from its parent, you won’t see header.php here. You can click on the name of your parent theme in the file list to access it:
Once you’ve located and selected header.php, paste the Google Analytics Tracking Code right before the </head> tag in the Editor:
Then, make sure to click on the Update File button. Google Analytics tracking code is now being added to WordPress.
3. Integrate Google Analytics and WordPress with Google Tag Manager
Another method you can use to add Google Analytics to WordPress is using Google Tag Manager. This is really meant for sites that incorporate scripts for a variety of purposes, not solely analytics.
Google Tag Manager is a tag management system. It allows you to quickly and easily update tags and code snippets on your website or mobile app, such as those intended for traffic analysis and marketing optimization.
You can add and update scripts from the Tag Manager user interface, instead of editing site code in WordPress. This reduces errors and frees you from having to involve a developer when configuring tags.
The easiest way to do this is to use the free Google Tag Manager WordPress plugin. It currently has over 300,000 active installs and a five-star rating:
You can download the Google Tag Manager plugin from the WordPress repository or by searching for it under Plugins > Add New in your WordPress dashboard. You will then need to sign up for a Google Tag Manager account and create your first container.
Once you have completed the above steps, you can copy your container ID from the Google Tag Manager dashboard by navigating to the Admin tab and looking for a series of characters starting with GTM:
Then return to your WordPress site and paste your Google Tag Manager ID into the relevant field on the plugin’s settings page. You can get there by navigating to Settings > Google Tag Manager:
Any tag you set up in Google Tag Manager (including the one for Google Analytics) will now work on your WordPress site via the plugin.
4. Sync Google Analytics Locally for Performance Reasons
The final method you can use is to sync the Google Analytics script locally on your own server. Please be aware that this method isn’t supported by Google.
However, there are a couple of reasons you might want to do this. First, syncing Google Analytics locally can reduce your external HTTP requests. Additionally, you’ll have full control over the caching of the file so you can use your own server’s cache headers.
This is important for improving website performance. If you use Google PageSpeed Insights, you may have seen a “Serve static assets with an efficient cache policy” warning for your Google Analytics script:
The problem is that by default, Google Analytics has a two-hour cache time, as you can see in the warning above. This is likely because if Google needs to modify something on its end, it wants all users to have access to the changes as fast as possible.
By hosting the Google Analytics script on your own server, you can change the cache time to improve performance.
Syncing Google Analytics Locally with the Complete Analytics Optimization Suite
To get started, we recommend using the Complete Analytics Optimization Suite (CAOS), a free plugin created and developed by Daan van den Bergh. It currently has over 20,000 active installs with a five-star rating:
You can download it from the WordPress repository, or by searching for it under Plugins > Add New in your WordPress dashboard.
- Anonymize the IP address of your visitors.
- Set an adjusted bounce rate.
- Change the placement of the Google Analytics script (header or footer).
You’ll need to deactivate any analytics plugin you already have installed before using it.
When you install CAOS and enter your Google Analytics Tracking ID, the plugin adds the necessary code for Google Analytics to your WordPress website. It then downloads and saves the analytics.js file to your server, and keeps it updated using a scheduled script in wp_cron().
We recommend setting the script to load in the footer by going to Settings > Optimize Analytics and selecting the Footer option under Position of tracking code:
It’s important to note that CAOS doesn’t work with other Google Analytics plugins.
Syncing Google Analytics Locally With Perfmatters
Another plugin you can use for local analytics is Perfmatters. This is a premium website performance optimization plugin created by Brian Jackson and his brother Brett:
Among its many features, there’s also an option to sync Google Analytics locally by entering your tracking ID. Like CAOS, you can also set the script to load in your footer instead of your header.
Google Analytics is a vital tool for website owners and managers, although not the only analytics tool available you could use.
Tracking your site’s user activity, referral sources, and other data is key to growing your audience and your brand. To connect Google Analytics to your WordPress site, you can use one of the following ways:
- Add Google Analytics to WordPress with a plugin (Site Kit or GA Google Analytics).
- Manually connect Google Analytics and WordPress by adding code directly to your theme.
- Integrate Google Analytics and WordPress using Google Tag Manager (easy with a plugin such as Google Tag Manager WordPress).
- Sync Google Analytics locally for performance reasons (using the CAOS or Perfmatters plugins).
Have any thoughts or different methods you use to add Google Analytics to WordPress? If so, we would love to hear them!
Thank you for this very timely post! I currently use Jetpack on my WordPress site, and want to switch to Google Analytics. I can’t seem to find any information on the web about this, and am very concerned if my data from the last 1.5 years while on Jetpack will disappear once I switch to Google Analytics. Could you kindly point me in the right direction?
Hello Kasia, we recommend checking in with the Automattic team to confirm whether those analytics will be retained.
I’m a bit confused. I have Google Site Kit installed on my site, and have connected it to my Google Analytics account. It’s been that way for a few weeks, but all my analytics info is zeros. We’ve gotten a number of online class registrations during that time, so I know people have visited the site.
With your link to the “How to Edit WordPress Code” article at the beginning of the plugins section, you seem to be implying that Site Kit will add that code.
Does Site Kit actually insert the script code into the WordPress site? I don’t see any indication that it does. It seems to just display my analytics data in the dashboard?
Hi Don, I just performed some testing with the Site Kit plugin. After installing the plugin, I checked my HTML source code and saw Google’s gtag.js injected. If you’ve installed Site Kit and linked it up with your Google Analytics property, it should work.
Thank you for this post! I have a question:
The instructions say “If your child theme is inheriting template files from its parent, you won’t see header.php here. You can click on the name of your parent theme in the file list to access it”
So are the instructions to add the analytics tag via the header.php file from the parent theme?
Wondering because earlier in the article it says that “it’s essential that you use a child theme so that your tracking code won’t be overwritten every time you update your WordPress theme.”
Thank you for this detailed article! It really works wonders for newbies.. and I’m glad it came up in my search results.
Thanks a lot for sharing this post about the installation of Google Analytics Tag on WordPress. I ended up using Google Site Key and then activated Google Analytics. I don’t think I knew about this plugin before. Thank you
My name is Amy. I listened to the video and looked through the four methods as suggested above. But after I re-added the google analytics to my website, I still can’t see any data loading. Everything was fine Yesterday, I still saw all the data, but since this morning, google analytics completely didn’t work properly. I didn’t know when it turned to be “deactivate”, after I activated it, nothing showed up yet. but it showed me that “The server is temporarily unable to service your request due to maintenance downtime or capacity problems, please try again later”. I was told by wordpress representative because I activated the “Site Kit of Google”, my server was broken. It is your system problem.
I want your group to find out what is going on with my server and why it happened to me only. I also would like to have it back to work properly asap!
Hi Amy, where do you host your WordPress site, is the site working properly or is it unavailable at the moment?
Thank you for your reply!
I am using Godaddy/wordpress as my hosting system. Since this May, the server always have problems. The variety of issues always happens on my site, like the page contents can’t be updated, saved. The site has been crashed twice…
I talked to Google analytics technical support. I was told that someone who have access to my backend removed the tags linked with google analytics. That’s why all data turned to be zero. Yesterday I was guided by the Google Analytics Technical Support to copy the code from my google analytics account and pasted on my website. But the data from May to now can’t be recovered because the code was removed. But this morning. the code which I added yesterday disappeared again. A few data showed up on my GA account. I don’t know how it works after the code is gone again.
I have had over 1000 people visited my website and the trends and data looked fine. But now the zero can’t show me anything. It’s a big loss for me.
Now all plugins on my site turns to be “”deactivate”, I don’t know why. The site works very slow when I try to update the contents based on the SEO results. When I activated the google analytics. no change at all but zero shows up
Can you please help me further? I really want all data from this May to now got recovered.
Hi Amy, I’m sorry to hear that. Unfortunately, I’m afraid that data can not be recovered retrospectively if the Google Analytics code was not placed correctly or for some reason, it was not working. Also interesting is that all the plugins are deactivated. Are you sure you are the only one with access to the backend?
What I would do in your position is to find a WordPress developer from a trusted source (like Codeable) and even with a small budget they can get your site in shape. I would ask to review and fix the analytics code, check the website for malware and find out why the plugins are deactivated.
Thank you so much for your reply. AS you said, the wordpress representative mentioned that they couldn’t do anything to recover the data because the code was removed or misplaced.
Except me, only godaddy/wordpress have access to my backend. They all happened at the same time. It’s so strange. I lost three months analytics data to do analysis report for my website. It’s a huge loss.
I paid for wordpress premium technical support to take care of this issue. But they couldn’t do anything.
This platform sounds like very unstable.