eCommerce tracking with google tag manager (GTM) and push ecommerce Datalayer via GTM
Step 1: First of all enable the ecommerce setting under GA
Step 2: Create a GA variable in Google tag manager and make sure that enable enhanced eCommerce features are checked and use the DataLayer option.
Step 3: Now lets push eCommerce data layer in GTM with the help of Custom HTML tag and set it fire with DOM Ready trigger
Datalayer example code:
<script>
var purchase_id = document.location.search.split(‘?’)[1];
purchase_id = document.location.search.split(‘=’)[1];
window.dataLayer = window.dataLayer || [];
dataLayer.push({
‘event’: ‘purchase’,
‘ecommerce’: {
‘currencyCode’: ‘USD’,
‘purchase’: {
‘actionField’: {
‘id’: purchase_id,
‘revenue’: 1,
}
}
}
});
</script>
Note: The variable used in this datalayer needs to be changed as per your need
Trigger: DOM Ready (set it for All DOM Ready Events)
Step 4: Now setup Google Analytics: Universal Analytics tag setup
Choose Track Type: Event
Category: Purchase (whatever you can choose)
Action: Purchase Successfully (whatever you can choose)
Label: Amount (whatever you can choose)
Value: {{Setting A data layer variable for total amount}}
Setting up data layer variable :
Select Variable type: Datalayer
How to create datalayer variable for ecommerce purchase value, Please see above datalayer code and now choose the first value under Purchase event, say ecommerce >> purchase >>actionField >> revenue and use . notation to add them, please see below
ecommerce.purchase.actionField.revenue
Step 5: Now lets create the trigger that will fire on successful purchase event.
Choose trigger type: Custom Events
Trigger Fires on : All Custom Events
Now this way we can track the ecommerce tracking in Google analytics with the help of datalayer push in GTM. Thanks