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“.

However, keep in mind Shopify will remove the additional scripts section on August 28, 2025. I recommend using custom pixels or the Google Shopify App and turning on conversion tracking under Apps > Google & YouTube > Settings > Conversion tracking.

<!-- 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_id }}',
      "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 %}

Shopify Updates

Finally, as part of ongoing improvements Shopify is making to Checkout and the Thank You page, you need to check to your Additional Scripts to ensure that your conversion tracking and downstream systems are working correctly.

What you need to do

Replace “checkout.order_number” with “checkout.order_id” in Additional Scripts and similarly update any downstream systems that rely on this data.

In the code above, I’ve already replaced checkout.order_number with checkout.order_id but you still need to check any downstream systems if you have them.


Comments

2 responses to “How To Add GA4 Purchase Conversion Tracking To Shopify”

  1. Youness Idbakkasse Avatar
    Youness Idbakkasse

    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.

    1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get Involved & Explore More

an abstract painting with blue and yellow colors

Catch up on what I’ve been writing lately.

Show your gratitude.

Join Dare To Code Email List

Get emails from me on full-stack PHP development by subscribing to the Dare To Code mailing list.