How to Add Google Analytics 4 (GA4) to Magento 2?

It's no news that without valid data reports, you can't make any relevant decisions about your business, especially in eCommerce. Concise data reports on page views, traffic sources, bounce rates, session times and other important eCommerce events allow you to improve your marketing campaigns. 

As the key GA stats suggest, Google Analytics is one the most widely-used website analytics platforms that provide you with all that data. And since we're all used to it, the news about Universal Analytics going away took us by surprise. 

Many have started to look for an alternative, not even giving Google Analytics 4 a chance, though it deserves it. 

So, in this guide, you'll learn what Google Analytics 4 has to offer and how you can add Google Analytics 4 to Magento 2. You'll see that it is much easier than you think if you use Magento 2 Google Analytics 4 Extension.

What is Google Analytics 4?

Google Analytics 4 (GA4) is a next-generation measurement solution built upon previous Universal Analytics (GA3) properties that have been publicly tested since 2020. It employs machine learning and features comprehensive integrations with Google advertising platforms. It gives you insights into customers' journey across different platforms and devices. 

Besides, while Universal Analytics measures data based on pageviews and sessions, the GA4 measurement model is based on events and parameters.

What does this give you? 

You now don't get only data about views your products get or how much time users spend on certain pages.

You can track what products are added to the cart the most, what custom product blocks (related products,cross-sells,up-cells) get the most views, how many customers go to and finish checkout or how many people register on your store

GA4 Events

GA4 really takes eCommerce tracking to a new dimension. And now due to better integration with Google Ads, you can segment your website visitors using these events and retarget them.  

Benefits of Google Analytics 4

Since we already know that Google Analytics is a huge step forward in data tracking, let's highlight the top advantages of GA4.

  • Data streams

One of the biggest benefits of Google Analytics 4 is that it allows you to track data on your website, mobile app or both in a single property. You can have multiple data streams, like iOS, Web and Android under one property.

Create data streams GA4

  • Customer journey tracking

Google Analytics has changed to its core in the GA4 edition. The emphasis is now on users and their interactions with your store (captured by events)  rather than the sessions.

It allows you to put together a customer journey even if a customer visits your store from a mobile device, then revisits it from a desktop and makes a purchase through an app. 

  • Focus on user engagement

Since GA4 can collect more data and has aligned report menu sections with customers' journey, it allows you to focus on user-centric reports. You can now find Acquisition, Engagement, Monetisation and Retention analysis under the Lifecycle and User section with the demographics and technology.

Google Analytics Dashboard

  • More segmented audiences

More powerful analytics and reports mean more segmented audiences which you can use to create more effective marketing campaigns.

You can create an audience for dynamic remarketing based on those who abandon their cart, those who make purchases, those who have products on their wishlists and much more. Not speaking about its ability to aggregate data from multiple devices and browsers. 

GA4 Create Audience

Opportunities are limitless for your eCommerce store with Google Analytics 4. That's why you don't have to wait till it expires on July 1, 2023. Switch to GA4 now and started getting all these benefits. 

How to Set Up Google Analytics 4 in Magento 2?

Normally to add Google Analytics to Magento 2 you need to insert a Google Analytics tracking code into the <head> of your website. Not only is it inconvenient for those with no technical experience, but ineffective as well. 

The best way to add Google Analytics 4 to Magento is through Google Tag Manager (GTM). GTM gives you the flexibility of adding different eCommerce events to track customer journeys. But you won't even need to do that manually.

Intrigued? Keep reading.

In this guide we focus on client-based (browser-based) tracking. However, you can also enable server-side tracking in Magento using GA4 Measurement Protocol.

1. Create GA4 Property

Before you start to add GA4 to Magento, you need to create a  GA4 property. If you already have one, move to the next step. 

To create GA property go to Google Analytics and press Get started today to start setting it up. You need to specify your Account Name and select the Account Data Sharing Settings.  

Create Google Analytics 4 account

Then move to the Property setup and set the property name and some additional information like reporting time zone and currency.

GA4 property setup

Next, you need to provide information about your business category, size and purpose. It is used by the platform to tailor your experience. 

Business information GA4 setup

Once you agree to Google Analytics Terms you're almost set up. The last thing you need is to create a data stream, meaning tell GA4 where to track data from. So you choose the Web as a platform. 

Create data streams GA4

Then provide the Website URL and Stream name. Don't forget to click on Create stream button and that's it — you've created the Google Analytics 4 property. 

Set up data stream in Google Analytics

2. Get your Measurement ID

Right after you create a GA property, you'll be redirected to the Web stream details where you can find the Measurement ID you'll need later. 

However, if you already have your property set up, go to your Google Analytics account > Admin > Data Streams and click on the data stream you need to collect the Measurement ID from. 

GA4 Measurement ID

3. Set Up Google Tag Manager Account and Container

Since we add Google Analytics 4 to Magento through Google Tag Manager, you need to set it up too. Same as with the GA account, if you've already set up the GTM account, move to the next step. 

Go to the GTM homepage and press the Create Account button. Then, set the Account Name, Country, and Container Name and choose the target platform

Google Tag Manager Configuration

Note: container name is a descriptive title used to associate the container with your website.

4. Get your Public ID, Account ID and Container ID

Apart from Google Analytics Measurement ID, you also need to get some tracking IDs from GTM. Go to the GTM homepage and choose the container you want to use for tracking. 

  • Public ID 

Go to the Admin tab to find the public ID near the container name.

GTM Public ID

  • Account ID

You can find the account ID on the container page when you're in the Workspace tab. It is in the browser address after /accounts/.

GTM Account ID

  • Container ID

The container ID is located in the container page address as well but after /containers/.

GTM Container ID

5. Install Magento 2 Google Analytics 4 Extension

As we've mentioned before, Google Analytics 4 integration with Magento is much easier and less technical if you use the right solution. So the next step is to install Magento 2 Google Analytics 4 extension and manage it from the admin panel. 

6. Paste your GTM and GA4 IDs into Magento Admin

After you install the GA4 extension, go to Stores > Configuration > Magefan Extensions > Google Tag Manager and Analytics > Web Container and paste the GTM tracking IDs you've collected earlier.

Set up Google Tag Manager in Magento 2

Note: if you use or any other custom GTM loaders, choose Head Script and Body Noscript in the Installation Method field.

Then unfold the Google Analytics 4 section and paste your Measurement ID there. Don't forget to Save the settings. 

Google Analytics in Magento 2

7. Generate GTM Tags

You can create different custom events like add_to_cart or initiate checkout in your GTM account manually if you like. However, if you have Google Analytics 4 extension, you don't need to. 

When you're still on the extension configuration page, scroll down to the Export Web Container section and press on the Generate JSON Container & Download File button to get the file with all the essential tags. 

export web container magento

Note: depending on the plan of the Google Analytics 4 extension you have, you'll be able to track different data. Magento 2 Google Analytics 4 Plus and Extra editions offer you more flexibility.

After the file is downloaded, you need to go to GTM homepage > Admin and press on the Import Container option.

Import Google Tag Manager Container

Choose the file to import, the workspace and select whether you want to overwrite or merge the selected workspace with the selected container. 

Note: once you click to choose Existing Workspace, select it from the list (it is usually default). 

Import JSON container into GTM

After you select a workspace you'll be asked to confirm the imported tags, triggers and variables. And once you do you will need to verify the GTM connection.

Updated GTM Tags and Triggers

8. Test the GTM Connection

GTM allows you to check if Google Tag Manager is working correctly right from the workspace. While you're on the container page workspace, click Preview and enter your Website URL in a popup. 

Preview Google Tag Manager

Connect tag assistant to your website

The URL will open in a new browser tab, and you'll see a success message back on the GTM page. Click Continue to see all monitored tags in the Tag Assistant.

Connect Tag Assistant with your store

Here's also where you can test the events by triggering them on your website.

Tag assistant panel

9. Check Google Analytics 4 in Realtime

If the GTM tags work properly then they are collecting some data about your website visitors. So, you can go to GA4 > Reports > Realtime and monitor the activity on your store. You have added Google Analytics 4 to Magento 2!

Additionally, you can debug GA4 to make sure the data is received properly by your property.

Google analytics report

Though it might look too complicated compared to Universal Analytics, you'll grow to love it for all the perks it offers. Track almost any action customers take on your store and use the collected data for dynamic remarketing campaigns or conversion tracking.