0

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.

4

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.

Documentation:

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

        dataLayer.push({
            '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
|improve this answer|||||
  • You can also use a non free extension because Magento GTM integration is very basic. – Franck Garnier Jan 28 '19 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.