Best Way to Add Google Analytics 4 to WordPress Without Using Plugin

Add Google Analytics 4 to WordPress

Following Google’s declaration that Universal Analytics would no longer be supported, WordPress users must add Google Analytics 4 to WordPress site in order to continue tracking and analyzing their sites. This article will show you how to easily add Google Analytics 4 to WordPress without any WordPress plugin site step-by-step.

What’s new in Google Analytics 4 (GA4)?

The first is how it monitors users. Universal Analytics(UA) tracks users via sessions, which are predetermined intervals that include everything a user performs on your site. Whereas GA4 is based on events that keep track of all the actions they take when a user revisits a website instead of starting a new session. This approach helps Google better identify and remove duplicate individuals by focusing on their actual actions on your site, rather than visits.

The second distinction is in the reporting. In UA, a variety of pre-built reports can be altered. GA4 only presents top-level findings; to find more specific details, go to the analysis tab. This enables you to drill down to the data that matters to you the most and gives you greater creative control over how your reports look.

The third distinction is how they are put up. UA uses a property and view structure, but GA4 enables you to combine data from your apps and website. A single attribute and data streams from Google Analytics are used to do this. We can use the same tracking code to track a person across many properties (such as a website and mobile app) and we can combine the data. This indicates the existence of a fresh tracking code.

Steps to add Google Analytics 4 to WordPress site

Create a Google account

The very first thing you should have to add Google Analytics 4 to WordPress site without using plugin is to create a Google account. You can use your personal/professional email with your domain name or other email provider address like outlook.com or aol.com.

Create your Google Analytics account

You can visit the Google Analytics website after creating and logging into your Google account. You will see the following landing page if you don’t have a Google Analytics account.


Add Google Analytics 4 to WordPress - Welcome

After you click “Start measuring,”, you’ll then need to enter the Account name for your Google Analytics account with different data-sharing settings. Check all the boxes and then click Next.

Add Google Analytics 4 to WordPress - Account Creation

A second screen will ask you for the name of the property which is the website name where you want to add GA4 and select a reporting time zone and then click Next.

Add Google Analytics 4 to WordPress - Property Creation


You will be prompted to the business details section. Select your respective Industry Category and Business Size and then click next.

Add Google Analytics 4 to WordPress - Business


Finally, choose your business objectives. We advise choosing to Get baseline reports. This will get you all the Google Analytics reports.

Add Google Analytics 4 to WordPress - Business Objectives

A popup will appear where you can see the Google Analytics Terms of Service Agreement. Please review the Google Analytics Terms of Service Agreement depending on your country and the regulations of data protection and accept it.

Add Google Analytics 4 to WordPress - Terms of Service

After accepting the terms of service, you will then be redirected to Data Streams Section where we need to Choose a platform.

Add Google Analytics 4 to WordPress - Data Stream


Click on the Web button to track your WordPress site. Enter your WordPress website URL and Stream name, which is your site name. Then click on Create Stream.

Add Google Analytics 4 to WordPress - Create Stream

Install Google Tag to your WordPress site:

You have now created a Web Stream. Now, in order to add Google Analytics 4 to WordPress and the site, you need to install a Google tag. There are two ways of making this happen. One is by using plugins like MonsterInsights Plugin, Site Kit Plugin, etc.

The other method is installing it manually by adding a Google Tag script to your WordPress site. Here, we apply the manual method.

Go to the Data Streams section and click on the data stream that you just created.

Add Google Analytics 4 to WordPress - stream details

On clicking, you should see the Stream Details. Click on the View tag instructions at the bottom.

Add Google Analytics 4 to WordPress - Tag Instructions

A Google tag installation section will appear. Click on the Install Manually Tab. You now need to copy the gtag script provided and save it to your WordPress site after the element.

Add Google Analytics 4 to WordPress - gtag script

To add gtag script to your WordPress website, go to your respective active theme functions.php file.
Add either of the following PHP code in the functions.php and save it.

/**
 * Adds gtag scripts to the head section.
 */
function wptr_add_gtag_scripts() {
	?>
	<!-- Google tag (gtag.js) -->
	<script async src="https://www.googletagmanager.com/gtag/js?id=G-ZJ6RUE9Q84"></script>
	<script>
		window.dataLayer = window.dataLayer || [];
		function gtag(){dataLayer.push(arguments);}
		gtag('js', new Date());

		gtag('config', 'G-ZJ6RUE9Q84');
	</script>
	<?php
}
add_action( 'wp_head', 'wptr_add_gtag_scripts', 9 );

Or

/**
 * Enqueues and adds gtag scripts to the head section.
 */
function wptr_add_gtag_scripts() {
    wp_enqueue_script( 'gtag', 'https://www.googletagmanager.com/gtag/js?id=G-ZJ6RUE9Q84', array(), null, true );
    wp_add_inline_script( 'gtag', "
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'G-ZJ6RUE9Q84');
    " );
}
add_action( 'wp_enqueue_scripts', 'wptr_add_gtag_scripts' );

Note: Please make sure that you activate a child theme first and make the changes in child theme functions.php.

And that’s all. By following this method, you can successfully add Google Analytics 4 to WordPress site without using any plugins.

You might also like