How to connect Google Analytics 4 to Magento 2

How to connect Google Analytics 4 to Magento 2

Follow these clear steps to connect your Google Analytics 4 to Magento 2 store through Google Tag Manager. 

NOTE: This post has been updated, as the previous one had instructions for Universal Analytics (GA3) and Magento 1. Enjoy the refreshed instructions below.

A basic way to connect your Google Analytics 4 (GA4) property to any webshop is by inserting the Google Analytics 4 tracking code (gtag.js) in the <head> tag of every page you want to track. You can find the code by clicking on the property Admin > Data Streams > then click on the stream and there you have it. 

There is also an easier way to connect it by using one of many Google Analytics Magento extensions on the market. In this case, you’ll need just to insert the Measurement ID number of your GA4 property in the Magento extension interface, without copy-pasting the above-mentioned code.

How it is done is depending on the Google Analytics Magento extension that you choose to use. Therefore it’s best to search for the step-by-step instructions of the extension provider.

  1. Create a Google Tag Manager (GTM) account and container
  2. Install the Google Tag Manager Magento 2 Extension
  3. Connect your GTM container with the Magento GTM extension
  4. Create a GA4 configuration tag in GTM
  5. Test your connection in GTM Preview Mode

But, the best way to connect your Google Analytics to Magento is to connect it through Google Tag Manager. Overall, Google Tag Manager gives you more management options when it comes to handling Google Analytics and other marketing tools like Google Ads or Facebook, and enables you to create Google Analytics Enhanced eCommerce events that can help you track the user journey.

Here you can read more about why I think that’s the best way to connect Google Analytics to Google Tag Manager. In this case, instead of the Google Analytics Magento extension, you’ll need the Google Tag Manager Magento extension to set up everything properly.

Without further ado, here are the steps of connecting Google Analytics 4 to your Magento webshop with Google Tag Manager.

1. Create a Google Tag Manager (GTM) account and container

Start by clicking the “Start for free” button at this link:https://marketingplatform.google.com/about/tag-manager/ and then I highly recommend watching this short video tutorial on how to create your first GTM account and container.

You’ll also get an overview of the interface and account structure. If you later feel the need to dive deeper into the GTM, I recommend checking out other videos and materials from the Analytics Mania Youtube channel.

2. Install the Google Tag Manager 2 Extension

I will not suggest any particular GTM extension here, because they all do the job. The best for you is to Google it and choose the extension that has features that fits your needs the best. In general, they all need to follow Google documentation regarding GA4 Enhanced eCommerce configuration. We put effort into making our own performance-aware GTM extension that is used exclusively by our clients free of charge and therefore I’ll show you the interface of this extension in the example. Connecting the GTM with the GTM extension is similar and simple, regardless of the extension provider so I’m sure you will connect it successfully.

3. Connect your GTM container with the Magento GTM extension 

Open your GTM container and click on the GTM ID number. 

Then copy only the GTM ID number from the code.

Open your Magento GTM extension and paste the GTM ID number in Stores > Configuration > Sales > Google API > Google Tag Manager section. Save the configuration and the GTM is connected to your Magento webshop. You can test it by viewing the page source of your webshop and finding the GTM ID number. Or install the Tag Assistant Legacy browser extension. It is a useful extension that can help you troubleshoot installations of GTM, and Google Analytics and give you some other insights.

4. Create a GA4 configuration tag in GTM

I assume you have already created the GA4 property needed for this step. If you didn’t, check out how to do it in this blog post. Now open the GTM container and on the Tags section click the button “New”.

Then click on the Tag configuration space to open tag types.

Choose the GA4 configuration tag types.

…and then enter your GA4 Measurement ID number. The Measurement ID is located in the same place as the GA4 tracking code.

Remember the screenshot at the beginning of this blog where I showed you the path on how to find the GA4 tracking code on the screenshot you’ll see the Measurement ID location.

Now click on the triggering section and choose the trigger “All pages”.

Name your tag “GA4 Configuration” and click save.

And that’s it. You connected your Google Analytics property to your Magento webshop with Google Tag Manager. 

5. Test your connection in GTM Preview Mode

Open your Google Analytics GA4 property and check if you’re receiving traffic. GA4 needs some time to start showing the traffic, but you can test your connection immediately by clicking on the “preview” button in your GTM container. Enter your webshop URL and click “Connect”. 

GTM will open your webshop in another tab. Now go to your GA4 property and in the Configure report open the DebugView interface. You should see a page_view event that happened when the GTM opened your webshop in another tab, which means that your Google Analytics receives hits and is connected to your webshop. Continue to browse through your webshop and observe what events are starting to show in the DebugView in Google Analytics. 

If you want to continue to set up Enhanced eCommerce events like e.g. this one:

feel free to reach out to us and we will do it for you.

Also, setting up events in Google Analytics 4 is one thing, and monitoring, researching, and analyzing the data is another important thing to do. We can do that for you also. Our GTM/GA and UX specialists will analyze the data, test the hypotheses, and give you reports with suggestions on what needs to be improved/optimized on the webshop to improve the conversions – keeping in mind the revenue increase as a final goal.

 

Related Inchoo Services

You made it all the way down here so you must have enjoyed this post! You may also like:

Adding Magento products to Google Merchant Center Toni Anicic
Toni Anicic, | 127

Adding Magento products to Google Merchant Center

Magento & GWT “Googlebot found an extremely high number of URLs on your site” Toni Anicic
Toni Anicic, | 2

Magento & GWT “Googlebot found an extremely high number of URLs on your site”

Track validation errors on Magento forms using Google Analytics Nikola Stojiljkovic
Nikola Stojiljkovic, | 2

Track validation errors on Magento forms using Google Analytics

34 comments

  1. This article is one of the better resources for Magento-specific GA4 assistance, but important aspects are not mentioned in this guide. If this can all be done through Google Tag Manager, why do they have to install their own extension to make the GA4 configuration work? That extension has to be adding important elements on the backend and is where the real magic is going on. Nobody speaks about this as it’s the final piece of the puzzle.

  2. Great guide, However, The process itself is not so straightforward, a certain level of technical literacy is required and there are some security issues (although GTM offers a lot of options to solve these problems!).

  3. Analytics works as good. Eventhough ECommerce is enabled (3months before) stills ecommerce related data are not populating in it. Is there any reason behind it ? have tried both placing the GA Snippets and Configuring in Admin panel

  4. Digital Marketing Company In Gurgaon
    Shadow Technology is that the growing IT company providing Webs and different IT services in Gurgaon. we tend to square measure functioning from past five years within the trade with 100 percent client orientating services.Visit: http://shadowtechnology.in/
    As a clever we tend tobs and software system service supplier we square measure providing web site planning Service in Gurgaon with SEO , on-line promoting , 3d Animation and software system Development. At Shadow Technology, we tend to live the worth of technology by however it edges individuals.Call for more information at 9717755870

  5. Hi, thanks for sharing valuable information. I was working on e-commerce store to sell the toys & games for kids. I was really seeking to track the traffic & user behaviour on my store. This write-up really helped me a lot to set-up the tracking.

  6. hi tony
    What is the best method of redirecting the visitor to the store directory since they land

  7. Thank you for providing help in magento analyttics setup. I am new to magento SEO and I have been feeling difficulties in managing some SEO parameters.

  8. Hey Toni,

    Followed the above steps as it is, the site is also showing up in the list but the thing is that I’m not getting any traffic data in it.

  9. Hi Toni Anicic,

    I have done all the above set up but a slight change in code – replaced ga.js with dc.js to support re-marketing.

    I am not getting e-commerce data in the report – does dc.js support e-commerce tracking?

    Thanks

  10. Hi
    I am building an environment in Magento where different sellers can open their stores and sell their merchandise. I want to provide every merchant with his/her own analytics report. Is there a way to track each store separately through GA & report in a Magento interface?

    Thanks

    Ilan

  11. Hello,

    Can you please tell me exactly WHAT this will populate in Google Analytics? Will it populate all field in ECommerce tab?

    Conversions
    Transactions
    Average Order Value
    Purchased Products

    Or is there any additional tracking? We’ve implemented on a yet-to-be-launched site, so I can’t see what’s working just yet.

    Also, please, have mercy on me if this is a silly question – not a tecchie.

  12. Hi, I upgraded my site to 1.5.1.0 from 1.4.1.1, the GA e-commerce worked great but after the upgrade it’s not tracking anymore.

    I checked the settings on your tutorial and everything is correctly set-up and should be working too.

    I have checked several posts for this problem but I still cant find the fix. Any idea what might cause the problem?

    thanks a lot,
    May Ann

  13. Am I right in thinking that if you follow these steps that you don’t have to add any additional e-commerce tracking code within the GA tracking code using parameters such as _addTrans(), _addItem() and _trackTrans(). Will this be done automatically?

  14. Hi,
    I did the above setup as instructed but the problem is my ecommerce transactions are recorded 5 times for a single transaction. When i checked the ecommerce tracking code on the thank you page i found that it is being executed 5 times. Please help me with this. Also if anyone can tell me where can i find the file where the code is written it will be helpful.
    Thanks.

  15. Just tested the integration with a brand new installation of Magento. It seems that there are some issues with GA reporting:
    – when I look in the checkout/onepage/success/ page, I did not see any of the GA code for inserting the transaction to Google analytics.
    – When I look in GA, SOME of the data is missing:
    Sales of 0 products generated $15.00 (correct revenue amount) but sales of 0 product?

    the data also shows:
    33.33% Conversion Rate
    1 Transactions (correct)
    $15.00 Average Order Value (correct)
    1 Purchased Products (correct).
    But when I click on the 1 Purchased Products, I get no records found

  16. @David Corman, if you did everything as explained in this article and still can’t see e-commerce tracking you probably missed something. this should work by default, no additional code is needed.

  17. I added the web property id and activated the setting to track e-commerce data but still do not see the tab in my Analytics account for e-commerce tracking. Do I have to install the tracking code into the thank-you page as well?

  18. @Tyler, @karisa: True, Google Analytics is not real time, but it’s not updated once per day, it’s updated more often. Usually it’s somewhat like 3 hours late. You can check the stats for the current day but you’ll need to choose today in the drop down box with dates (top right).

  19. “I get analytics to work…at night after I have left my job. When I check in the morning there are new stats posted. Throughout the day however, the stats do not change. Any idea of whats happening or causing this?”

    Google analytics is not real time, the data is updated once a day, typically overnight.

  20. I get analytics to work…at night after I have left my job. When I check in the morning there are new stats posted. Throughout the day however, the stats do not change. Any idea of whats happening or causing this?

  21. i am getting into the magneto game as well, i always wanted to know the following, most magento installations are installed in a folder. What is the best method of redirecting the visitor to the store directory since they land on the root domain.

    .htaccess file or anything else

  22. Seems a simple enough step, try to complicate the situation by adding in adwords conversion code, the amount of blog posts around that topic seems to show its a big issue for many developers.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.