WordPress connected to Google Analytics 4 through Google Tag Manager

How to Add Google Analytics 4 to WordPress the Right Way

There are two clean ways to add Google Analytics 4 to a WordPress site: the native Google tag, or Google Tag Manager. Both work well. What matters is choosing one and installing it only once.

Before you start

In Google Analytics, create a property and a web data stream for your domain (Admin › Data streams). Copy the Measurement ID, which looks like G-XXXXXXXXXX.

Option 1: the native Google tag

Use it when you only need GA4 and a few events. Add it to your child theme’s functions.php so it loads on every page:

add_action( 'wp_head', function () {
	if ( current_user_can( 'manage_options' ) ) {
		return; // Don't track administrators.
	}
	?>
	<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
}, 1 );

Option 2: Google Tag Manager

Use it when you plan to add more: conversions, the Meta Pixel or other marketing tags. You install one container once, and manage every tag from Tag Manager without touching the theme again.

  1. Create a container for your website in Tag Manager.
  2. Add its first snippet in wp_head and the noscript part right after the opening body tag, using the wp_body_open hook.
  3. In Tag Manager, create a Google tag with your Measurement ID and the Initialization – All Pages trigger.
  4. Click Submit to publish the container. Nothing goes live until you publish.

Don’t count every visit twice

Before adding GA4, look for old tracking codes: theme options with a “tracking code” field, analytics plugins, or a previous snippet in the header. Open your page source and search for gtag and googletagmanager. Each one should appear once.

Consent

If you have visitors from regions that require consent for analytics cookies, such as the EU or the UK, use a consent banner that supports Google Consent Mode. In Tag Manager, consent settings can control when each tag fires.

Check that it works

  • In Tag Manager, use Preview to see which tags fire on each page.
  • In Google Analytics, open Reports › Realtime and visit your site from another device.
  • For detailed checks, use Admin › Data display › DebugView.

Connect Search Console

In Admin › Product links › Search Console links, link your Search Console property. You will see the search queries that bring visitors, next to what they do on your site.

Common mistakes

  • Installing GA4 with a plugin and Tag Manager at the same time.
  • Tagging a staging copy of the site with the production Measurement ID.
  • Counting your own visits. Define your internal traffic and turn on the data filter.
  • Forgetting to publish the Tag Manager container.

Next step: measure what really matters, the leads your forms bring. If you want me to set up your analytics, get in touch. You can also browse my portfolio or read more about me.

Scroll to Top