cart id is sometimes not received from browser cookie

Topic summary

A developer is experiencing intermittent issues retrieving Shopify-generated cart IDs from browser cookies when tracking frontend events using the Shopify pixel.

Current Implementation:

  • Subscribes to product_added_to_cart events
  • Fetches cart token from browser cookies using browser.cookie.get("cart")
  • Processes the cart ID before sending with event data

The Problem:

  • Cart ID is sometimes not present in the cookie
  • Issue occurs inconsistently and cannot be reliably reproduced

Questions Raised:

  • Is this the optimal approach for retrieving cart IDs?
  • Why does the cookie occasionally fail to contain the cart ID?

The discussion remains open with no responses yet, seeking guidance on both the implementation approach and the root cause of the intermittent cookie availability issue.

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

we are using the shopify pixel to track the frontend events
we want to send the shopify generated cart id whenever customer adds something to the cart
we are using the below code, but sometimes the cart id is not present

register(async ({ analytics, browser, init, settings }) => {
analytics.subscribe("all_events", async (event: any) => {
if (event.name === "product_added_to_cart") {
if (!cartId) {
cartId = await thirdPartyCheckoutHelper.fetchCartToken();
cartId = cartId.split("?")[0];
// send this cartId along with event.data
});
});

async fetchCartToken() {
    return await this.browser.cookie.get("cart");
  }

is this the best way to do it ? why is it sometimes not present ? we are not able to reproduce the issue