How to add Google Analytics 4 code to Magento 2?
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.
Table of contents
- The benefits of Google Analytics 4 compared to GA
- Types of events in Google Analytics 4
- How to add Google Analytics 4 code to Magento 2 with GTM?
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.
- 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.
- 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.
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.
- 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%).
- 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
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.
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.
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.
- Click Next to move on to Property creation.
- Enter the property name and set the required information fields, including reporting time zone and currency.
- Click Next to move on to Business details. Then, choose your business category and size.
- Click Next to move on to Business objectives and choose your target. Remember, you can choose multiple answers in this section.
- Click Create > choose I accept to agree with the terms of service.
- 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.
- Enter your Website URL and its 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.
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.
Remember: The container name serves as a descriptive title that helps link the container to your website.
Step 4. Locate Public, 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.
- Public ID
Navigate to the Admin section and find the public ID next to the container name.
- 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/.
- Container ID
The container ID can be found easily in the container page address, right after /containers/.
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.
- 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.
- 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.
- 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.
- Click on the Preview button in the Workspace tab.
- Enter your 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.
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.
& Maintenance Services
Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.Get Started