How to Set up Magento 2 Google Analytics 4 (GA4)? – Guide

Updated on: Oct 17, 20248 min read Magento

Looking to set up Google Analytics 4 in Magento 2? Follow this complete guide till the end for a step-by-step tutorial.

Google Analytics 4 (GA4) is a powerful tool to get insights into the customer’s journey and experience in your store. It offers on-of-its-kind analytics and reporting that can help you make better data-driven business decisions.

However, integrating Magento 2 Google Analytics 4 is not straightforward. Therefore, as an expert in this field, we prepared a complete step-by-step guide.

Why Integrate GA4 with Magento 2?

Here are some reasons why every Magento store owner should set up Google Analytics 4 tracking:

  • Event-based tracking, which means more granular data for you
  • User engagement metrics offer a bird-eye view of the customer’s journey
  • Track your website and app users under the same property in Google Analytics 4
  • GA4 supports exporting datasets to BigQuery for further analysis
  • Magento 2 Google Analytics 4 offers more real-time insights into the user’s journey

Lastly, it’s a free tool offered by Google that can help you get insights to improve your store’s experience and make informed decisions. You can also use GA4 set up to track Google Ads conversions in your Magento 2 store.

Methods to Add Google Analytics 4 to Magento 2

There are two ways you can set up Google Analytics 4 in Magento 2:

  • Use Extension: This is the easiest method to set up Google Analytics 4 tracking via GTM. You can use a Magento 2 GA4 extension that helps you quickly set up integration, create events, and start tracking data.
  • Set up Manually: You can add the GA4 tracking code manually to your Magento 2 store. This enabled basic GA4 setup and does not include any e-commerce events like checkout and purchase.

In this tutorial, we cover the easiest method to set up integration and start tracking your data in Google Analytics 4.

How to Set up Magento 2 Google Analytics 4 (GA4) via GTM?

To set up GA4 in Magento using GTM, first, you’ll need to create Google Analytics and Google Tag Manager accounts for your website property. (If you already have them, you can simply skip Steps 1 & 2.)

Follow these steps till the end to set up GA4 in Magento 2 and start tracking data.

Step 1: Create a GA4 Property

Go to analytics.google.com and log in using your Google account.

Next, click the Start measuring button to set up a new Google Analytics property.

Create a Google Analytics account for Magento 2 GA4 tracking

Enter a name for the account and set your account data-sharing preferences.

Click Next.

Enter account details for Magento 2 GA4 account

Now, enter a name for your GA4 property, and set the reporting time zone and the preferred currency.

Click Next.

Create a property for set up Magento 2 Google Analytics 4

Select your business industry and business size.

Then, click Next.

Describe your business to set up Google Analytics 4 in Magento 2

In the next screen, you’ll need to choose your business objectives. These preferences will affect the type of default reports shown by GA4. You can select multiple options too

Click the Create button.

Set business objectives of your Magento 2 store

Read and accept Google’s Terms of Service agreement displayed on the screen to create your GA4 property.

Select Web (since we’re setting up GA4 for the Magento 2 online store).

Create a web stream to set up Magento 2 GA4

Enter your Magento 2 store URL and name for the data stream.

Also, ensure that the Enhanced Measurement feature is enabaled.

Click Create Stream.

Enter Magento 2 website URL and turn on enhanced ecommerce tracking in GA4

Once the web stream is created, you’ll get a screen like shown below.

Note down the Measurement ID displayed on the screen (you’ll need it in the upcoming steps).

Copy GA measurement ID from the stream
Magento 2 Google Analytics 4 (GA4)

Integrate Google Analytics 4 with your Magento 2 store via GTM to track your customers entire journey on your website.

Get Extension

Step 2: Create a GTM Container

Go to tagmanager.google.com and click the Create Account button.

Create a GTM account to add Google Analytics to Magento 2

Enter your Account name, Country, and Container name, and create your account.

Select Web in the target platform and click Create.

Create container in GTM for Magento 2 GA4

Once created, you’ll get the GTM codes that you can use to install GA4 in Magento 2 via GTM. In this screen, you’ll find:

  • JS code
  • Non-JS code
  • Account ID
  • Container ID

as shown in the screenshot below.

Get GTM details

Keep this tab open as you will need these details while setting up GA4 in Magento 2.

Step 3: Add Your GTM Codes to Magento 2

Log into your Magento 2 store and go to Stores > Configuration > Meetanshi > GA4 to configure the GA4 extension.

Enable the extension and paste the JS and Non-JS codes that you get in the previous step.

Enable Magento 2 Google Analytics 4 extension

Step 4: Configure Magento 2 GA4 Settings

Scroll down to configure the Magento 2 GA4 tracking settings.

You can choose the variable to use for product identification, enable product brands, and other settings.

Configur GA4 tracking in Magento 2

Step 5: Configure GTM API Settings in Magento 2

Scroll down to the GTM API Configuration section and add the following details:

  • Account ID: It’s the numeric value after /accounts/ from the GTM URL (see Step 2)
  • Container ID: It’s the numeric value after /containers/ from the GTM URL (see Step 2)
  • Measurement ID: It’s the ID of your web stream (see Step 1)
GTM API configuration in Magento 2 GA4 extension

Step 6: Generate & Download GTM JSON

In the Google Tag Manager, close the popup and you’ll see the Public ID as shown below.

Copy GTM ID

Come back to the Magento 2 GA4 extension configuration and scroll down to the JSON Export Options section.

Paste the Public ID and click Generate JSON for GTM.

Download the generated JSON file.

Generate JSON for GTM tags in Magento 2

Once, all the configuration settings are done, click the Save Config button at the top-right.

Step 7: Import Container into GTM & Publish Changes

Go to Google Tag Manager and navigate to Admin > Import Container in your workspace as shown below.

Import container in GTM to set up GA4 in Magento 2

In the Import Container popup, upload the JSON file that you downloaded from the Magento 2 GA4 extension.

Select the Workspace as Default Workspace and the import option as Merge.

Click the Confirm button.

Import Magento 2 GA4 container to GTM

Now, the list of triggers from the uploaded JSON will be shown.

Click Submit.

Submit GTM Tags to add magento 2 ga4 enhanced ecommerce tracking

To submit the GTM container, enter a name and description for the version.

Click the Publish button.

Publish GTM container for GA4 Magento 2 tracking

And that’s it. You’ve successfully set up GA4 in Magento 2 via GTM.

You can enable the Debug mode in GA4 to test the connections or wait a few hours for Google Analytics to populate the reports with data. In most cases, you will be able to use the real-time report to quickly test the connection.

Magento 2 GA4 tracking screenshot

That’s how simple it is to integrate Google Analytics 4 in Magento 2 using Meetanshi’s extension.

List of E-commerce Events You Can Track

The GA4 extension by Meetanshi helps you track all the important events for enhanced e-commerce tracking. By default, you’ll be able to track all these events using the extension:

GA4 EventDetails
add_payment_infoUser adds payment details
add_to_cartUser adds a product to the cart
add_to_compareUser adds a product to compare
add_to_wishlistUser adds a product to the wish list
begin_checkoutUser begins checkout
purchaseUser completes the payment and places an order
remove_from_cartUser removes a product from the cart
select_itemUser selects an item from the list
select_promotionUser applies a promotion to the cart
shipping_infoUser adds shipping details
view_cartUser views the cart
view_itemUser views the item
view_item_listUser views the item list
view_promotionUser views the promotion

How to Add Google Analytics 4 Code to Magento 2?

From your GA4 account, copy the gtag.js script and follow these steps: 

  • Log into your Magento 2 admin panel and navigate to Content > Design > Configuration
  • Click Edit against the default design configuration for the store
  • Expand the HTML Head section and paste the copied script
  • Click the Save Config button

The Google Analytics code will be successfully added to your Magento 2 store. However, note that you’ll need to create custom events in your GA4 account manually since this method only tracks the default ones.

What is the Best Magento 2 Extension for Google Analytics 4?

Magento 2 Google Analytics 4 (GA4) by Meetanshi is the best extension to set up GA4. It’s easy to use, accurate, and supports tracking all major events without any coding.

Shivbhadrasinh Gohil
Article by Shivbhadrasinh Gohil

Shivbhadrasinh is the Co-founder & Chief Marketing Officer at Meetanshi. He leads the marketing team and is the person behind the marketing & branding success of the company. Being a… Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

Order Your Free SEO Report