Google Tag Manager and the Shopify sandbox

Topic summary

Core Issue:
Google Tag Manager (GTM) loaded via Shopify’s sandboxed iframe creates multiple tracking problems. The iframe and main window each have separate dataLayers, preventing tools from accessing consent cookies and causing incorrect URL reporting.

Key Problems Identified:

  • GA4 events appear in the top window’s dataLayer but not in the iframe
  • Third-party tracking tools (Snap, Facebook, TikTok, Google Ads) either fail or report sandbox URLs instead of actual page URLs
  • GTM preview mode doesn’t work in the sandbox environment, forcing blind debugging on live sites
  • User engagement tracking fails because GA4 operates within the iframe
  • Scroll tracking fires instantly since the iframe doesn’t scroll
  • Server-side tracking breaks without custom transport URLs

Workarounds Implemented:

  • Manually override page_location and page_title parameters on all GA4 events
  • Route GA4 tracking through a subdomain to server-side GTM, which returns client-side Google Ads pixels
  • Use console.logs for debugging published changes
  • Some users recommend third-party tools like Elevar, Analyzify, or Converge

Community Resources:
One user shared a GitHub repository with Shopify pixel code and GTM container JSON for implementation.

Unresolved:
Engagement time metrics remain at zero. Users are testing whether adding event_timestamp to all events might resolve this, with results pending.

Summarized with AI on October 27. AI used: claude-sonnet-4-5-20250929.

One of my clients migrated to the new Shopify platform and has Google Tag Manager implemented via a custom script. So now this GTM container is loaded in a Sandboxed iframe. After some debugging I found out this iframe and the surrounding window (the actual shop) both have their own datalayer. That also explains why I see the GA4 events like view_item in the top.window.dataLayer but not in the one in the iframe.

We have about 8 different tools that listen to 4 specific events (for 95%), and they check the consent cookie beforehand to see what is allowed. Those tools don’t work anymore, they are not allowed to check the consent cookies, and only the tools that are loaded on page load are firing, but the URL they send along is the one from the iframed sandbox, not the real url of the surrounding window.

We can’t change the basic GA4 tracker to use a custom transport URL, so in stead of sending GA4 tracking to our own domain first it’s directly being send to google-analytics.com. Therefore all our serverside tracking isn’t working either.

The preview function of GTM is also not working, due to the iframe/sandbox. In another thread I read it was suggested to just publish changes and check if it works in the live site
 What about QA and stuff, we first need to check things in preview


Isn’t there any other way to just load GTM in the main source of the site, like unsandboxed?

3 Likes

For future reference when people search for this problem. We managed to get a lot of tracking working, but it’s not 100% the way it should work.

GA4 is now firing from within the Sandbox GTM container. We have to manually override the page_location and page_title parameters on all GA4 events. Otherwise you would get Sandbox URLs which look weird. Most of the other trackers do use the Sandbox URL (So the URL reports in Snap, Facebook, Tiktok, etc are not usable).

The Google Ads remarketing and conversion pixels are not firing while Snap, Facebook, Tiktok, etc are all working. We created a workaround by sending the GA4 tracking via our own subdomain to a serverside GTM container. That container is returning a request to the browser to send Google Ads pixels clientside, and those are working fine.

Because GA4 is working in an iframe the user_engagement tracking is not working at all, so the time people spent on the site isn’t reported correctly. We also had to turn of scroll tracking because there’s no scroll in the iframe, so the 95% scrolled event would fire instantly.

2 Likes

Have you had any further success figuring out the implementation?

No not really. The current setup ‘works’, but it’s sort of a blackbox. We can’t use the preview mode in GTM so everything we do is blind. With a lot of console.logs while publishing changes to the live container we manage to debug things.

For this specific client I’m the only one capable of managing this because I was a developer once and have experience with over 500+ GTM containers in the past. Based on that I have it working, but is way too technical.

I am experiencing some similar issues:

  1. gtm / gtag.js doesn’t work correctly, if I test it under Tag Aisstant Preview, the console reports an error about “iframe sandbox orign is null”.
  2. Also, Google Tag doesn’t seem to work even if Tag Assistant privew is not enabled.
    2.1 If I install gtag.js directly in the iframe sandbox and run dataLayer.push(“event”, “test_event”), my Google Analytics does not pick up any test events.
    2.2 If the GA4 event is triggered by the GTM tag, then Google Analytics is able to receive the data.

In short, it works in some cases and sometimes it doesn’t.
It appears that Shopify’s Customer event pixel is not really worth using.
Without using this mechanism, I’m not sure what way Shopify provides for us to reliably capture key events like add_to_cart / checkout_started / add_payment_info.

We have the same problem. It makes it impossible for ADS advanced conversions to work properly.

If anyone finds a solution: feel free to share. :grinning_face:

I published my implementation details in a GitHub repository (including Shopify pixel code and Google Tag Manager container JSON for importing into your container):

lstellway/snippets/shopify/google-tag-manager

I hope this can be helpful for others, as this was a pretty confusing implementation (Shopify pixels are new since the last time I touched Shopify).

I would also love feedback on the implementation if you have any - feel free to open an issue with your feedback.

3 Likes

Hi @AndreScholten

Do you know if overriding the page_location and page_title is necessary on all GA4 events? I only found documentation about these parameters for the Google page_view event.

I do see the page_location on all events, so yes, you do need to override it if you don’t want the sandbox url being used.

@AndreScholten thanks for asking this question. I have the same issue do you get any resolution?

Hey @AndreScholten thanks for your insightful post! Few questions I’m sure your lights will help me : I’ve added GTM server side script in my checkout page, all my events are capture by GTM web and sent to my GTM server , it seems to be working fine but :

  • my Google as conversions server side don’t work
 Google Ads detects activity but doesn’t attribute the conversion

  • since i made the move all my purchase in ga4 fall under Direct / Unassgined


I’m getting crazy with this :slightly_smiling_face:

Hey, I found some tools like Elevar, Analyzify and Converge that I tend to implement nowadays, they will give you a nice setup to work with without doing all the coding yourself.

@lstellway this code can be work in app pixel ?

@Anonymous

As specified in the README, it is intended to work with Shopify’s custom pixels.

Best of luck!

1 Like

Hey @lstellway

first. Thanks for your hard work! I have see you add a lot of time! Do you have a additional version to send also the data to google ads ? For Conversation tracking ?

Hi @KamaChen

The linked version sets up the recommended GA4 events related to Shopify’s standard events.

Hi @AndreScholten Did you get any solution for user_engagement event tracking in GA4?

Are there any updates? Engagement time is 0 across GA4 reports.

Hey Elliot,

If you use server side GTM perhaps this would help with cookies on checkout https://www.simoahava.com/analytics/cookie-access-with-shopify-checkout-sgtm/

It worked for us.

Thanks,

Matei

Hi @AndreScholten & @Matei86 ,

have you tried sending an event_timestamp with all events ?

Do you think that will work ?

Or is there any other way you have figured out to get the engagement time metric working in GA ?

Please let me know.