Complete guide how to setup GA4 on Shopify
Step 10. How to implement GA4 purchase event on Shopify
If you want to see purchases and revenue generated by your Shopify shop in your GA4 reports you have to implement GA4 purchase event. Here you have instruction how to do it using Google Tag Manager and data layer variables in Shopify.
- Implement data layer variables in Shopify.
In this step we implement transaction details data layer variables into Shopify template. These variables we use later in GTM to sent them to Google Analytics every time when user make a purchase Shopify.
Let's go to your Shopify Admin to edit your checkout settings: Shopify admin>Settings>Checkout>Additional scripts, and paste there the following data layer code:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'purchase',
'totalValue': '{{ checkout.total_price | divided_by: 100.0 }}',
'shipping': '{{shipping_price | money_without_currency }}',
'tax': '{{tax_price | money_without_currency }}',
'payment_type': '{{order.transactions[0].gateway}}',
'currency': '{{ shop.currency }}',
'transaction_id': '{{order.order_number}}',
'items': [
{% for line_item in line_items %}
{
'item_id': '{{ line_item.product_id }}',
'item_name': '{{ line_item.product.title | remove: "'" | remove: '"'}}',
'item_category': '{{ line_item.product.type }}',
'price': '{{ line_item.price | times: 0.01 }}',
'quantity': '{{ line_item.quantity }}',
'item_variant': '{{ line_item.item_variant.title }}'
},
{% endfor %}
]
}); - Implement Google Tag Manager container code.
As you already have data layer variables in your checkout code you need to add after GTM Transaction Data layer section, Google Tag Manager code.
If you already have GTM code in your checkout setting please just make sure that is implemented after GTM Transaction Data Layer and go straight to the 3 point.
To obtain GTM container code you have to do first 3 steps from here: How to install Google Tag Manager in Shopify.
If you have already copied GTM container code, go to:
Shopify admin>Settings>Checkout>Additional scripts
and paste it there, after Transaction Data Layer code. - Define new variables in Google Tag Manager using data layer.
Go to "Variables" section in you Google Tag Manager.
then in "User-Defined Variables" section click "New" button.
then:
a) Create "DL – totalValue" data layer variable as follow (chose variable type as "Data Layer Variable")
b) Create "DL – shipping" data layer variable as follow (chose variable type as "Data Layer Variable")
c) Create "DL – tax" data layer variable as follow (chose variable type as "Data Layer Variable")
d) Create "DL – payment_type" data layer variable as follow (chose variable type as "Data Layer Variable")
e) Create "DL – currency" data layer variable as follow (chose variable type as "Data Layer Variable")
f) Create "DL – transaction_id" data layer variable as follow (chose variable type as "Data Layer Variable")
g) Create "DL – items" data layer variable as follow (chose variable type as "Data Layer Variable") - Create "CE – purchase and Dom ready" trigger.
We use it to fire new purchase tag. Go to trigger section and create new trigger, clicking "New" button.
then
a) Create new "CE – purchase" trigger as follow:
b) Create new "CE – purchase and Dom Ready" event as follow (chose trigger type as: Trigger Group)
We are using here trigger group to connect 2 existing triggers
– CE – purchase which is activated when purchase custom event is present in data layer)
– All Page View – Dom ready which is activated when all Dom elements of your website is loaded. We are using Dom ready type of trigger to be sure among others that all variables are already present in data layer. - Create GA4 – purchase tag in Google Tag Manger.
Go to "Tags" and click "New" button to add new tag and name like "GA4 – purchase"
then choose tag type and select “Google Analytics: GA4 Event”
then choose your configuration tag that you have already created here: How to implement GA4 Configuration tag in Google Tag Manager?
and set "Event Name" to "purchase"
then setup "Event Parameters" as follow:
then choose your trigger that you have already created a while before (CE – purchase and Dom ready),and click "Save" button on the top right corner.
After this implementation you will be able to see purchase details in your GA4:
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-xxx');
Lets go to the next step of our tutorial which is: How to implement GA4 add_to_cart event on Shopify
.
If you enjoyed the content please share it:
If you have any question about this topic feel free to comment or react below.
This guide is all about how to implement Google Analytics(GA4) on Shopify. To setup GA4 on Shopify website we use Google Tag Manger(GTM) and Shopify data layer.

If you want to measure traffic on your Shopify website, the first thing is to create Google Analytics account for your website


If you have Google Account but you don't have GA4 property yet, because for example you are still using Universal Analytics property (former version of GA4) you have to create new GA4 property.


Because we will use Google Tag Manger as a method to implement Google Analytics 4 you have to create GTM account.


If you want to get the best out of Google Analytics 4 apply the following recommended GA4 settings and best practices.


Environment - is a set of necessary elements of technical / software infrastructure, which is the basis for the operation of a given website/application. Google Tag Manager environments are simply multiple versions of the same container. When we have a website with a different version for development, testing, production and QA, instead of creating separate GTM containers for each version, we can set up separate environments in the same container that will work independently in each version.


Some time ago I listened to a podcast from the Technical Marketing Handbook series from Simo Ahava, whose guest was Adam Halbardier from the Google Tag Manager product team. During the conversation, the topic of transparency of data handled by the Server-Side container appeared.


If you plan email marketing campaigns you are interested in the same as with other campaigns, to measure their impact on your business. When you are starting with this topic it is good to know that without additional effort you will not be able to access this impact. You will see no results because Google Analytics will treat traffic from the email campaigns as direct traffic (when users are using your own app) or as referrals (when users are using web email clients).
