How to Add Google Analytics Script to WordPress
Adding Google Analytics to your WordPress site is the first critical step toward understanding who your audience is and what they do on your pages. This tutorial cuts straight to the chase, showing you four different methods to get your Google Analytics tracking code installed, from a simple plugin approach to more hands-on manual options.
What is the Google Analytics Script (and Why Do You Need It)?
The Google Analytics script, also known as the tracking code or the Global Site Tag (gtag.js), is a small snippet of JavaScript. When you add this code to your website, it tracks visitor activity - like which pages they view, how long they stay, and where they came from - and sends that information to your Google Analytics account.
Every Google Analytics 4 property has a unique "Measurement ID," which looks like G-XXXXXXXXXX. This ID is included in the tracking script and tells Google where to send the data. You may have seen the older Universal Analytics format (UA-XXXXXXX-X), but all new properties now use the GA4 format.
So, why is this so important? Without it, you're flying blind. Installing this script allows you to:
- See how many people are visiting your website.
- Discover which blog posts or pages are the most popular.
- Understand where your traffic is coming from (e.g., Google searches, social media, other websites).
- Track user demographics like age, gender, and location.
- Measure the success of your marketing campaigns by tracking conversions and goals.
Finding Your Google Analytics Tracking Code & Measurement ID
Before you can add the script to WordPress, you need to find it in your Google Analytics account. If you haven't created a property for your website yet, you'll be guided to create a "Data Stream" as part of the setup, and this process will generate your ID and script.
Here's how to find it in an existing GA4 account:
- Log into your Google Analytics account.
- Click the Admin gear icon in the bottom-left corner.
- In the Property column, make sure your website's property is selected.
- Click on Data Streams.
- Select the data stream connected to your website (you'll likely only have one here).
On the next screen, your Measurement ID (G-XXXXXXXXXX) will be clearly visible in the top right. Copy this — some plugins only require the ID. For manual installation methods, you'll need the full script. To get it, click View tag instructions at the bottom, then select the Install manually tab. This will reveal the complete gtag.js script for you to copy.
Method 1: Using a Plugin (The Easiest & Safest Way)
For the vast majority of WordPress users, a plugin is the best way to add Google Analytics. It's safe, simple, and you don't have to touch a single line of code. It also ensures the tracking code doesn't get removed when you update your theme.
While many plugins exist, we recommend Google's own official plugin, Site Kit by Google, because it's free, lightweight, and integrates other Google services like Search Console.
Step-by-Step Instructions for Site Kit
- From your WordPress dashboard, navigate to Plugins > Add New.
- In the search bar, type "Site Kit by Google" and press Enter.
- Find the plugin in the search results and click Install Now, followed by Activate.
- Once activated, you'll see a banner at the top of your Plugins page. Click Start Setup.
- The plugin will walk you through a simple setup process. You'll be asked to sign in with the Google account that you use for Google Analytics.
- Grant Site Kit the necessary permissions to access your data. This is secure and allows it to pull info from your Google properties.
- Site Kit will automatically detect the Google Analytics property associated with your website. Just confirm it's the right one.
- That's it! Site Kit automatically places the correct GA4 tracking script in your website's code. It will also add a dashboard right inside WordPress where you can see top-level traffic stats without leaving your site.
Method 2: Using Your Theme Options
Many modern WordPress themes come with built-in functionality to add tracking scripts to the header or footer. This method is a great alternative if you want to avoid installing another plugin and your theme supports it. Popular themes like Divi, Avada, or Astra almost always have this feature.
Look for this setting under Appearance > Customize or in a dedicated Theme Options panel in your WordPress dashboard. You're searching for a section labeled something like "Integrations," "Custom Scripts," or "Header/Footer Code."
Step-by-Step Instructions
- From your Google Analytics dashboard, copy the entire Global Site Tag (gtag.js) script.
- In your WordPress dashboard, find your theme's script/code section. Its location varies by theme, so you may need to check your theme’s documentation.
- Paste the full script you copied into the box labeled "Header Scripts" or a similar name. It's important to place it in the header so it loads on every page.
- Click Save Changes or Publish.
Heads Up: The one major downside to this method is that the tracking code is tied to your theme. If you decide to switch themes in the future, your Google Analytics code will be removed, and you'll have to add it again.
Method 3: Adding the Script to functions.php
This method is for more advanced users who are comfortable editing theme files. Adding the script via your functions.php file is a clean and reliable way to install it without a plugin.
Warning: Editing your theme's functions.php file can break your site if not done carefully. We strongly recommend using a child theme for this. If you edit the parent theme's file directly, your changes will be erased the next time the theme is updated.
Step-by-Step Instructions
- Make sure you have a child theme installed and activated.
- From your Google Analytics account, copy the entire
gtag.jsscript. - In your WordPress dashboard, go to Appearance > Theme File Editor.
- From the "Select theme to edit" dropdown on the right, choose your child theme.
- Select the Theme Functions (functions.php) file from the list.
- Scroll to the bottom of the file and paste the following PHP code. Remember to replace the placeholder script with your own
gtag.jsscript.
function add_google_analytics_script() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-YOUR-ID-HERE"></script>
<script>
window.dataLayer = window.dataLayer || [],
function gtag(){dataLayer.push(arguments),}
gtag('js', new Date()),
gtag('config', 'G-YOUR-ID-HERE'),
</script>
<?php
}
add_action('wp_head', 'add_google_analytics_script'),- Click Update File to save your changes. This code properly hooks into WordPress and injects your GA script into the site header on every page.
Method 4: Manually Adding the Script to header.php
This is another manual method for those who prefer editing theme files directly. You'll be inserting the Google Analytics script directly into your theme's header file. Like the previous method, you absolutely should use a child theme.
Step-by-Step Instructions
- Copy the entire
gtag.jsscript from your Google Analytics property's Data Stream page. - Navigate to Appearance > Theme File Editor in WordPress.
- Select your child theme.
- Find and click on the Theme Header (header.php) file to open it in the editor.
- Scroll through the file and locate the closing
</head>tag. - Paste your full Google Analytics script on the line just before the
</head>tag. - Click Update File.
This action places the script on every page of your site, but again, remember it's tied to your child theme. If you change themes, you'll need to do it again.
How to Verify Google Analytics is Working Correctly
After you've added the code using one of the methods above, it's a good idea to confirm that it's actually working. Here are a couple of quick ways to check.
1. Use the Realtime Report
This is the simplest way. While logged into Google Analytics, navigate to Reports > Realtime. Then, open your website in a new incognito browser window. Within a minute or so, you should see at least one "active user" pop up in your Realtime report — that's you! If you see activity, it means Google is successfully receiving data from your site.
2. Check Your Site's Source Code
Visit any page on your website, right-click, and select "View Page Source." Use your browser's find function (Ctrl+F or Cmd+F) and search for "gtag.js" or your Measurement ID (e.g., "G-XXXXXXXXXX"). If you see your tracking script in the code, it has been successfully added.
Final Thoughts
This article covered four proven methods for adding the Google Analytics script to your WordPress site, ranging from user-friendly plugins to manual code edits. For most users, using a dedicated plugin like Google's Site Kit is the most reliable and straightforward path to get your site's data flowing.
Once you start collecting data, understanding it becomes the next challenge. Stitching together reports from Google Analytics, your email platform, and social media ads to see the full picture can be incredibly time-consuming. We built Graphed to remove this friction by connecting all your data sources in one place. You can simply ask questions in plain English — like "Which marketing channels drove the most sales last month?" — and get instant dashboards and real-time answers, freeing you up to focus on growing your business instead of wrangling spreadsheets.
Related Articles
How to Connect Facebook to Google Data Studio: The Complete Guide for 2026
Connecting Facebook Ads to Google Data Studio (now called Looker Studio) has become essential for digital marketers who want to create comprehensive, visually appealing reports that go beyond the basic analytics provided by Facebook's native Ads Manager. If you're struggling with fragmented reporting across multiple platforms or spending too much time manually exporting data, this guide will show you exactly how to streamline your Facebook advertising analytics.
Appsflyer vs Mixpanel: Complete 2026 Comparison Guide
The difference between AppsFlyer and Mixpanel isn't just about features—it's about understanding two fundamentally different approaches to data that can make or break your growth strategy. One tracks how users find you, the other reveals what they do once they arrive. Most companies need insights from both worlds, but knowing where to start can save you months of implementation headaches and thousands in wasted budget.
DashThis vs AgencyAnalytics: The Ultimate Comparison Guide for Marketing Agencies
When it comes to choosing the right marketing reporting platform, agencies often find themselves torn between two industry leaders: DashThis and AgencyAnalytics. Both platforms promise to streamline reporting, save time, and impress clients with stunning visualizations. But which one truly delivers on these promises?