Where to Install Google Analytics in WordPress?

Cody Schneider••9 min read

Thinking about adding Google Analytics to your WordPress site? Good call. The path to growing your audience starts with understanding them, and that requires tracking who is visiting your website, where they're coming from, and what content they care about. This guide will show you exactly where and how to install your Google Analytics tracking code in WordPress, covering the easiest methods for beginners and more advanced options for those who like to get their hands dirty with code.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Before You Start: Get Your GA4 Measurement ID

Before you can add Google Analytics to WordPress, you need the unique code snippet that tells Google Analytics this is your site. In the latest version, Google Analytics 4, this is called a "Measurement ID" or "Global Site Tag."

If you don't have this yet, here’s how to get it in under a minute:

1. Create or Log into Your Google Analytics Account

Head over to the Google Analytics website and sign in. If you've never used it, you'll be prompted to create a new free account here.

2. Create a GA4 Property

In the admin section, click "Create Property." Give your property a name (like your website's name), select your time zone and currency, and click "Next." Provide some basic business information to finish the setup.

3. Set Up a Web Data Stream

Inside your new GA4 property, you need to tell Google where to collect data from. Choose "Web," and enter your website's URL (e.g., www.mycoolblog.com) and a name for the stream (again, your website name is fine). Then, click "Create stream."

4. Copy Your Measurement ID or Global Site Tag (gtag.js)

After creating the stream, you'll land on a page with your stream details. You'll see an ID that looks like G-XXXXXXXXXX. This is your "Measurement ID." Below that, you'll see a section called "Global site tag (gtag.js)." If you plan to install this manually, click it to expand and copy the entire JavaScript code snippet.

Now that you have your code, it's time to add it to your WordPress site. We'll start with the easiest method first.

Method 1: Use a Dedicated WordPress Plugin (Easiest & A+ for Beginners)

For 99% of WordPress users, this is the best, safest, and most straightforward method. Using a plugin means you never have to touch a single line of code, which eliminates the risk of accidentally breaking your site. These plugins handle the placement for you and often come with bonus features like viewing analytics reports right from your WordPress dashboard.

Some of the most popular and trusted plugins include:

  • Google Site Kit: The official plugin directly from Google. It's built to connect your site not just with Analytics, but also Search Console, AdSense, and PageSpeed Insights.
  • MonsterInsights: Arguably the most popular GA plugin, it offers an incredibly user-friendly setup wizard and powerful reporting features in both free and premium versions.
  • ExactMetrics: Another excellent plugin offering similar features to MonsterInsights, with a focus on ease of use.

Let’s walk through setting it up with Google Site Kit, since it’s Google’s official solution.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Step 1: Install and Activate the Site Kit Plugin

From your WordPress dashboard, navigate to Plugins > Add New. In the search bar, type "Google Site Kit" and press Enter. Click "Install Now" on the correct plugin, and then "Activate" it once it's done installing.

Step 2: Connect Your Google Account

After activating, you'll see a big blue banner prompting you to start setup. Click "Start Setup." You'll be taken through a simple authentication process where Site Kit asks for permission to access your Google account data. Sign in with the Google account you used to create your Analytics property. You’ll need to approve several permissions to allow Site Kit to view and manage your data on your behalf - this is perfectly normal and safe.

Step 3: Complete the Setup

Site Kit will automatically look for existing accounts connected to your Google account. It will find the Google Analytics property you just created. Confirm that it's the right one, and just like that, the connection is made. The plugin instantly adds the necessary tracking code to every page on your website in the correct place without you lifting a finger.

The best part? You can now see a simplified overview of your traffic and top-performing content directly within your WordPress admin area, saving you trips to the full Google Analytics interface.

Method 2: Manually Add the Tracking Code to header.php

This method involves editing your WordPress theme files directly. It's not as scary as it sounds, but it comes with a major warning: if you're not using a child theme, your changes will be deleted the next time you update your theme. One small typo could also take down your site.

A child theme inherits the look and functionality of another theme (the "parent theme") but allows you to modify it without touching the original files. It’s a WordPress best practice for any code customization.

If you're confident and using a child theme, here’s how to proceed.

Step 1: Get the Global Site Tag (gtag.js)

Go back to your Google Analytics Data Stream details page (Admin > Data Streams > Your Web Stream). Under the "Installation instructions" tab, find the "Install manually" section, which displays the Global Site Tag. Click the copy icon.

The code will look something like this:

<!-- 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', 'G-XXXXXXXXXX'),
</script>
GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

Step 2: Edit your Theme's header.php File

In your WordPress dashboard, navigate to Appearance > Theme File Editor. On the right side, find the file named header.php and click to open it.

Be very careful in this file. Scan for the closing </head> tag. Place your cursor right before it, hit Enter to make a new line, and paste your entire Global Site Tag snippet.

Step 3: Save Your Changes

Click the "Update File" button at the bottom. The Google Analytics tracking script will now be present in the head section of every single page on your website.

Method 3: Inject the Code with Your functions.php File

This is a more technically sound way to add the code manually. Instead of editing header.php directly, you use WordPress’s built-in hooks to inject the script. This method keeps your logic separate from your presentation files and is generally preferred by experienced developers.

Just like with the previous method, you should absolutely do this in your child theme's functions.php file.

Step 1: Open Your functions.php File

Go to Appearance > Theme File Editor and select your child theme's functions.php file from the list.

Step 2: Add a Custom Function

Scroll to the bottom of the file and paste the following PHP snippet. Make sure to replace <!-- YOUR GA SCRIPT HERE --> with the actual Global Site Tag you copied from Google Analytics.

function add_google_analytics_tag() { 
?>
<!-- YOUR GA SCRIPT HERE -->
<?php
}
add_action('wp_head', 'add_google_analytics_tag'),

Here's what it looks like with the real script:

function add_google_analytics_tag() { 
?>
<!-- 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', 'G-XXXXXXXXXX'),
</script>
<?php
}
add_action('wp_head', 'add_google_analytics_tag'),

This code creates a simple function and hooks it into wp_head, a standard WordPress action that runs just before the </head> tag is printed. This achieves the same result as editing header.php but in a much cleaner, more manageable way.

Step 3: Update The File

Click "Update File" and you're good to go.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Advanced Option: Use Google Tag Manager

For more advanced users, marketers, or anyone who plans on adding multiple tracking scripts to their site (like a Facebook Pixel, Hotjar tracking, or LinkedIn insights tag), Google Tag Manager (GTM) is the ultimate solution.

Think of it as a container for all your scripts. Instead of adding several snippets directly to your site, you just add one GTM snippet. Then, you manage all your other tags from the GTM interface. It has a steeper learning curve but offers incredible flexibility and power.

The general workflow is:

  1. Create a free GTM account and add the two GTM code snippets to your WordPress site (either manually or with a plugin like GTM4WP).
  2. Inside your GTM dashboard, create a new "Tag" using the pre-built Google Analytics GA4 Configuration template.
  3. Paste your GA4 Measurement ID into the tag configuration.
  4. Set the tag to "trigger" on All Page Views.
  5. Publish your container.

From then on, GTM will handle firing your GA script site-wide.

How to Verify the Installation is Working

After installing your code using any one of these methods, you should check to see if it’s working. Here are two easy ways.

1. Use the Realtime Report

The simplest way is to check the Realtime report inside Google Analytics. Log into your GA account, navigate to Reports > Realtime. Then, open your own website in a new browser tab or on your phone. In about 30 seconds, you should see your own visit pop up in the Realtime report. If you see activity, it’s working!

2. View Page Source

Visit your website, right-click anywhere on the page, and select "View Page Source." This will open a new tab with your site's HTML. Press CTRL+F (or CMD+F on Mac) and search for "gtag.js" or for your unique Measurement ID (e.g., "G-XXXXXXXXXX"). If you find the script in the code, you know it has been successfully added.

Final Thoughts

Connecting Google Analytics to your WordPress is a foundational step in building a data-driven strategy for your website. While there are a few ways to get it done, most users will find that a dedicated plugin like Google Site Kit offers the perfect blend of simplicity, safety, and functionality. For more technically-minded users, injecting the code via functions.php or using Google Tag Manager offers more control and scalability.

Getting your tracking code set up is just step one, the next challenge is turning that mountain of data into actionable insights without spending all day in complex report builders. This is where we built Graphed to help. We allow you to connect your Google Analytics account in seconds and create real-time dashboards and reports simply by asking for what you want in plain English. Instead of hunting through menus, you can just ask, "Show me my top 10 landing pages from organic search this month," and get a clear, live visualization instantly.

Related Articles