Error Using Await on Browser Cookie in Custom Pixel

Topic summary

Error in a Shopify Custom Pixel when using await to read a browser cookie (browser.cookie.get('_dyid_server')) inside the checkout_started event handler. The Custom Event editor reports “Missing semicolon” on the line with await, blocking save; a screenshot was attached.

Root cause: await can only be used inside an async function. The handler was not marked async, so the syntax checker flagged it.

Proposed fix: declare the event callback as async, e.g., analytics.subscribe(‘checkout_started’, async (event) => { const dyid_server = await browser.cookie.get(‘_dyid_server’); }). This aligns with the Web Pixels API usage when awaiting asynchronous cookie reads.

Status: A clear solution was suggested; no confirmation from the original poster yet. The thread appears open pending verification that marking the callback async resolves the error.

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

Fairly new to working with Pixels, and trying to create one from scratch using a Custom Pixel within a Client’s Shopify environment. However, it does not appear that using the documentation code to get a Cookie (browser documentation here :: https://shopify.dev/docs/api/web-pixels-api/standard-api/browser) is working, as it throws an error in the Custom Event editor.

The code, simplified, is as follows:

analytics.subscribe('checkout_started', (event) => {
  const dyid_server = await browser.cookie.get('_dyid_server');
});

Doing this, however, gives the error “Missing semicolon” on the line with await.

I have attached a screenshot showing this error. The error prevents the Custom Pixel from being saved, and I am unsure what to do here. Thanks for any help you can give!

You need to wrap your function with async in order to use await. Try this instead.

analytics.subscribe('checkout_started', async (event) => {
  const dyid_server = await browser.cookie.get('_dyid_server');
});
1 Like