Setting domain-wide cookies via JavaScript?

Topic summary

Main issue: A developer cannot set a domain-wide cookie in a Shopify store via JavaScript; adding the path attribute (needed for domain-wide scope, e.g., path=/) causes the cookie not to be set at all.

Proposed cause/solution: Code likely runs inside a sandboxed pixel/section where Shopify restricts cookie and path behavior. Recommended approaches:

  • Move cookie logic to the top-level layout file (theme.liquid) so it executes in the top frame.
  • Alternatively, use Shopify’s Web Pixels API, specifically browser.cookie.set, which is designed to set cookies from the top frame.

Update: Moving the code to theme.liquid did not resolve the issue. The developer plans to try the Web Pixels API next.

Open question: Where to initialize and use the Web Pixels API—inside theme.liquid or in another location? Guidance on correct placement/initialization for Web Pixels remains outstanding.

Status: Unresolved. Next step is to implement cookie setting via Web Pixels API; further instructions on integration location are requested.

Summarized with AI on December 11. AI used: gpt-5.

Hello everyone,

I’m currently trying to set a domain-wide cookie in a Shopify project by using the usual JavaScript commands. Unfortunately, it seems Shopify doesn’t like the when I add the “path” to the cookie command, which is required to make it domain-wide. As soon as I do that, the cookie isn’t set at all anymore.

What am I doing wrong and what’s the best solution to fix this?

Thanks in advance!

1 Like

Move the cookie code out of a sandboxed pixel/section into the top-level layout (theme.liquid) or use the Web Pixels browser.cookie.set API so it runs in the top frame (sandboxed pixels restrict cookie/path behavior).

1 Like

I tried setting it at theme.liquid level and it also didn’t work. So I guess I’ll have to try the Web Pixel API. Thanks for pointing me in the right direction!

One more question, as I’m brand new to working with Shopify APIs :sweat_smile: Do I insert the code for initializing and working with the API in the theme.liquid file, or somewhere else? Thanks in advance!