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.
Enter a name for the account and set your account data-sharing preferences.
Click Next.
Now, enter a name for your GA4 property, and set the reporting time zone and the preferred currency.
Click Next.
Select your business industry and business size.
Then, click Next.
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.
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).
Enter your Magento 2 store URL and name for the data stream.
Also, ensure that the Enhanced Measurement feature is enabaled.
Click Create Stream.
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).
Integrate Google Analytics 4 with your Magento 2 store via GTM to track your customers entire journey on your website.
Get ExtensionStep 2: Create a GTM Container
Go to tagmanager.google.com and click the Create Account button.
Enter your Account name, Country, and Container name, and create your account.
Select Web in the target platform and click Create.
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.
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.
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.
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)
Step 6: Generate & Download GTM JSON
In the Google Tag Manager, close the popup and you’ll see the Public ID as shown below.
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.
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.
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.
Now, the list of triggers from the uploaded JSON will be shown.
Click Submit.
To submit the GTM container, enter a name and description for the version.
Click the Publish button.
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.
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 Event | Details |
---|---|
add_payment_info | User adds payment details |
add_to_cart | User adds a product to the cart |
add_to_compare | User adds a product to compare |
add_to_wishlist | User adds a product to the wish list |
begin_checkout | User begins checkout |
purchase | User completes the payment and places an order |
remove_from_cart | User removes a product from the cart |
select_item | User selects an item from the list |
select_promotion | User applies a promotion to the cart |
shipping_info | User adds shipping details |
view_cart | User views the cart |
view_item | User views the item |
view_item_list | User views the item list |
view_promotion | User views the promotion |
Frequently Asked Questions
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.