Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johnchatel/4e8cb17b99c3e7bbf209e4da60b71aa7 to your computer and use it in GitHub Desktop.
Save johnchatel/4e8cb17b99c3e7bbf209e4da60b71aa7 to your computer and use it in GitHub Desktop.
Shopify checkout tracking code
<!-- 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-XXXXXXX');</script>
<!-- End Google Tag Manager -->
{% if first_time_accessed %}
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ ecommerce: null });
var shipping_price = '{{shipping_price | money_without_currency }}';
shipping_price = shipping_price.replace(",", ".");
var total_price = '{{total_price | money_without_currency }}';
total_price = total_price.replace(",", ".");
var tax_price = '{{tax_price | money_without_currency }}';
tax_price = tax_price.replace(",", ".");
// GA4 Ecommerce & UA EE
window.dataLayer.push({
'page_type': 'purchase',
'event': 'ds_purchase',
'currency': "{{ shop.currency }}",
'totalValue': total_price,
'shipping': shipping_price,
'tax': tax_price,
'payment_type': '{{order.transactions[0].gateway}}',
'transaction_id': "{{order.name}}",
'ecommerce': {
'purchase': {
'actionField': {
'id': "{{order.name}}",
'revenue': total_price,
'tax':tax_price,
'shipping': shipping_price,
}
}
}
});
</script>
{% endif %}
@johnchatel
Copy link
Author

  1. Remember to change the GTM id first.
  2. In the Shopify admin, go to Settings > Payment , then copy in the Additional script field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment