Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.


How to add Google Analytics 4 code to Magento 2?

Vinh Jacker | 05-09-2017

How to add Google Analytics 4 code to Magento 2?

The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

Google Analytics 4 (GA4) is a must-have tool for website owners and marketers as it helps to efficiently and comprehensively track website performance. With the data GA4 provides, online businesses can easily define or adjust strategies for maximum profits.

It’s undeniable that the old version, Google Analytics (Universal Analytics), was one of the most widely used web analytics tools. However, with the improvement of technology, it can not stay still. This is why GA4 was built and put into testing in 2019.

Previously, during GA4’s beta development, we had to run both versions together. But since July 1st, 2023, GA4 is fully capable of replacing GA and running independently.

If you want to stay competitive in the e-commerce world, now is the time to switch from GA to GA4 for your Magento store.

Note: This article has been updated because the previous Google Analytics version is now outdated and no longer available. We hope you will enjoy our fresh new instructions below.

The benefits of Google Analytics 4 compared to GA

Let’s discuss the major advantages of Google Analytics 4, a significant leap in data tracking.

  • Multiple data streams

One of the main reasons why Magento 2 Google Analytics 4 is so sought after is because it allows users to track data from their website, mobile app, or both within one property. This includes having various data streams such as iOS, Web, and Android all under a single property.

Data streams

  • Customer tracking

GA4’s fundamental shift is in its approach. It focuses on users and their interactions with your store rather than just sessions. This allows you to map a customer’s journey, even if they switch between devices like mobile and desktop before making a purchase through an app.

  • User engagement

With its enhanced data collection capabilities and user-centered report sections, GA4 emphasizes user-centric reports. You’ll find analysis categories like Acquisition, Engagement, Monetization, and Retention below the Lifecycle and User section, along with demographics and technology.

User engagement

  • Advanced audience segmentation

Another great feature of Magento 2 Google Analytics 4 is that its robust analytics and reporting enable more refined audience segmentation. This means you can create more effective marketing campaigns. Whether it’s targeting those who abandoned their shopping carts, completed purchases or added products to their wishlists, GA4 offers powerful options. Additionally, it can aggregate data from various devices and browsers.


The opportunities for your eCommerce store with Google Analytics 4 are boundless. Don’t wait until it’s no longer available after July 1, 2023. Make the switch to GA4 now and start reaping these benefits.

Types of events in Google Analytics 4

GA4 has four types of events, and each has specific use cases. These events include:

  • Automatically Collected Events
  • Enhanced Measurement Events
  • Recommended Events
  • Custom Events

Automatically Collected Events

After setting up GA4 on your website or app, these events will be collected automatically without the need for additional coding or tagging. They encompass a comprehensive list provided by Google, including advanced measurement events. Some examples of automatically collected events are session start, first visit, and user interactions.

Collected event

Enhanced Measurement Events

These events are gathered in GA4 when you activate enhanced measurement events, which are useful for tracking actions that previously required separate event setups.

For example:

  • Page view, which triggers each time a user reloads a page.
  • Scroll tracking, records when a user reaches the bottom of a page (noted at depths like 25%, 50%, or 75%).

Measurement event

  • Outbound click events, monitoring clicks leading to external websites.
  • Site search events, triggered when a visitor performs a search on your website based on query parameters.
  • Video interaction events, covering video actions like start, progress, and completion on embedded website videos.
  • File download events, tracking whenever a user downloads common file extensions. Scroll down this page for a full list.

You can also disable individual events or access their advanced settings from the same setup section. From the advanced settings, you have the ability to toggle off any events that you don’t want to track.

Google Analytics 4 recommends these events in the event reports, but they won’t be automatically activated or tracked unless you set them up manually with the exact event names and parameters Google suggests.

Although tracking GA4 events is quite flexible in terms of the event names and events you can track, these recommended events require you to adhere to Google’s suggestions to activate and track them correctly.

These suggested events are categorized into three main business verticals:

  • All Properties
  • Online Sales
  • Games

You can review the list of these events here. They appear in the Event reports as suggestions.

If you want to track events that already exist in any business vertical, it’s better to use the event names and parameters recommended by Google. This ensures proper tracking and the collection of high-quality data.

Custom Events

What if you need to keep an eye on things like form submissions or button clicks that aren’t covered by the categories we talked about earlier? That’s when you can use custom events in GA4. These aren’t set up automatically, so you’ll have to do it yourself by giving your events specific names.

Custom events offer the flexibility to track actions that might not be covered by Google’s predefined recommendations.

Custom event

How to add Google Analytics 4 code to Magento 2 with GTM?

There are two ways to set up GA4 into Magento 2. The first, also what people normally do, is to place a GA tracking code into the website’s <head>. However, this method can be tricky if you’re not a tech expert. Additionally, every time you want to launch a campaign with a third-party tool, you need to insert a piece of code into your store. This can negatively impact your website’s speed, especially when running large campaigns.

The best way is to use the Magento 2 Google Tag Manager (GTM) extension. Although the default Magento does support GTM, it only works for the old GA3 version. So, to use GA4 through GTM to reduce manual code modification, we recommend using the M2 Google Tag Manager module.

Notably, Mageplaza’s M2 GTM extension is an advanced tool, seamlessly combining GTM and GA4 together. Instead of creating campaigns with GTM and then switching to GA4 for data tracking, with our solution, you can easily handle everything in one place.

Let’s go through it step by step!

Set Up Google Analytics 4

Step 1. Create GA4 Property

To add GA4 to your Magento 2 store, you must first set up a GA4 property. If you’ve already done that, please move to the second step.

Alright! Let’s begin!

  • Go to Google Analytics > click the Get started today button on the top right corner > press Start measuring to create your GA4 account.
  • Provide your Account Name and Choose the Account Data Sharing Settings.

Create GA account

  • Click Next to move on to Property creation.
  • Enter the property name and set the required information fields, including reporting time zone and currency.

Create property

  • Click Next to move on to Business details. Then, choose your business category and size.

Business details

  • Click Next to move on to Business objectives and choose your target. Remember, you can choose multiple answers in this section.

Business objectives

  • Click Create > choose I accept to agree with the terms of service.

Terms and conditions

  • Next, you need to create a data stream which means specifying where GA4 should track data from. In this case, select Web as your platform.

Choose platform

  • Enter your Website URL and its Stream name.

URL and stream name

  • Finally, click on Create stream. Now, you have finished creating a GA property.

Step 2. Get Measurement ID

Once you’ve set up a GA property, you’ll be taken to the Web stream details. Here, you can find the Measurement ID, which you’ll need later.

In case you already have a property, just go straight to your GA account, then click on Admin and go to Data Streams. From there, select the data stream you want and obtain the Measurement ID.

Measurement ID

Step 3. Create a GTM Account and Container

Since we are using Google Tag Manager to add Google Analytics 4 for Magento, we need to set up GTM too. If you’ve already got a GTM account, just move to the next step. If you haven’t, follow our instructions below.

  • Visit the GTM homepage> click on the Create Account button.
  • Next, fill in the required fields, including Account Name, Country, Container Name, and Target platform.

Create GTM account

Remember: The container name serves as a descriptive title that helps link the container to your website.

Step 4. Locate Account and Container ID

In addition to the Google Analytics Measurement ID, you’ll also need to obtain some tracking IDs from GTM. Simply head over to the GTM homepage and select the container you intend to use for tracking.

  • Account ID

For account ID, you can locate it on the container page while you’re in the Workspace tab. It appears in the browser address right after /accounts/.

Account ID

  • Container ID

The container ID can be found easily in several places, such as right when you log into the GTM account Container ID

or next to the container name in the Admin tab Container ID

Add GA4 to Magento 2 Store

After setting up GTM, it’s time to add Magento 2 Google Analytics 4.

Note: Mageplaza Google Tag Manager extension also includes Google Analytics 4 so both of them operate in the same way.

Google Tag Manager

Google Tag Manager for Magento 2

Easily monitor store stats and adjust marketing tags and snippets with simple source code

Check it out!

  • Remember the Container ID before? Copy and paste it into your Magento store.
  • Go to the admin backend > Stores > Configuration > Mageplaza Extensions > Google Tag Manager.

Navigate to GTM

Paste Google Tag ID

  • Select Yes in the Enable field to turn on the extension.
  • Paste the container ID you got on the Admin page before in the Google Tag ID field.
  • Save Config.

Check GTM on your site

Checking whether Google Tag Manager has been added to your store is an essential step to make sure all your efforts above worked.

There are 2 ways to do so, and both are very simple.

Method 1:

  • Open the home page.
  • Press CTRL + U to open the view source.
  • Press CTRL + F to find GTM.
  • If you find gtm in the code source like below, then congratulations, Google Tag Manager has successfully been added to your site.

Method 2:

  • Click on the Preview button in the Workspace tab.

Preview button

  • Enter your site URL.

Site URL

  • If everything is configured properly, a confirmation message will appear as follows:


Check Realtime GA4

Now that we’ve ensured the Google Analytics 4 Global Site Tag is active on your store, let’s check if Real-Time data is flowing into your Google Analytics 4 property. This data will help us start collecting information about customer behavior!

First of all, log in to your GA account and ensure that the Google Analytics property we created earlier is selected.


Secondly, navigate to the “Home” section using the menu on the left-hand side. Please note that you won’t see much data here at this point, as it can take about 24-48 hours for reports to begin generating based on the incoming data. However, you can click the “View Realtime” link, and you should be able to view the current visitors to your store, including yourself.

GA4 data

The data displayed here, as well as in other sections of your Google Analytics account, may be limited to basic information that the property collects. Nevertheless, it should provide you with a well understanding of what visitors are doing on your Magento 2 store!

If you want to learn more about Google Tag Manager, please visit our blog: How to add Google Tag Manager (GTM) to Magento 2 store


GA4 might seem a bit complex compared to our old friend Universal Analytics, but once you get to know it, you will fall in love with all its perks. By following our step-by-step instructions to add Magento 2 Google Analytics 4, you can track almost every customer action on your store and take advantage of these data for conversion tracking or dynamic remarketing.

Table of content

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

    Related Post

    Website Support & Maintenance Services

    mageplaza services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.