Skip to content

Instantly share code, notes, and snippets.

@benjaminmangold
Last active September 8, 2022 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminmangold/1944b3656ba38c5a26b930cf8f28e7b2 to your computer and use it in GitHub Desktop.
Save benjaminmangold/1944b3656ba38c5a26b930cf8f28e7b2 to your computer and use it in GitHub Desktop.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-EXAMPLE"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-EXAMPLE');
{% if first_time_accessed %}
gtag("event", "purchase", {
transaction_id: "{{ order.order_number }}",
value: {{ total_price | times: 0.01 }},
tax: {{ tax_price | times: 0.01 }},
shipping: {{ shipping_price | times: 0.01 }},
currency: "{{ order.currency }}",
items: [
{% for line_item in line_items %}
{
item_id: "{{ line_item.product_id }}",
item_name: "{{ line_item.title | remove: "'" | remove: '"' }}",
currency: "{{ order.currency }}",
price: {{ line_item.original_price | times: 0.01 }},
quantity: {{ line_item.quantity }}
},
{% endfor %}
]
});
{% endif %}
</script>
@craigcooperxyz
Copy link

For the transaction_id would it be better to use the order.id? It is possible for a customer to place an order and for the order object to be missing order_number and name at this point. They are then added later by Shopify (when the order processing is complete). But since we only have chance to track when the user views the page, that order will not come through to GA4.

@benjaminmangold
Copy link
Author

It’s based on Shopify’s own recommendation for conversion tracking on the order status page. See https://help.shopify.com/en/manual/orders/status-tracking/customize-order-status/add-conversion-tracking

So if their documentation isn’t right, you might want to customize it, but it works for my Shopify store.

@craigcooperxyz
Copy link

Yeah I don't think that help page is right any more. Check your store orders for anything with a confirmation number in the order activity and then check to see if that order was tracked by GA4.

@benjaminmangold
Copy link
Author

Mine all have an order number, but feel free to customize as you need.

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