# How to install Google Analytics on WordPress

URL: https://wp24x7.com/how-to-install-google-analytics-in-your-wordpress-website/
Updated: 2026-11-29
Type: blog post
Audience: WordPress site owners and marketers

> To track visitors, add a Google Analytics 4 property to your site and connect it with a plugin or a tag. Site Kit by Google is the simplest route, a Tag Manager container is the most flexible and a small snippet in a site plugin suits a site that needs only analytics. Pick one method, test in the Realtime report and handle consent where it applies to you.

## Key facts

- For: owners adding Google Analytics 4 to a WordPress site
- Needs: a GA4 property and web data stream with a Measurement ID starting G-
- Methods: Site Kit by Google, a small snippet, or Google Tag Manager
- Test: private window, then the Realtime report within a minute or two
- Retired: Universal Analytics (UA- IDs) no longer processes data
- Privacy: consent rules depend on your visitors' location; check what applies to you
- Help: WooCommerce tracking audit through WordPress support

To track visitors, add a Google Analytics 4 property to your site and connect it with a plugin or a tag. **Site Kit is the simplest route, a Tag Manager container is the most flexible, and a small snippet suits a site that needs only analytics.**

> **Quick answer**
Create a GA4 property and data stream, copy the `G-` Measurement ID, add it with one method only, then confirm your own visit in the Realtime report. Decide about consent first.

## What do I need before I start?

- A Google account, and a **Google Analytics 4 property** with a web data stream for your domain. The stream gives a **Measurement ID** beginning `G-`.
- Administrator access to WordPress.
- A decision about consent: whether you need a banner before loading analytics.

Universal Analytics, with IDs starting `UA-`, has been shut down, so ignore guides that tell you to paste one.

## Which method should I pick?

| Your situation | Suggested method |
|---|---|
| You want reports inside WordPress and little setup | Site Kit |
| You want only the tag and nothing else | Small snippet in a site plugin |
| Several marketing tags, or a marketing team that changes them | Tag Manager |
| A shop that needs purchase tracking | Tag Manager or an e-commerce analytics plugin, then test with an order |

Pick one method and remove the others, because stacking them is the usual cause of double counting.

## How do I install it?

### Site Kit
Site Kit is Google's own plugin and connects Analytics, Search Console, Tag Manager and other services from the dashboard.

1. Install and activate **Site Kit by Google** from Plugins, Add New.
2. Follow the set-up screens and sign in with the account that owns the property.
3. Connect Analytics and select your GA4 property and data stream.
4. Watch what it adds: it can place the tag itself, or you may already have a tag from another plugin.

It brings reports into the dashboard. The trade-off is another plugin and permissions to your Google data, so remove access when you stop using it.
### Small snippet
If you want only analytics, add the tag in a small site plugin or a header-scripts plugin. Do not paste into the parent theme's `header.php`.

```php
add_action( 'wp_head', function () {
	if ( is_user_logged_in() && current_user_can( 'edit_posts' ) ) {
		return; // Do not count staff visits.
	}
	$id = 'G-XXXXXXXXXX'; // Your Measurement ID.
	?>
	<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr( rawurlencode( $id ) ); ?>"></script>
	<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());
		gtag('config', '<?php echo esc_js( $id ); ?>');
	</script>
	<?php
}, 20 );
```

Replace `G-XXXXXXXXXX` with your own ID. Excluding logged-in editors keeps your own browsing out of the data. If you need consent, this should run only after the visitor agrees, which a consent plugin can control.
### Tag Manager
Tag Manager lets you add and change tags, including Analytics, without touching WordPress again. Install one container snippet (or use Site Kit or a Tag Manager plugin) and manage the rest in the Tag Manager interface. It suits sites with several marketing tags. For one tag it is more than you need.

## How do I test that it works?

1. **Open your site** in a private window with ad blockers off.
2. **Check Realtime.** In Analytics open **Reports, Realtime**; your visit should show within a minute or two.
3. **Use DebugView** under **Admin, DebugView** for events.
4. **Use Preview** in Tag Manager to see which tags fire.
5. **Search the page source** for your `G-` ID. If it appears twice you have two installations and are double counting.

## What about consent and privacy?

Analytics sets identifiers and collects data about visitors, which privacy laws in many places regulate. Whether you need a banner, and what must happen before the tag loads, depends on your visitors' location and your configuration. This is a legal question, so check the rules that apply to you, update your privacy policy and use a consent management plugin if needed. Google offers a Consent Mode setting that adjusts how tags behave based on a visitor's choice.

## What mistakes are common?

| Mistake | Effect | Fix |
|---|---|---|
| Two installations at once | Inflated numbers | Keep one source of the tag |
| Optimizer delays JavaScript | Short visits unrecorded | Test with your optimizer on |
| Counting yourself | Distorted reports | Exclude editors or filter internal traffic |
| Wrong property | Data in the wrong place | Match the ID to your domain's data stream |
| No key events | A visit count alone is not useful | Mark form submissions or purchases as key events |

## What should I set up after the tag works?

- **Data retention:** the default is short; extend it in the property's data settings.
- **Internal traffic:** define your office address and filter it out.
- **Key events:** mark actions such as a form submission, a phone-number click or a purchase.
- **Search Console link** to see which queries bring visits.
- **Cross-domain:** if checkout is on another domain, configure it or sessions break there.

Enhanced measurement can record scrolls, outbound clicks and file downloads automatically. Review what it records and switch off anything you do not need.

## A worked example, and WooCommerce

A local business wants to know whether its contact form generates leads. After installing the tag, the owner submits a test enquiry and watches Realtime. The form shows a thank-you page at its own address, so the owner records a key event when that page loads. If the page is also reachable by direct link, the count includes those visits, so the owner makes it non-indexable and does not link to it.

Store tracking needs more than the basic tag: view item, add to cart and purchase events. Test with a real order, then compare the purchase event with WooCommerce's order list. If numbers differ, see our [WooCommerce experts](/woocommerce-experts) or [WordPress support](/wordpress-support) for a tracking audit.

## How do I remove or replace the tag later?

Search the page source for the old ID and find which plugin or snippet adds it before deleting anything. Remove it from the one place that inserts it, test the source again and confirm Realtime shows no traffic from the old property. If you switch methods, do it in one step: install the new one, remove the old one, test.

**Need tracking set up properly?**
Our engineers can install, test and audit it.
[Post a project](/post-project)
[WordPress development](/wordpress-development)

## Frequently asked questions

### Is Universal Analytics still available?

No. Google stopped processing Universal Analytics data in 2023. Google Analytics 4 is the current version, and its measurement IDs start with G-.

### Do I need a cookie banner?

Often yes, depending on where your visitors are and how you configure analytics. This is a legal question, so check the rules that apply to you and use a consent tool if required.

### How do I know it is working?

Open your site in a private window, then check the Realtime report in Google Analytics. Your visit should appear within a minute or two.

### Which method should I choose?

Site Kit if you want reports inside WordPress and little setup, a small snippet if you want only the tag, and Tag Manager if several marketing tags or a marketing team change them. Pick one and remove the others.

### Why are my numbers doubled?

Two installations are probably running, such as a theme option, a plugin and Tag Manager all inserting the tag. View the page source and search for your G- ID: if it appears twice, remove one.

### Where should I paste the Analytics code?

In a small site plugin or a header-scripts plugin, not the parent theme's header.php, since a theme update erases it.

### Can I stop counting my own visits?

Yes. Exclude logged-in editors in the snippet, or define your office address as internal traffic and filter it in Analytics.

### Why do short visits go unrecorded?

Caching and optimization plugins with a Delay JavaScript setting can postpone the tag so much that short visits are missed. Test with your optimizer on.

### What should I set up after the tag works?

Extend data retention if needed, filter internal traffic, mark key events such as form submissions or purchases, link Search Console and configure cross-domain tracking if checkout is on another domain.

### Can Analytics track WooCommerce sales?

Store tracking needs view item, add to cart and purchase events, which plugins from Google and others send. Test with a real order and compare the purchase event with WooCommerce's order list.

### How do I remove or replace the tag later?

Search the page source for the old ID, find which plugin or snippet inserts it and remove it from that one place. Switch in one step: install the new one, remove the old one, test.

### Does the Measurement ID have to match my domain?

Yes. Check the ID matches the data stream for your domain, since a wrong property sends data to the wrong place.
