How to set up a Shopify data layer for Google Tag Manager, without an app
To set up a Shopify data layer without an app, you add a custom pixel under Settings → Customer events that loads Google Tag Manager and subscribes to Shopify's standard events: page_viewed, product_viewed, product_added_to_cart, checkout_started, payment_info_submitted and checkout_completed. Each time one fires, the pixel pushes it to the dataLayer with the products, value, currency and order ID, and your GTM tags fire on those pushes. It works on the storefront, the checkout and the Thank you page, with no app and no theme code.
The video above shows the setup on a Shopify store in under seven minutes, with a Google Ads purchase tag on top. This post is the written version, focused on the data layer itself: what the code does, the events it sends, what Shopify's sandbox changes for GTM Preview and your scripts, and how to set consent properly.
What changed. Shopify's checkout, Thank you and Order status pages no longer run theme.liquid, checkout.liquid or the checkout's Additional scripts box. Tracking there has to come from a pixel in Customer events, which Shopify loads on the storefront, the checkout, the Thank you page and the Order status page (Shopify Help Center, pixels overview). My older video on the Shopify data layer from 2024 is useful background, but follow this post for a store today.
What a Shopify data layer is, and why GTM needs one
Google Tag Manager doesn't know what a product, a cart or an order is. It only reacts to messages pushed into an array called dataLayer. A data layer is the code that turns what happens on your store into those messages: "this product was viewed, at this price", "this order was placed, for this total, with these items".
Without it, your GA4, Google Ads and Meta tags have nothing to read. They fire with an empty value, no order ID and no products, or they don't fire at all.
On Shopify, the data source is the Web Pixels API. Shopify publishes a standard event at each step of the shopping journey, and a custom pixel can listen to them with analytics.subscribe (Shopify.dev, standard events). The data layer in this post listens to six of them and pushes each one to GTM under the event names GA4 uses, which is the mapping Shopify itself suggests (Shopify Help Center, Google Tag Manager custom pixel):
| Shopify event | Pushed to the dataLayer as | When it fires |
|---|---|---|
page_viewed |
page_view |
Every page, checkout included |
product_viewed |
view_item |
A product page is opened |
product_added_to_cart |
add_to_cart |
A product is added to the cart |
checkout_started |
begin_checkout |
Checkout starts |
payment_info_submitted |
add_payment_info |
Payment details are submitted |
checkout_completed |
purchase |
The order is placed |
Shopify has more, such as collection_viewed, search_submitted, cart_viewed, product_removed_from_cart and the contact, address and shipping steps of checkout. You can add them the same way once the six above work.
What you need
- Admin access to your Shopify store, to add a custom pixel.
- A Google Tag Manager account with a Web container. You need its ID, which starts with
GTM-. - The data layer code. You can write it from the example in Step 2, use Shopify's own Google Tag Manager example (linked under Sources), or use a ready-made one. The video uses mine, linked in its description, together with a GTM container template.
- A test product you can buy, or a discount code to place a cheap real order.
Step 1: Create your Google Tag Manager container
In the video, I start by creating the purchase conversion action in Google Ads, because that video ends with a Google Ads tag. For the data layer alone, you only need the GTM container.
Go to tagmanager.google.com and click Create account. Use your business name as the account name and pick your country. For the container, use your store's name and choose Web. Accept the terms, and GTM shows your container ID. Keep it handy.
You won't paste GTM's install code into your theme. The data layer loads GTM for you, inside the pixel.
Step 2: Build the data layer code
A Shopify data layer has two parts: the GTM loader, and one analytics.subscribe block per event.
Shopify's sandbox only runs JavaScript, so GTM's usual install code can't go in as it is. You keep the script inside it and drop the <script> tags around it (Shopify Help Center, custom pixel code).
Here's a short version you can read line by line. Replace GTM-XXXXXXX with your own container ID:
// 1. Load Google Tag Manager inside the custom pixel
window.dataLayer = window.dataLayer || [];
(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);
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-XXXXXXX');
// 2. Helpers: one product, and a clean push
function toItem(variant, quantity) {
return {
item_id: variant.product.id,
item_name: variant.product.title,
item_variant: variant.title,
variant_id: variant.id,
sku: variant.sku,
price: variant.price.amount,
quantity: quantity || 1
};
}
function fromCheckout(checkout) {
return {
currency: checkout.currencyCode,
value: checkout.totalPrice.amount,
items: checkout.lineItems.map(function (line) {
return toItem(line.variant, line.quantity);
})
};
}
function push(eventName, ecommerce) {
window.dataLayer.push({ ecommerce: null });
window.dataLayer.push({ event: eventName, ecommerce: ecommerce });
}
// 3. Shopify's standard events, pushed under GA4 names
analytics.subscribe('page_viewed', function (event) {
window.dataLayer.push({
event: 'page_view',
page_location: event.context.document.location.href,
page_title: event.context.document.title,
page_referrer: event.context.document.referrer
});
});
analytics.subscribe('product_viewed', function (event) {
var variant = event.data.productVariant;
push('view_item', {
currency: variant.price.currencyCode,
value: variant.price.amount,
items: [toItem(variant, 1)]
});
});
analytics.subscribe('product_added_to_cart', function (event) {
var line = event.data.cartLine;
if (!line) {
return;
}
push('add_to_cart', {
currency: line.cost.totalAmount.currencyCode,
value: line.cost.totalAmount.amount,
items: [toItem(line.merchandise, line.quantity)]
});
});
analytics.subscribe('checkout_started', function (event) {
push('begin_checkout', fromCheckout(event.data.checkout));
});
analytics.subscribe('payment_info_submitted', function (event) {
push('add_payment_info', fromCheckout(event.data.checkout));
});
analytics.subscribe('checkout_completed', function (event) {
var checkout = event.data.checkout;
var ecommerce = fromCheckout(checkout);
ecommerce.transaction_id = checkout.order ? checkout.order.id : null;
ecommerce.tax = checkout.totalTax ? checkout.totalTax.amount : 0;
ecommerce.shipping = checkout.shippingLine ? checkout.shippingLine.price.amount : 0;
push('purchase', ecommerce);
});
A few things worth knowing about it:
- The field names come from Shopify.
productVariant,cartLineandcheckoutand the fields inside them are documented for each event on Shopify.dev. For example,checkout_completedhas the order ID, the total, the currency and every line item (Shopify.dev, checkout_completed). checkout_completedfires once per order, usually on the Thank you page. If you use a post-purchase upsell, it fires on the first upsell page instead, and not again on the Thank you page.ecommerce: nullbefore each push clears the previous event's products, so a purchase never carries items left over from an add to cart.- The item ID here is Shopify's product ID. If you'll use the purchase for Google Ads cart data, the IDs must match your Merchant Center feed, which often uses a longer format. This post on cart data shows how to convert them in GTM.
A data layer you buy or download will be longer: more events, customer details for enhanced conversions and advanced matching, a console.log of each push for testing. The idea is the same.
Step 3: Add the data layer as a custom pixel
- In your Shopify admin, go to Settings → Customer events.
- Click Add custom pixel, give it a name such as
GTM data layer, and click Add pixel. - Set the Customer privacy options. Step 4 explains them.
- Clear the placeholder code and paste your data layer into the Code box.
- Click Save, then Connect.
Don't skip Connect. A saved pixel that isn't connected doesn't track anything (Shopify Help Center, manage custom pixels).
Before you connect it, remove any old GTM or data layer code from theme.liquid, and any tracking left in Additional scripts. Shopify says to do this so events aren't counted twice. If you're moving from old code, Shopify's advice is a trade-off: connect the new pixel before removing the old code and you won't miss events; connect it after and you won't double count (Shopify Help Center, pixel migration).
Step 4: Set your consent settings
This is the step people rush. There are two layers: Shopify's settings on the pixel, and the consent signals Google's tags read.
Shopify's Customer privacy settings. Every custom pixel has two:
- Permission. Required means the pixel needs the visitor's permission before it collects data for the purposes you tick: marketing, analytics or preferences. You need a cookie banner to ask for it. Not required means the pixel collects data without asking. New pixels start as Required.
- Data sale. This decides how the pixel behaves for visitors who opt out of the sale of their data, where the law gives them that right.
In the video I choose Not required. That suits a store without visitors from places with strict privacy laws. If you sell to the EU, the UK or other places with consent laws, choose Required and tick the purposes your tags use. This isn't legal advice; check what your business needs.
Google Consent Mode. Shopify's own GTM example sets all four Google consent signals to granted, and warns that this might not match your region's rules (Shopify Help Center, Google Tag Manager custom pixel). A safer pattern is to read the visitor's real choice from Shopify. A custom pixel gets it in init.customerPrivacy, and can listen for changes when the visitor answers the banner (Shopify.dev, pixel privacy).
Put these lines at the very top of the pixel, before the GTM loader. Google says the consent default must be set before any tag loads (Google, set up consent mode):
window.dataLayer = window.dataLayer || [];
function gtag() { window.dataLayer.push(arguments); }
function consentFrom(privacy) {
var ads = privacy.marketingAllowed ? 'granted' : 'denied';
return {
analytics_storage: privacy.analyticsProcessingAllowed ? 'granted' : 'denied',
ad_storage: ads,
ad_user_data: ads,
ad_personalization: ads
};
}
gtag('consent', 'default', consentFrom(init.customerPrivacy));
api.customerPrivacy.subscribe('visitorConsentCollected', function (event) {
gtag('consent', 'update', consentFrom(event.customerPrivacy));
});
Now GA4 and Google Ads follow the same answer your Shopify cookie banner collected. For Meta, TikTok and other tags, add a consent check in GTM so they wait for marketing consent. My guide to Consent Mode and GDPR explains how the four signals work.
Step 5: Connect GTM to the data layer
The data layer only pushes events. GTM still needs variables and triggers to use them.
- Triggers. Create a Custom Event trigger for each event name the data layer pushes:
page_view,view_item,add_to_cart,begin_checkout,add_payment_infoandpurchase. The name must match exactly, including case. - Variables. Create a Data Layer Variable for each value your tags need:
ecommerce.value,ecommerce.currency,ecommerce.transaction_id,ecommerce.items, andpage_locationfor page views.
In the video, I skip this by importing a ready-made container: Admin → Import container, choose the file, pick Existing workspace and Merge. The tags, triggers and variables appear at once, and you only fill in your own IDs. If you build it yourself, it takes longer but you learn what each part does.
Then add the tags you need on top. That's where the other guides take over (see below).
Step 6: Publish and test, the sandbox way
Click Submit, give the version a name such as Shopify data layer v1, and click Publish. Nothing runs on your store until you do.
Testing is where the sandbox surprises people. Custom pixels run in an isolated sandbox, separate from your store's pages (Shopify Help Center, pixels overview). GTM loads inside it, not on the page you see. Shopify says Google Tag Assistant's troubleshooting feature doesn't detect Google tags in a custom pixel, or tags GTM loads from one (Shopify Help Center, Google Tag Manager custom pixel). So GTM Preview often shows nothing, even when everything works.
Test it this way instead:
- Shopify Pixel Helper, Shopify's Chrome extension, shows whether each event reaches the sandbox without errors.
- The browser console (right-click, Inspect, Console) shows what the data layer pushed, if your code logs it. Most ready-made data layers do.
- Each platform's own test view: GA4 DebugView, Meta's Test events, and Google's EC Assist extension for Google Ads, which is what the video uses.
Walk through the store like a buyer: open a product, add it to the cart, start checkout, enter payment details and place a test order. You should see all six events.
If you're adding a Google Ads conversion, expect it to show Inactive at first. As the video explains, it changes once real conversions come in, usually within a few hours of ad traffic.
What the sandbox means for your scripts
The same sandbox changes what GTM can do on Shopify, not just how you test it.
- Page URLs. GTM's built-in page variables read the sandbox, and Shopify says those URLs can include sandbox details. Use the
page_locationandpage_titlethe data layer pushes, and in GA4 turn off the automatic page view so pages aren't counted with the wrong address. - Clicks, forms and scrolling. GTM's own click, form and scroll triggers listen to the page they run in, and inside the sandbox that isn't your store's page. Shopify notes, for example, that outbound link clicks can't be tracked automatically in a custom pixel. Shopify publishes some page interactions as DOM events you can subscribe to instead.
- Custom HTML tags that read or change your store's page won't work the way they do on a normal site.
- No stateful dataLayer. Shopify says GTM's stateful dataLayer isn't available in the sandbox, so each push should carry everything its tags need. That's why the code above sends the full
ecommerceobject every time.
What to do with it next
The data layer is the foundation. On its own it sends nothing to an ad platform. Pick what you need:
- GA4: Shopify GA4 ecommerce tracking with Google Tag Manager builds one GA4 tag per event on this data layer.
- Google Ads: Google Ads conversion tracking on Shopify with GTM adds the purchase conversion with value, currency, transaction ID and enhanced conversions.
- Meta: How to install Facebook Pixel on Shopify with Google Tag Manager builds every Meta event from the same data layer.
One data layer can feed all three. That's the point of having one.
Common mistakes
GTM still sits in theme.liquid as well. Two copies of GTM means two copies of every event on the storefront. Load it from the custom pixel only.
The pixel was saved but not connected. It tracks nothing until you click Connect.
Triggers that don't match the event names. Purchase and purchase are different events. So are purchase and checkout_completed. Use the exact name your data layer pushes.
Trusting GTM Preview. An empty Preview doesn't mean a broken data layer. Check Shopify Pixel Helper, the console and the platform's own test view.
Consent left at "granted" for everyone. Shopify's example does this, and so do many copied scripts. If you need consent, read it from Shopify as shown in Step 4.
Pasting the code with <script> tags. The sandbox only runs JavaScript. Remove the HTML tags around GTM's code.
Questions people ask
How do I enable the data layer in Shopify?
Shopify doesn't have a data layer switch. You add one as a custom pixel under Settings → Customer events: code that subscribes to Shopify's standard events and pushes them to dataLayer, with GTM loaded in the same pixel.
Can I set up a Shopify data layer without an app?
Yes. Custom pixels are built into the Shopify admin, so the data layer is just code you paste in. An app is an option if you'd rather not maintain code yourself.
Does the data layer work on Shopify's checkout and Thank you page?
Yes. Pixels in Customer events load on the storefront, the checkout, the Thank you page and the Order status page. That's the main reason to use a custom pixel instead of theme code.
Why doesn't my Shopify data layer show in GTM Preview?
Because GTM runs inside Shopify's custom pixel sandbox, and Google's Tag Assistant can't see into it. Test with Shopify Pixel Helper, the browser console and each platform's own test tools.
Can I still use Additional scripts for tracking?
No. Shopify has retired Additional scripts and checkout.liquid for the Thank you and Order status pages. Customer events is where tracking code goes now.
Where KiteTrack fits
A data layer in a custom pixel still runs in the shopper's browser, so ad blockers and browser privacy rules can stop some events from reaching your platforms. KiteTrack runs your own tracking server on your store's subdomain and sends your Shopify events to Meta, Google Ads, GA4 and TikTok from there. Customer details are hashed before they leave it, and your tracking is checked every 30 minutes, so a broken event becomes an alert. See how it works for Shopify.
Not sure what your Shopify data layer is sending right now? Start with the free Tracking Check. It looks at your site in about two minutes, no signup. If you'd rather talk it through, book a call with me.
Sources
- Shopify Help Center, Pixels overview: https://help.shopify.com/en/manual/promoting-marketing/pixels/overview
- Shopify Help Center, Google Tag Manager custom pixel: https://help.shopify.com/en/manual/promoting-marketing/pixels/custom-pixels/gtm-tutorial
- Shopify Help Center, Manage your custom pixels: https://help.shopify.com/en/manual/promoting-marketing/pixels/custom-pixels/manage
- Shopify Help Center, Pixel migration: https://help.shopify.com/en/manual/promoting-marketing/pixels/pixel-migration
- Shopify.dev, Web Pixels API standard events: https://shopify.dev/docs/api/web-pixels-api/standard-events
- Shopify.dev, Pixel privacy: https://shopify.dev/docs/api/web-pixels-api/pixel-privacy
- Google, Set up consent mode on websites: https://developers.google.com/tag-platform/security/guides/consent
Get tracking tips by email
Short, practical fixes for Meta, Google Ads and GA4, from Abir.
You’re in. First tip lands soon.