I want to track google analytics when user add item to minicart. I noticed that it make an ajax call to customer/section/load. So I want to add my google analytics code when these ajax success.

But I am not able to find proper file.

1 Answer 1


Magento 2 Enterprise Edition include this feature out of the box.

You can use Google Tag Manager in order to track product add to cart event and product remove to cart event and many other events.


I can give you the logic used in Magento 2 EE :

  1. Add Google Tag Manager Js code

  2. Create an Observer on these events : sales_quote_product_add_after sales_quote_remove_item

  3. Create / Update a registry entry in order to track products add and remove such as GoogleTagManager_products_addtocart and GoogleTagManager_products_to_remove.
  4. Create an Observer on this event controller_action_postdispatch in order to set cookies with the registry values. Cookie name example : remove_from_cart and add_to_cart
  5. Create a JavaScript logic in order to retrieve cookie values
  6. Push Google Tag Manager dataLayer for addToCart and removeFromCart such as

            'event': 'addToCart',
            'ecommerce': {
                'currencyCode' : CurrencyCode,
                'add': {
                    'products': [{
                        'id': id,
                        'name': name,
                        'price': price,
                        'quantity': quantity
  7. Remove cookies

I cannot share complete code because it is a Magento 2 EE feature.

You can track another events such as :

  • addToCart
  • removeFromCart
  • productClick
  • promotionClick
  • productImpression
  • promotionView
  • productDetail
  • You can also use a non free extension because Magento GTM integration is very basic. Jan 28, 2019 at 8:18
  • @frank I have created a tag for the "addToCart" event in GTM (for Adobe commerce 2.4.x). The tag was created back in Nov/Dec 2022 and it was working fine back then. but now I am noticing that this tag is not tracking the add-to-cart event. I can not see any data layer call in the Tag Manager assistant preview for this event. do you have any idea what may be wrong with this event? remove cart, purchase and other events are working fine though. Apr 7, 2023 at 4:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.