How to add Google tag manager Enhanced E-commerce tracking in magento2? Could you please help me?
3 Answers
For Advanced Ecommerece tracking you can use weltpixel, a free extension.
NOTE : just so you know this extension will update some of templates ( i.e product / category listing page )
-
Does This extension support Enhanced E commerce Tracking? Commented Apr 30, 2019 at 6:21
-
@Sanjay Gohil, it does but i guess it needs some keyworkd mapping / configurations etc. Commented Apr 30, 2019 at 6:29
-
As specified by @Amex above, you can add basic script of GTM code on your Magento 2 store but GTM is all about data and data layers without them there is very little can be done using the basic script of GTM.
For example - for Google Enhanced Ecommerce, you need to create data layers for all the below actions / pages
Product Impressions (for category, up-sell, cross-sell and search results) - Wherever you are showing list of products. https://developers.google.com/tag-manager/enhanced-ecommerce#product-impressions
Product Clicks (for category, up-sell, cross-sell and search results) - Wherever you are showing list of products. https://developers.google.com/tag-manager/enhanced-ecommerce#product-clicks
Product Detail Impressions (for product detail page) https://developers.google.com/tag-manager/enhanced-ecommerce#details
Add / Remove from Cart (for add to cart or remove from cart action) https://developers.google.com/tag-manager/enhanced-ecommerce#cart
Promotion Impressions (for list of banners / ads) https://developers.google.com/tag-manager/enhanced-ecommerce#promo-impressions
Promotion Clicks (for click of the banner / ad image) https://developers.google.com/tag-manager/enhanced-ecommerce#promo-clicks
Checkout (for checkout / checkout steps) https://developers.google.com/tag-manager/enhanced-ecommerce#checkout
Purchases (for order confirmation page) https://developers.google.com/tag-manager/enhanced-ecommerce#purchases
Refunds (from admin to send refund to GA in case of refunds) https://developers.google.com/tag-manager/enhanced-ecommerce#refunds
If you are a developer you can add the above data layers in your code using the following layout handlers -:
default.xml
catalog_category_view.xml
catalog_product_view.xml
catalogsearch_advanced_result.xml
catalogsearch_result_index.xml
checkout_cart_index.xml
checkout_index_index.xml
checkout_onepage_success.xml
If not then there are quite few extensions available for small amount to buy. Personally I would recommend the following two extension for M1 and M2
What You need is to add the GTM code to your active theme. The GTM code have two parts: script and noscript.
Make sure a nonscript part comes just after openning of <body>
tag otherwise the dataLayer
will not work
make two files:
I. gtm_head.phtml ---> YourTheme/Magento_Theme/templates/html/gtm_head.phtml
add script:
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');</script>
<!-- End Google Tag Manager -->
II. gtm_body.phtml ---> YourTheme/Magento_Theme/templates/html/gtm_body.phtml
add nonscript:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
after that open up
default .xml file: --->YourTheme/Magento_Theme/layout/default.xml and add this:
referenceContainer name="head.additional">
<block class="Magento\Framework\View\Element\Template" name="gtm.head" before="-" template="Magento_Theme::html/gtm_head.phtml" />
</referenceContainer>
<referenceContainer name="after.body.start">
<block class="Magento\Framework\View\Element\Template" name="gtm.body" before="-" template="Magento_Theme::html/gtm_body.phtml" />
</referenceContainer>
After GTM is added you can set up Google Analytics and other scripts and test your implementation with google chrome plugin (google tag assistant). If you have google analytics set already "old fashion" way through magento back-end you have to remove it because you will end up double tracking your website and you will see 2 GA accounts on google tag tester. Good Luck :)