Skip to content

Instantly share code, notes, and snippets.

@jmlagace
Created October 30, 2020 19:50
Show Gist options
  • Save jmlagace/c1efd114f6e359063ea6e5a9914610cb to your computer and use it in GitHub Desktop.
Save jmlagace/c1efd114f6e359063ea6e5a9914610cb to your computer and use it in GitHub Desktop.
<script>
/* track the sale in GTM */
$(document).on(`page:load page:change`, function() {
try {
if (Shopify.checkout && Shopify.Checkout.page == 'thank_you') {
dataLayer.push({
'event': 'eec.purchase',
'ecommerce': {
'currencyCode': Shopify.checkout.currency,
'purchase': {
'actionField': {
'id': Shopify.checkout.order_id,
'revenue': Shopify.checkout.total_price,
'tax': Shopify.checkout.total_tax,
'shipping': Shopify.checkout.shipping_rate.price,
'coupon': (Shopify.checkout.discount ? Shopify.checkout.discount.code : ''),
'discount': (Shopify.checkout.discount ? Shopify.checkout.discount.amount : '')
},
'products': Shopify.checkout.line_items.map(function(item) {
return {
'name': item.title,
'id': item.variant_id,
'price': item.price,
'brand': item.vendor,
'category': '',
'variant': item.variant,
'quantity': item.quantity,
'coupon': ''
};
})
}
}
});
}
} catch(err) {
console.log(err);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment