If you are not using the Google App in Shopify with GA enabled already, the following code may be what you need.
- First, go to: Settings > Checkout > Order status page (toward the bottom).
- Then, copy and paste the following code into the “Order status page” section.
- Replace
G-XXXXXXXXXX
with your GA4 measurement ID. - Finally, Click “Save“.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
{% if first_time_accessed %}
<script>
gtag('event', 'purchase', {
'send_to':'G-XXXXXXXXXX',
"transaction_id": '{{ checkout.order_number }}',
"affiliation": "Shopify Shopping Cart",
"value": {{ checkout.total_price | divided_by: 100.0 }},
"currency": '{{ checkout.currency }}',
"tax": {{ checkout.tax_price | divided_by: 100.0 }},
"shipping": {{ checkout.shipping_price | divided_by: 100.0 }},
"items": [{% for line_item in line_items %}{
"item_id": '{{ line_item.product_id }}',
"item_name": '{{ line_item.title }}',
"item_variant": '{{ line_item.variant_id }}',
"quantity": {{ line_item.quantity }},
"price": {{ line_item.final_price | divided_by: 100.0 }},
"affiliation": '{{ line_item.vendor }}',
"discount": {{ line_item.line_level_total_discount | divided_by: 100.0 }}
}{% unless forloop.last %},{% endunless %}{% endfor %}]
});
</script>
{% endif %}
2 thoughts on “How To Add GA4 Purchase Conversion Tracking To Shopify”
Hey, thanks for the explination, however this deprecated approach, you might wanna migrate this to the new Customer Events API, and use gtag and subscribre function to track instead of Liquid.
Hey Youness,
I have an article here on that topic as well: How To Install GA4 On Shopify Using Customer Events, Pixels, and GTM. This article exists to provide an alternative to customer events and the official Google App for Shopify.
Thanks,
Kevin