Install GA4 with GTM in wordpress without plugin

Last edited:
October 26, 2023
Reading time:
9 mins

/

Blog

/

Marketing

/

Install GA4 with GTM in w...

💡We may earn a commision if you subscribe to a service from a link on this page.

In today’s digital landscape, understanding user behavior and gathering crucial data is paramount for success in the online realm. Google Analytics 4 (GA4) emerges as a powerful analytics platform that provides deeper insights into user interactions, allowing you to make informed decisions and refine your online strategy. In this comprehensive guide, we’ll explore the significance of GA4, its unique features, and most importantly, how to install it in your WordPress website without relying on plugins.

The Importance of Google Analytics 4 (GA4)

Before delving into the installation process, let’s understand why GA4 is a game-changer for website owners and marketers alike. GA4 is the next generation of Google Analytics, designed to adapt to the evolving digital landscape and provide a more comprehensive view of user behavior. It offers advanced tracking capabilities, improved cross-device tracking, and enhanced reporting features that empower businesses to gain deeper insights into their audience’s interactions.

One of the standout features of GA4 is its event-based tracking system, which offers greater flexibility in defining and tracking specific user actions, such as clicks, form submissions, and video views. This enables you to monitor key interactions and user journeys more effectively, helping you optimize your website’s performance and conversion rates.

Differences Between Implementing GA4 Without GTM and With GTM

Implementing GA4 Without GTM:

In this approach, you manually add the GA4 tracking code directly to your website’s pages. To track specific user interactions, like when someone clicks a button or submits a form, you need to create custom tracking code for each of these events. This method requires coding knowledge, and as your tracking needs evolve, you’ll have to edit your website’s code repeatedly.

Implementing GA4 With GTM:

When using Google Tag Manager (GTM), you insert the GTM code on your website just once. After that, you use the GTM interface to set up and manage tracking events. You can configure tracking for actions such as button clicks or form submissions without needing to edit your website’s code directly. GTM simplifies the process, making it more accessible for individuals without coding skills. It allows for easy event customization and rule-based tracking configurations, reducing the need for manual code updates.

The Benefits of Using Google Tag Manager (GTM)

While there are various methods to integrate GA4 with your WordPress website, utilizing Google Tag Manager (GTM) is arguably the most efficient and flexible approach. GTM acts as a centralized hub for managing all your tracking codes and scripts, streamlining the process and reducing the need for manual coding.

One of the primary advantages of GTM is its simplicity and user-friendliness, making it accessible even to those without extensive coding knowledge. GTM allows you to deploy and manage various tracking tags, including GA4, through a user-friendly interface, eliminating the need to manually edit your website’s code.

Moreover, GTM offers the convenience of centralized tracking management. Whether you’re running Google Ads, Facebook Pixel, or other marketing campaigns, GTM enables you to consolidate all your tracking codes in one place. This not only simplifies the setup but also ensures accuracy and consistency in your tracking efforts.

Installing GA4 with GTM in WordPress

Now that we’ve established the importance of GA4 and GTM, let’s delve into the step-by-step process of installing GA4 with GTM in your WordPress website. This method offers a seamless way to harness the power of GA4 without relying on third-party plugins, giving you more control over your tracking setup.

Step 1: Setting Up a Google Analytics 4 Property

The first step is to create a Google Analytics 4 property if you haven’t already. Go to the Google Analytics website (https://analytics.google.com/) and sign in with your Google account. Once logged in, if this is the first time you create a Google Analytics account, you will see the welcome screen with Start Measurement button:

 

If you already have a Google Analytics account for another property, you must navigate to the Admin section and click on “Create Account.”:

After hitting Creating Account, you’ll be redirected to the account details which is the first from 5 steps you need to complete in order to create your account:

Next we must add property details, business details, business objectives and accept the terms:

 

On the last step, we choose Web as a platform, since we are running a WordPress website:

After completing the steps, our last action is to finalize our data stream. Enter your website URL and Steam name:

When we click Create stream, we have completed the creation of our new Google Analytics 4 account! We are redirected into the details of our Google Analytics property from which we are going to grab Measurement ID. This will help us implement GA4 in our website with the help of Google Tag Manager:

Beware! An automatic pop up will open and you will be shown the gtag.js code after you complete the installation We don’t need this and we can ignore it for now. We only need this for manual installation of Google Analytics 4, without the use of Google Tag Manager. The only parameter we need from our Google Analytics 4 account is the Measurement ID which we obtained in the previous action:

That’s it! You now have all the necessary information and prerequisites we want in order to install Google Tag Manager and Google Analytics 4 to our website.

Step 2: Creating a Google Tag Manager Account

If you don’t already have a Google Tag Manager account, you’ll need to create one. Visit the Google Tag Manager website (https://tagmanager.google.com/) and sign in with your Google account. Click on “Create Account” and follow the setup wizard to create your GTM account.

Choose Web as your target platform:

After clicking Create, our new Google Tag Manager container is ready and installation instructions are presented to us:

Step 3: Configuring Google Analytics 4 in Google Tag Manager

With GTM set up, you can now configure your GA4 tracking within the GTM interface. Inside your GTM container, click on “Tags” and then click “New” to create a new tag.

Choose the “Google Analytics” tag type.

And then, choose Google Tag:

Step 5: Adding Your GA4 Measurement ID

In the GA4 Configuration tag, you’ll need to input your GA4 Measurement ID. Now is were the Measurement ID we obtained from Google Analytics 4 comes in handy. Add the Measurement ID to the Tag ID field:

Step 4: Set a trigger for your GA4 Setup

Triggers define when your GA4 tag should fire. You can create triggers based on specific events, page views, or interactions. For instance, if you want to track page views, you can set a trigger to fire on all page views. On our specific case, since we want the code to run in every page view, we choose All Pages trigger:

 

So the final result should look like this:

 

Step 6: Publishing Your Configuration

Once you’ve configured your GA4 tag and triggers, click “Save” and then “Submit” to publish your configuration to your live website. This action makes your GA4 tracking go live, allowing you to collect data immediately.

Step 7: Adding the GTM Container Code to Your Website

Upon creating a container, GTM will provide you with a container code snippet. This code should be added to your website’s header and body section, for the script and noscript tags respectively. To do this, access your WordPress child theme’s files through your theme editor, your hosting control panel or FTP client and add the below code. Make sure you replace the Tracking Codes with your codes that you obtained in step 2.

<?php 

/*** ... your functions.php code ***/


// Insert GTM script in header.
function insert_gtm_head() {
    ?>
    <!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->
    <?php
}
add_action('wp_head', 'insert_gtm_head');

// Insert GTM noscript code in the body.
function insert_gtm_body() {
    ?>
    <!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
    <?php
}
add_action('wp_body_open', 'insert_gtm_body');

Step 8: Verifying Your Setup

To ensure your GA4 tracking is working correctly, you can use the Google Tag Manager preview mode (debug mode). To make sure that Google Analytics 4 are properly setup, visit your GA4 dashboard and check stats as the real-time reporting in your GA4 property. Verify that events and data are being recorded accurately. Remember that there is some time needed for you to see tome updates in your dashboard (except from real-time stats), so have a few hours patience before making your final checks.

Add Google Analytics 4 with Google Tag Manager in WordPress

In conclusion, installing GA4 with GTM in WordPress without plugins provides you with a robust tracking solution that’s both flexible and manageable. This setup empowers you to harness the full potential of GA4’s advanced analytics while maintaining control over your tracking efforts. Moreover, using a simple snippet to your child theme guarantees that your tracking remains intact, even as you make updates and improvements to your website. With GA4 and GTM in place, you’ll gain invaluable insights into user behavior, enabling you to make data-driven decisions that drive your website’s success.