search ]

Google Analytics 4 – WordPress Installation Guide

On July 1, 2023, Google’s Universal Analytics (UA) ceased processing data. You can continue to view data collected before this date for a certain period, but any new data received from this date onward will be visible exclusively in Google Analytics 4 (GA4), which is essentially the next generation of Google Analytics.

In this guide, we will explain the differences between GA4 and UA and show how to install the tracking code for GA4, whether there is an existing analytics tag from the old version or if it’s an entirely new installation of Google Analytics.

If you want to skip the theoretical part and go straight to the practical section, here are two parts of the article. The first one is about installing Google Analytics 4 for new setups, and the second one is about installing Google Analytics 4 on a site where Universal Analytics already exists.

A Few Words About Google Analytics 4

In a few words, Google Analytics 4 is a new type of asset that offers several advantages compared to the previous asset of Universal Analytics. It can be said that the main change is that in Google Analytics 4, everything revolves around users (users) and events (events), not sessions as we were used to.

The event-based model processes each user interaction as an independent event, a significant change from the previous session-based model that grouped user interactions over a certain period.

I assume this might not tell you much, so for the sake of simplicity, let’s just say that the model change provides significant benefits for marketers, offering much greater flexibility in monitoring user behavior on the site.

Here are some additional advantages of Google Analytics 4 compared to the previous version:

  • Focus on preserving privacy.
  • Use of machine learning to understand the customer journey until conversion across different devices.
  • Allows smoother integration with Google’s advertising platforms (Google Ads), enabling optimization of campaign performance leading to better marketing results.

Keep in mind that there are many aspects beyond that in which Analytics 4 differs from Universal Analytics, both in terms of reporting and in the calculation of various metrics such as Users, Pageviews, Bounce Rate, and Conversions. For more information.

So, as I mentioned, there are two options – one in the case of the initial and new installation of Analytics 4 on a site without an old version of Analytics (or when Google does not allow connecting between the two versions), and the second for those who install Google Analytics 4 on a site where an older version of Analytics already exists (and it is possible to connect between the two versions). Below is a guide for each option.

New Installation of Google Analytics 4

If this is your first time dealing with Google Analytics, the first thing you need to do is create an account and then create a new Analytics property under that account.

Creating a property will provide you with the tracking code that you will need to install on your site to monitor and track user movements through the Analytics platform. In any case, the process is very simple and short. Here are the steps to do this:

  1. Login with your Google account to the Analytics management interface.
  2. Click the blue button Start Measuring.
    Step 1 - Opening a Google Analytics account
  3. In the screen in front of you, choose a name for your account (an account can contain multiple different Analytics properties). The bottom part is less relevant, and you can leave it as default.
    Google Analytics 4 - Account Details
  4. Clicking the blue button will take you to a screen where you need to choose a name for the property you want to create. Choose a name that describes the site where you want to embed Analytics and click the button leading to the next screen. (The additional details in this screen explain themselves).
    Google Analytics 4 - Property Setup
  5. In this screen, you need to choose some details about the business in question, namely details about the site where you plan to embed Analytics. Choose the appropriate details and click the blue button again (you can change these details later).
    Google Analytics 4 - Business Information
  6. Now you need to agree to the terms of use that Google will present to you. When you do this, you will move to the Data Streams screen in the Analytics management interface, where you need to choose where you intend to embed the tracking code. In the case of a website, choose Web.
    Google Analytics 4 - Data Streams
  7. In the next screen, you will need to enter the domain of the relevant site and choose any name for that Data Stream. Leave the default under Enhanced measurements at the bottom and click Create Stream.
    Google Analytics 4 - Set Up a new Data Stream
  8. The next screen will show you information about the new property you created, as well as the MEASUREMENT ID. Now, to get the tracking code, click on the yellow button View Tag Instructions.
    Google Analytics 4 - Data Stream Details
  9. In the next screen, click on the Install Manually tab to get the tracking code for the Analytics property you created. You will need to embed this code on your website pages (detailed explanation later).
    Google Analytics 4 - Get Tracking Code

You have completed this part! Now, all that’s left is to install the tracking code you received on your WordPress site…

Installing Google Analytics 4 Tracking Code on a WordPress Site

The tracking code you received can be installed on WordPress sites in several ways. Here are some of them:

A. Adding GA4 Tracking Code via functions.php File

One way to add this tracking code to WordPress sites is manually. Connect to the server via FTP, edit your functions.php file in your theme (in the child theme if it exists), and add the following function (without the opening PHP tag on the first line!):

function add_analytics_ga4_tag() {
       /*** Your google tracking code here ***/
add_action( 'wp_head', 'add_analytics_ga4_tag', 100 );

B. Adding GA4 Tracking Code Using a Plugin

If you don’t feel comfortable doing this with code, there are plugins available that allow you to add GA4 tracking code. One such plugin is a simple one called Insert Header and Footer.

Install and activate the plugin. Then, go to the plugin settings under Settings > Insert Headers and Footers Scripts and add the tracking code you received under Scripts in Header, and you’re done.

C. Adding GA4 Tracking Code Using Google Tag Manager (GTM)

You can also add the tracking code through Google Tag Manager, which is Google’s tag management system. Here’s how to do it:

  1. Log in to your account on Google Tag Manager.
  2. Add a new tag and give it a name (e.g., GA4 Analytics).
  3. Under Tag Configuration, choose Google Analytics: GA4 Configuration.

    Choose GTM Tag Type for Analytics 4
  4. Under Measurement ID, enter the ID of the new property you created. You can find this ID on the property settings page in the GA4 management interface under Property > Data Streams. Clicking on the Data Stream itself will display the same Measurement ID (among other things) that you need to enter in the tag you created in Tag Manager. Add it and click Save.

    How to Find Measurement ID in Google Analytics 4
  5. Now, Tag Manager will ask you if you want to create a Trigger, indicating when to load that GA4 tag you just created. We want to load it on all pages (in most cases), so create a new Trigger, choose All Pages, and click Save.
  6. You have completed this part; don’t forget to publish the changes in Tag Manager by clicking the blue Submit button on the main screen.

D. Adding GA4 Tracking Code Using the WordPress Admin Interface

There is also the option to copy the tracking code through the WordPress admin interface. Go to Admin > Appearance > Editor, edit the header.php file, and add the code as shown below:

הוספת גוגל אנליטיקס דרך ממשק הניהול של וורדפרס

Adding GA4 via WordPress Dashboard

Hereby, the process has concluded. If you now go to Google Analytics, you’ll be able to see the data for which you worked so hard in the Google Analytics interface.

A recommended step to take after completing this process is to link Google Search Console to Google Analytics 4. Take a look at the attached link! (If you’ve finished reading this post)

Adding Analytics 4 to a Site Already Using Old Google Analytics

If you’ve reached this part, it’s likely that your site already has the Universal Analytics tracking code, and you want to install the Google Analytics 4 tracking code in addition.

It’s important to note that adding a new Google Analytics 4 property will not affect data collected by UA in any way. Data will be collected in parallel, and there will be no changes in the data received in UA in any way. Additionally, after adding a GA4 property, you can access both properties at any time – the UA property and the GA4 property.

I’ll add and mention that at the current stage, data is no longer received in Analytics from the old version. Furthermore, adding a GA4 property will not transfer data from the old Universal Analytics property but will only collect future data. If you want to view historical data, use UA reports.

Here are the steps you need to take to install GA4 on a site where UA already exists:

  1. Click on the Google Analytics admin interface on Admin.
  2. Under the Account column, make sure you are in the desired account.
  3. In the Property column, select the Universal Analytics property – the property that currently collects data on the site.
  4. In the same column, click on the first option GA4 Setup Assistant. Adding GA4 to a site with existing Analytics - Step 1
  5. Click on Get Started.
  6. In the window that appears, click on Create Property. Please note, if you see the option to mark “Enable data collection using existing tags” (and it is marked), when you complete the steps, you can theoretically already see the data in the new GA4 property you created, and you don’t have to add the new Analytics 4 tracking code. But if this option is not marked, you will need to add the tracking code yourself as described in the previous section.Adding GA4 to a site with existing Analytics - Step 2
  7. At this stage, you will receive a message that you have successfully connected the UA property to the new GA4 property. Clicking on See your GA4 property will take you to the GA4 property and its reports.

Please note – creating a Google Analytics 4 property in this way will copy the property name, website address, region and time settings, and even the currency settings from the existing Universal Analytics property.

A recommended step to take after completing this is to link Google Search Console to Google Analytics 4. Take a look!

This concludes it… And to celebrate this – here’s a video (in English) explaining and going over the new interface of Google Analytics 4.

I also mention that I’m aware this topic can be a bit confusing, so questions in the post comments are welcome. Either way, I constantly improve the writing and readability of the post so that the information is easier to understand 🙂

Appendix A – Why should I add a new Google Analytics 4 tracking code to a site with the old Analytics version?

If the Universal Analytics property belonging to the site uses gtag.js, the GA4 setup tool will allow you to reuse the existing tag (i.e., the tracking tag of the Universal Analytics property).

In this situation, you don’t need to add a new tracking tag to your website pages; you just need to ensure that both properties are linked. In this case, you can already see data being received in the new Analytics 4 property without the need for additional action on your part.

However, it’s possible that you may need to wait up to 30 seconds to see the data…

On the contrary, if the existing Universal Analytics property uses analytics.js, the GA4 setup tool will not allow you to reuse the existing tag and connect between the old and new properties. In this situation, you need to add the new tracking tag to all your website pages.

Appendix B – How to Find the Google Analytics 4 Tracking Code

Let’s see how to find and add the new GA4 tracking tag. I remind you that adding this tracking tag will not affect existing data received in the UA property if it exists; it will simply lead to receiving data in both properties simultaneously.

Let’s start: In the settings of the property you created for GA4 under Data Streams under the Property column, as shown in the following image:

Finding the Google Analytics 4 Tracking Code

If you don’t see the Data Streams option, you are likely currently in the settings of the Universal Analytics property and not the new GA4 property you created.

Now, under the Tagging Instructions option, click on Add new on-page tag and then on Global site tag (gtag.js). The code you need to add to the <head> of the website will appear before you.

Tagging Instructions for Adding GA4 Tracking Code

Up to here. If you’re interested in the topic, take a look at the post I wrote about tracking conversions in Google Analytics 4.

Roee Yossef
Roee Yossef

I develop custom WordPress themes by design. I love typography, colors & everything between, and aim to provide high performance, seo optimized websites with a clean & semantic code.


Leave a Comment

Add code using the buttons below. For example, to add PHP click the PHP button & add the code inside the shortcode. Typo? Please let us know...