How to Add Google Analytics Tracking Code in WordPress

Cody Schneider8 min read

You've built your WordPress website, and now you want to know if anyone is actually visiting. Answering that question is the first step toward understanding your audience, measuring your marketing efforts, and ultimately growing your traffic. Google Analytics is the best tool for the job, and this guide will show you exactly how to add it to your WordPress site.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

We’ll cover the simplest plugin-based methods for beginners and also a manual approach for those who are more comfortable with code. Let's get your tracking set up so you can start making data-driven decisions.

Why You Need Google Analytics (Seriously)

Before jumping into the "how," it's helpful to remember the "why." Installing Google Analytics isn't just a technical task to check off a list, it unlocks a powerful understanding of your website's performance. It’s like turning the lights on to see who’s in the room.

Once it's running, you can answer critical questions like:

  • How many people visit my site? Track daily, weekly, and monthly users to see your growth over time.
  • Where do my visitors come from? See if your traffic is coming from Google search, social media, other websites, or directly. This tells you which marketing channels are working.
  • Who is my audience? Get anonymous demographic data like age, gender, and geographic location to better understand who you're reaching.
  • What content is most popular? Identify your top-performing blog posts and pages so you can create more of what your audience loves.
  • Are my website changes working? See how metrics like bounce rate and average time on page change after you redesign a page or publish new content.

Without this data, you're essentially guessing. With it, you have a roadmap for improving your website and growing your business.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

First Things First: Finding Your Google Analytics 4 Measurement ID

Before you can add Google Analytics to WordPress, you need to have an account and a tracking code to add. The latest version is Google Analytics 4, which uses a "Measurement ID." If you used Google Analytics years ago, you might remember a "Tracking ID" starting with UA-. That's the old version, everything now runs on GA4 and its G- Measurement ID.

Here’s how to find yours:

  1. Create or Sign In to a Google Analytics Account: Head over to the Google Analytics website and sign in with your Google account. If you've never used it, you'll be prompted to create a new account.
  2. Set Up a Property: A "property" is basically your website. Google's setup wizard will guide you through this. You’ll need to give your property a name (your website name is a good choice), and select your reporting time zone and currency.
  3. Set Up a Data Stream: GA4 gathers information from "data streams." Since you're tracking a website, choose "Web" as the platform.
  4. Enter Your Website Details: Type in your website’s URL (e.g., www.yourwebsite.com) and give the stream a name (again, your website name works well). Click "Create stream."
  5. Find Your Measurement ID: After creating the stream, you'll see a page with "Web stream details." In the top right corner, you'll find your Measurement ID. It will look something like this:
G-XXXXXXXXXX

This is the ID you need. Copy it and keep it handy. For some installation methods, you'll also need the full code snippet, which you can find under "View tag instructions."

3 Simple Ways to Add Google Analytics to WordPress

Now that you have your Measurement ID, it's time to connect it to your WordPress site. We'll start with the easiest and most recommended method and work our way to the more technical one.

Method 1: Use a WordPress Plugin (Recommended for Most People)

For most users, a dedicated analytics plugin is the safest and simplest option. You won't have to touch a single line of code, and you often get the added benefit of seeing basic reports directly within your WordPress dashboard.

One of the most popular and trusted options is MonsterInsights.

Using the MonsterInsights Plugin:

  1. Install and Activate the Plugin: From your WordPress dashboard, go to Plugins → Add New. Search for "MonsterInsights" and click "Install Now," then "Activate."
  2. Launch the Setup Wizard: Once activated, MonsterInsights will launch a friendly setup wizard. It walks you through the entire process, making it nearly foolproof.
  3. Connect Your Google Account: The wizard will ask you to connect MonsterInsights to your Google account. This is the magic step - it handles the complicated authentication process for you. You simply log in and grant it permission to access your Google Analytics data.
  4. Select Your Website Profile: After connecting, it will automatically find the GA4 property you created earlier. Select it from the list, and finalize the setup.

That's it! MonsterInsights will now automatically add the correct tracking code to every page of your site. As a bonus, it will start pulling in simple reports that you can view directly from the "Insights" tab in WordPress.

Other Great Plugin Options:

  • Site Kit by Google: Google's official plugin for WordPress. It connects your site to not only Analytics but also Search Console, AdSense, and PageSpeed Insights, all in one place.
  • ExactMetrics: Another popular and user-friendly choice that is very similar to MonsterInsights in function and a great alternative.
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Method 2: Use a Header and Footer Code Plugin

Maybe you don't want a full-featured analytics plugin, or maybe you're already using a different plugin to add scripts to your site (like for a Facebook Pixel). In this case, a simple header and footer scripts plugin is a great, lightweight solution.

This method involves pasting the entire Google Analytics code snippet - not just the Measurement ID - into your site's header.

Steps:

  1. Get the Global Site Tag (gtag.js) Script: Go back to your Google Analytics account. Navigate to Admin → Data Streams, and click on your website's stream. Under "Google tag," click on "View tag instructions." You'll see the full JavaScript code. It looks like this:
<!-- Google tag (gtag.js) -->
<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>
  1. Install a Code Snippet Plugin: In WordPress, go to Plugins → Add New and search for "WPCode." Install and activate it. This plugin provides a safe place to add any kind of script to your site's header or footer.
  2. Add the Code Snippet: In your WordPress dashboard, go to Code Snippets → Header & Footer.
  3. Paste and Save: Copy the entire gtag.js script from Google Analytics and paste it into the "Header" box. Click "Save Changes."

The plugin will now inject this script into the <head> section of every page on your site, which is exactly where Google wants it to be.

Method 3: Manually Adding the Code (For Advanced Users)

This method is for users who are comfortable editing theme files. It has the benefit of not requiring any extra plugins, but it comes with a major warning.

Warning: Editing your theme files directly can break your website if you make a mistake. Always use a child theme for customizations like this. If you don't know what a child theme is, please use Method 1 or 2 instead.

Steps:

  1. Create and Activate a Child Theme: If you don't already have one, create a child theme for your active theme. This ensures that your customizations won't be erased the next time you update your main theme.
  2. Get the Google Analytics Script: Copy the same full gtag.js script from Method 2.
  3. Edit Your functions.php File: In your WordPress dashboard, go to Appearance → Theme File Editor. On the right side, make sure you have selected your child theme. Click on the functions.php file.
  4. Add the PHP Code Snippet: Scroll to the bottom of the file and paste the following PHP code. Be sure to replace G-XXXXXXXXXX with your own Measurement ID where it appears in the script.
function add_google_analytics_tag() {
  ?>
  <!-- Google tag (gtag.js) -->
  <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' ),
  1. Save the File: Click "Update File" to save your changes.

This code hooks into the WordPress wp_head action, which properly injects your GA tag into the website's header on every single page load.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

How to Check if Google Analytics is Working

Once you've installed the code, you'll want to confirm it's working correctly. The best way is to use Google's own Realtime report.

  1. Open your Google Analytics dashboard.
  2. In the left-hand menu, navigate to Reports → Realtime.
  3. In a separate browser tab or on your phone, open your website.
  4. Watch the Realtime report. Within a minute or two, you should see at least one "user" appear on the map and in the charts.

If you see your visit register, congratulations! You have successfully installed Google Analytics on your WordPress site.

Final Thoughts

You’ve now added a powerful analytics tool to your WordPress site. Whether you used a simple plugin for a quick setup or manually added the code for a lightweight approach, you're ready to start collecting invaluable data about your audience and website performance.

Once data starts flowing into Google Analytics, the next step is turning it into clear, actionable insights without spending hours digging through complex reports. As you grow, you might connect other tools - like ad platforms, CRMs, or email services - and wonder how they're all working together. At Graphed, we built a tool that connects to all of your data sources and allows you to create dashboards and get answers just by asking questions in plain English. This lets you move from data collection to decision-making in seconds, rather than hours.

Related Articles