Skip to content

How to Install Google Analytics on WordPress Without Plugin

This guide shows you how to install Google Analytics on WordPress without using a plugin. You’ll learn to manually add the tracking code directly into your theme for accurate, lightweight analytics.

Key Takeaways

  • No plugin needed: Avoid plugin bloat by adding Google Analytics code manually.
  • Better performance: Manual installation reduces page load time and improves site speed.
  • Universal or GA4 support: Works with both older Universal Analytics and newer Google Analytics 4 properties.
  • Theme file editing required: You’ll edit your theme’s header.php or use a child theme for safety.
  • Always use a child theme: Prevents losing changes during theme updates.
  • Verify installation: Use Google Tag Assistant or Real-Time reports to confirm tracking is active.
  • Backup first: Always back up your site before making code changes.

Why Install Google Analytics Without a Plugin?

Adding Google Analytics to your WordPress site is essential for understanding visitor behavior, tracking conversions, and improving your content strategy. While plugins like MonsterInsights make it easy, they can slow down your site and add unnecessary code. By installing Google Analytics manually, you keep your site lightweight, fast, and fully under your control.

This guide walks you through the process of installing Google Analytics on WordPress without a plugin. Whether you’re using Universal Analytics (UA) or the newer Google Analytics 4 (GA4), the steps are simple and effective. You’ll learn how to add the tracking code directly to your theme, use a child theme for safety, and verify that everything is working correctly.

Step 1: Create a Google Analytics Account

How to Install Google Analytics on WordPress Without Plugin

Visual guide about How to Install Google Analytics on WordPress Without Plugin

Image source: w3techniques.com

Before adding any code to your site, you need a Google Analytics account and a property set up.

Sign in to Google Analytics

Go to analytics.google.com and sign in with your Google account. If you don’t have one, create a free Google account first.

Set Up a New Property

Click “Start measuring” or “Create Account” and follow the prompts. Enter your account name (e.g., your website name), then create a property. Choose between Universal Analytics (older) or Google Analytics 4 (recommended). GA4 is the future of Google Analytics and offers better insights for websites and apps.

Get Your Tracking ID or Measurement ID

Once your property is created, Google will provide a tracking code. For Universal Analytics, it looks like UA-XXXXXXXXX-X. For GA4, it starts with G-XXXXXXXX and is called a Measurement ID. Copy this ID—you’ll need it in the next step.

Step 2: Access Your WordPress Theme Files

How to Install Google Analytics on WordPress Without Plugin

Visual guide about How to Install Google Analytics on WordPress Without Plugin

Image source: teknovidia.com

Now you’ll add the tracking code to your WordPress site. This requires editing your theme files. Always use a child theme to avoid losing changes when the parent theme updates. If you haven’t created one yet, check out our guide on how to install WP themes manually for tips on setting up a child theme safely.

Log in to Your WordPress Dashboard

Go to your WordPress admin area (usually yoursite.com/wp-admin) and log in.

Navigate to Theme Editor

In the left menu, go to Appearance > Theme File Editor. This opens the code editor where you can modify theme files.

Choose the Right File

Look for header.php in the list of theme files. This file loads on every page and is the best place to insert the Google Analytics tracking code. Click to open it.

Step 3: Add the Google Analytics Tracking Code

How to Install Google Analytics on WordPress Without Plugin

Visual guide about How to Install Google Analytics on WordPress Without Plugin

Image source: img.techarea.co.id

Now you’ll paste the tracking code just before the closing </head> tag.

Locate the </head> Tag

Scroll through the header.php file until you find the line that says </head>. This tag closes the HTML head section.

Paste the Tracking Code

Just before </head>, paste the full Google Analytics tracking script. Here’s an example for GA4:

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-XXXXXXXX');
</script>

Replace G-XXXXXXXX with your actual Measurement ID. For Universal Analytics, the code looks slightly different:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXXXXXXX-X');
</script>

Save Your Changes

Click “Update File” to save the changes. Your tracking code is now active on every page of your site.

Step 4: Use a Child Theme (Recommended)

Editing the parent theme directly is risky. If you update your theme, your changes will be lost. That’s why using a child theme is essential.

Why Use a Child Theme?

A child theme inherits all the functionality of the parent theme but allows you to make custom changes safely. When the parent theme updates, your modifications—like the Google Analytics code—remain intact.

How to Create a Child Theme

If you don’t have one, create a child theme by adding a new folder in /wp-content/themes/. Name it something like yourtheme-child. Inside, create a style.css file with the following header:

/*
Theme Name: YourTheme Child
Template: yourtheme
*/

Then create a functions.php file to enqueue the parent theme’s styles. Once activated, you can edit the child theme’s header.php file instead of the parent’s. For more details, see our guide on how to apply a WordPress theme.

Step 5: Verify the Installation

After adding the code, you need to confirm that Google Analytics is tracking your site correctly.

Use Google Tag Assistant

Download the Google Tag Assistant Legacy Chrome extension. Visit your website and click the extension icon. It will check if the Google Analytics tag is firing properly.

Check Real-Time Reports

Go back to your Google Analytics dashboard and click on Real-Time under the Reports section. Then open your website in another tab. If you see active users, the tracking is working.

Wait 24–48 Hours

Full data, like page views and traffic sources, may take up to 48 hours to appear. But Real-Time reports should show activity immediately.

Troubleshooting Common Issues

Sometimes the tracking code doesn’t work as expected. Here’s how to fix common problems.

Code Not Showing in Page Source

If you view your page source (right-click > View Page Source) and don’t see the tracking code, double-check that you edited the correct header.php file. If you’re using a child theme, make sure it’s active and that you edited the child theme’s file, not the parent’s.

Multiple Tracking Codes

Avoid installing Google Analytics more than once. If you’ve used a plugin before, deactivate it to prevent duplicate data. Check your site with Tag Assistant to confirm only one tag is active.

Theme Updates Removed the Code

If your tracking code disappeared after a theme update, it means you edited the parent theme. Always use a child theme to prevent this. Learn more in our article on how to update a theme on WordPress without losing customizations.

GA4 vs Universal Analytics Confusion

Make sure you’re using the correct code for your property type. GA4 uses G-XXXXXXXX, while Universal Analytics uses UA-XXXXXXXXX-X. Mixing them up will cause tracking errors.

Alternative: Add Code via functions.php

If you prefer not to edit header.php, you can add the tracking code using your theme’s functions.php file.

Use wp_head Hook

Add this PHP code to your child theme’s functions.php file:

function add_google_analytics() {
?>   

This method is cleaner and avoids direct file editing. Just remember to replace G-XXXXXXXX with your actual ID.

Conclusion

Installing Google Analytics on WordPress without a plugin is a smart way to keep your site fast and efficient. By adding the tracking code manually to your theme’s header.php or functions.php, you avoid plugin overhead and gain full control over your analytics setup.

Always use a child theme to protect your changes during updates, and verify your installation with Google’s tools. Whether you’re using Universal Analytics or GA4, this method ensures accurate, reliable tracking for your website.

With this guide, you now have the skills to monitor your site’s performance, understand your audience, and make data-driven decisions—all without relying on a plugin.