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 %}