Skip to content

Instantly share code, notes, and snippets.

@vvastdev
Created April 19, 2022 14:30
Show Gist options
  • Save vvastdev/24ef59af6b04ff414b2332f4a92a9931 to your computer and use it in GitHub Desktop.
Save vvastdev/24ef59af6b04ff414b2332f4a92a9931 to your computer and use it in GitHub Desktop.
gtag based implementation of GA4 dataLayer population
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-5GE0TR6RDZ"></script>
<script>
let args = {
'event': "orderconfirmation",
'discount': '{{ order.total_discounts }}',
'shippingPrice': '{{ order.shipping_price | money_without_currency | replace: ",", "." }}',
'transactionTax': '{{ order.tax_price | money_without_currency | replace: ",", "." }}',
'transactionSubtotal': '{{ order.subtotal_price | money_without_currency | replace: ",", "." }}',
'transactionTotal': '{{ order.total_price | money_without_currency | replace: ",", "." }}',
'transactionId': '{{ order.name }}',
'currency': '{{ shop.currency }}',
'totalProducts': '{{ order.line_items.size }}',
'payment_type': '{{ order.transactions[0].gateway }}',
'products': '[{% for item in order.line_items %}{{ item.product.title }}{% unless forloop.last %},{% endunless %}{% endfor %}]'
}
window.dataLayer = window.dataLayer || [];
function gtag() { window.dataLayer.push(args); }
gtag('js', new Date());
gtag('config', 'GA_MEASUREMENT_ID');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment