Trying to specify cookie path to add cookie to root domain

I’m trying to add a cookie to root domain using the Shopify Web Pixel API (@shopify/web-pixels-extension) but if my utm link is /products or /anything except root, it adds the cookie to that path. This makes the cookie inaccessible for any other page and breaks my core functionality.

I tried this:

await api.browser.cookie.set('myapp_ref', affiliateId, {
        path: '/',
      });

I then visited a page with “/products/name?myapp_ref=Beckett” but the cookie path was still /products.

I also tried adding domain to the cookie initialization but that also didn’t work:

await api.browser.cookie.set('myapp_ref', affiliateId, {
        path: '/',
        domain: hostname
      });

This leads me to suspect that the pixel api uses a different syntax to specify cookie paths (if it allows that at all).

My hail mary approach was to try checking for a myapp_ref cookie with the /products path (from a different path) to just get the cookie (not ideal but whatever) but that also didn’t work.

Would really appreciate any input!!

Hello Beckett,

The browser.cookie documentation highlights how the set function can be used.

TL;DR: It doesn’t offer a third parameter but it’s possible to pass any cookie option as a string just like it would be done with document.cookie.

await api.browser.cookie.set(`myapp_ref=${affiliateId}; path=/; domain=${hostname};`);

Hope it helps clear things up around how this API works!

2 Likes

Thank you so much!! I was working on this issue for so long and this completely solved it. Also the link is broken, I’m pretty sure it’s meant to point to this page: browser.cookie documentation

1 Like

Oups, you’re right about the link! Fixed it in my original reply :smile:

1 Like