how to add google analytics to wordpress

How to Add Google Analytics to WordPress

Last modified: November 8, 2026

Fiverr freelancers

If you have a WordPress website and don’t use Google Analytics (yet), this post is for you. This post will teach you the entire process of how to add Google Analytics to WordPress. But before we go into creating an Analytics account, let’s understand what Google Analytics is, how it can help us and why we should want to add it to our WordPress website.

 

Show More

* This button will show the rest of the post and open up an offer from a vendor

What is Google Analytics?

Google Analytics is one of the most widely used free analytics tools for websites. The current version, Google Analytics 4 (GA4), replaced the older Universal Analytics in July 2023. If you are setting up analytics for the first time, you will be working with GA4.

Google Analytics tracks everything that happens on your website: who visits, where they come from, which pages they land on, how long they stay, and what actions they take. Here are the key things it tells you:

  1. Amount of traffic per set timeframe
  2. Traffic comparison between timeframes
  3. Traffic source (search, social, direct, referral)
  4. Traffic medium
  5. Country and city
  6. Landing page
  7. Exit page
  8. Time on site
  9. Conversions and events

All of this data is free once you connect GA4 to your WordPress site. The steps below will walk you through the full process.

Create a Free Google Analytics Account

First, go to analytics.google.com and sign in with the Google account you want to use for tracking. If this is your first time, click “Start measuring”.

You will be guided through three setup steps:

  1. Account name – enter your business or website name
  2. Property name – enter your website name, then set your time zone and currency
  3. Data stream – choose “Web”, enter your website URL and a stream name, then click “Create stream”

After creating the stream, Google will show you your Measurement ID (it starts with G-, for example G-ABC123XYZ) and a tracking code snippet. Make a note of both – you will need them in the next step.

The interface looks different from older screenshots you may see online, as Google updated GA4 significantly in 2023. The steps above reflect the current setup flow.

How to Add Google Analytics to WordPress

There are 2 approaches to installing Google Analytics on WordPress or any other script for that matter such as Facebook or Google pixels. If you are a developer and feel confident adding the code into your WordPress theme’s files, that would be the best approach. If you are just starting off or are even very knowledgeable with WordPress but just don’t know code – you should use the second approach which is to add a plugin.

There are 2 options we recommend:

1. Analytify: Best Google Analytics Plugin.

Install Plugin

In your WordPress dashboard, go to Plugins > Add New, search for “Analytify” and click “Install Now”, then “Activate”.

Installing the Analytify plugin from the WordPress plugin directory

Connect Google Analytics

Go to Analytify > Settings and click “Log in with your Google Analytics Account”. Sign in with the Google account that has access to your Google Analytics 4 property and allow access.

Analytify Settings Authentication tab with Log in with Google Analytics Account button

Select Your Property

Choose your GA4 property and data stream from the list, then save your changes. (You’ll need the GA4 property you created in Step 1 above.) Analytify adds the tracking code to your site for you, with no code needed.

Analytify Settings Profile tab showing GA4 property selection

View Your Stats

Open the Analytify dashboard in WordPress to see your traffic, top pages, traffic sources and real-time visitors.

Analytify dashboard overview showing visitor stats and traffic data inside WordPress

This was the first of 2 options we recommend. The second type of plugin is a plugin that enables you to add code to the site’s head / body / footer etc. Meaning, this is a bit more “techie” but still pretty simple.

2. Insert Headers and Footers

Insert Headers and Footers

Install Plugin

First of all, just like in the previous section, let’s go to the plugin section and add the “Insert headers and footers” to our WordPress site.

Headers and footers

After installing and activating the plugin, let’s go insert our WordPress code into it. Navigate to your settings tab and choose the “Insert Headers and Footers” link:

headers and footers

Next, navigate back to your Google Analytics tab and copy the script code we saw in the first section, or if you copied and pasted into a notepad file just copy that. Now in the “insert headers and footers” page we just paste the code into the header as displayed in the image below.

analytics

Now just hit the save button and you are done.

These 2 options are both easy to set up and you can have your Analytics tracking running within minutes. The other method is to put the Analytics code directly in your WordPress files.

How to Add Google Analytics Script in WordPress

The first option is to add the Google Analytics tracking code directly into your theme’s header.php file. This works for any GA4 property.

The tracking code snippet you need is in your Google Analytics account under Admin > Data Streams > your stream > View tag instructions. 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>

Replace G-XXXXXXXXXX with your actual Measurement ID.

Option A: Edit header.php directly

Open your theme’s header.php file via FTP and paste the tracking code anywhere before the closing </head> tag. Be aware that if you change your theme you will need to move the code again. A cleaner approach is to add the code inside your functions.php file using wp_head().

Option B: Use the Insert Headers and Footers plugin

This is covered in the plugin section above as option 2. Paste the tracking code into the header section and save. No file editing needed.

Is Google Analytics Working on Your WordPress Website?

The last step is to confirm your Google Analytics is actually receiving data. Whichever method you used above, open your GA4 account, go to Reports > Realtime and check if any users appear.

Then visit your own website in a new browser tab or on your phone. You should see at least 1 active user appear in the Realtime report within a few seconds.

If you see a user, your setup is working. If nothing appears after a minute, go back and check that the tracking code was saved correctly, or that Analytify is connected to the right GA4 property.

Save 4 Later
Email liked links to yourself

    One thought on “How to Add Google Analytics to WordPress”
    1. Chima Okpi says:

      Wow! this is a very detailed artilcle on how to add analytics to a site. I have been lookig for something like this for a while now.
      Thanks very much

    Comments are closed.

    Stay Updated with WordPress Insights

    Get the latest WordPress tips, theme reviews, and industry deals delivered to your inbox.