Skip to main content
Safari keeps a cookie written by JavaScript for at most 7 days, and clears localStorage after 7 days without a visit. A Safari visitor who comes back after a week gets a new anonymous ID. So they count as a new visitor and lose their first touch for attribution. A cookie that your site’s own server sets in a Set-Cookie header keeps its full lifetime. So set the web SDK’s mn_anonymous_id cookie on your server, and tell the SDK to only read it. This needs web SDK 0.17.0 and @manalens/node 0.5.0 or later. The helpers are in @manalens/node. @manalens/node/cookies has only the cookie helpers and no Node.js imports, so it also runs in edge runtimes such as Next.js middleware.
What the helpers do:
  • They keep a valid ID from the cookie (a UUID, as the web SDK makes) and make a new one otherwise.
  • They set the cookie again on every response, so the one-year expiry starts again at each visit: Path=/, Max-Age=31536000, SameSite=Lax, Secure, and Domain when you pass domain. It is not HttpOnly, because the web SDK reads it.
  • After the visitor opts out (mn_opt_out=true), they make and refresh nothing, and return no ID.
  • The cookie is only in the response. To use the ID in the same request, for example in a server event, use the returned anonymousId.
Pass domain (example.com) to share the ID with every subdomain, as the web SDK does by default with crossSubdomainCookies.

2. Turn on serverCookie in the web SDK

With serverCookie, the SDK reads the server’s cookie and never writes it from JavaScript. A write from JavaScript brings back Safari’s 7-day limit. When the cookie is missing (a page your middleware does not cover), the SDK makes and stores the ID as it does without the option. reset(), optOut() and setConsent(false) still delete the cookie.

Check it

  1. Open your site in Safari with an empty cookie store.
  2. In Web Inspector, open Storage, Cookies. mn_anonymous_id expires in one year, not in 7 days.
  3. Reload. The value stays the same and matches getAnonymousId() in the console.

Limits

  • Same server. The cookie must come from the server that serves your pages. Safari 16.4 and later also cap a server cookie at 7 days when the responding server’s IP address is not the site’s IP address. For example, a page is on one host and the cookie comes from a separate API host behind another provider.
  • No static hosts. A static host with no server code cannot set it. events.manalens.dev cannot set it either: it is another site.
  • No cached responses. Do not set it on a response that a shared cache (a CDN) stores. Every visitor of that cached page will get the same ID. Exclude cached routes from the middleware, or mark the responses Cache-Control: private.
  • Consent. In consent mode, set the cookie only after the visitor agrees. From @manalens/node 0.7.0, pass requireConsent: true and your publishableKey to ensureAnonymousId() or anonymousIdCookie(). The helper then sets the cookie only when the request has this project’s consent cookie (consentCookieName(publishableKey)) with the value 1. It does not set the cookie with Global Privacy Control. The web SDK writes the consent cookie only with crossSubdomainCookies on (the default). See consent mode in the Node.js SDK.
See what we collect for every name the web SDK stores.