Developer docs / Integrations

Consent and the visitor id

How a store decides whether it may keep a visitor id, and how a headless front end or app shows the same banner and opt-out link as the store's own theme.

Why one decision

Why: the banner, the opt-out link, the privacy policy and every event that carries a visitor id must agree, or a store would keep an id where its owner promised not to. How: one decision per request, made by the store from the visitor's country (Cloudflare's request.cf.country), their stored choice and Global Privacy Control, against the store's markets (Admin, Settings, Markets). The store's own theme, the events API and your front end all read the same answer.

The decision

GET /api/consent answers it for the calling browser, with no key: send the visitor's cookies, and the Sec-GPC header if the browser sets it. In the store template the same decision is visitorConsent(req) in api/consent/consent.service.ts, built on consentDecision() in api/consent/consent.rules.ts.

GET /api/consent

{
  "consent": {
    "country": "DE",
    "market": { "id": "eu", "name": "EU, EEA, UK and Switzerland", "rule": "ask_first" },
    "visitor_id": false,
    "counting": "daily_hash",
    "notice": "banner",
    "choice": null,
    "gpc": false,
    "reason": "not_asked"
  }
}
FieldMeaning
visitor_idtrue: a first-party visitor id may be set and sent with events. false: count with the daily hash only, and remove any id you kept.
countingvisitor_id or daily_hash, the same answer as a word
noticebanner: show Accept and Reject. choices_link: a link that reopens the choice. opt_out_link: a "Do not sell or share my personal information" link. none: nothing to show, the market keeps no id.
marketThe visitor's market: the one listing their country, else the rest-of-world market. An unknown country is the rest of the world.
reasonstore_off, market_off, not_asked, accepted, rejected, gpc, opted_out, not_opted_out or allowed
  • Ask first: the id only after the visitor accepted. Global Privacy Control counts as a Reject until the visitor accepts themselves, so a GPC browser gets no banner.
  • Opt-out: the id until the visitor opts out. Global Privacy Control is an opt-out that a stored Allow does not undo.
  • The store's Visitor id switch, or the market's, off: no id and nothing to show, anywhere in that scope.

Keep the visitor's choice

POST /api/consent keeps the answer for a year in the __Host-cn_consent cookie (granted or denied). Send it from the visitor's browser on the store's own origin: posts from another site are refused (403), so nobody can accept for a visitor. A denial also removes the visitor id cookie. JSON gets the new decision back; a form post (the store theme's banner, no script) is answered 303 back to the page named in back.

POST /api/consent
Content-Type: application/json

{ "analytics": true }
<form method="post" action="/api/consent">
  <input type="hidden" name="back" value="/products/mug">
  <button name="choice" value="granted">Accept</button>
  <button name="choice" value="denied">Reject</button>
</form>

In a browser, navigator.globalPrivacyControl is true when the browser sends Sec-GPC: 1 on its requests. The store reads the header, so a front end that calls GET /api/consent from the browser needs no check of its own.

In the store theme

Every store theme draws the banner, the choices and the footer link from the decision, in the page's language, with no script: the layout renders them with the page (no layout shift) and each button is a form post. A template needs no code for it. The page also carries data-consent="visitor_id" or "daily_hash" on its outer element, for scripts that must know without a request.

Read and set the markets

The owner API holds the markets: GET and PUT /api/owner/consent, with the settings.read and settings.write grants. A PUT sends the whole set in order: each market's name, countries (ISO 3166-1 alpha-2, each in one market only), rule (ask_first or opt_out), visitor_id, and exactly one market with rest: true for every other country. A market without an id is added and gets one. A country in two markets, or no rest-of-world market, answers 400 naming the field.

PUT /api/owner/consent

{
  "visitor_id": true,
  "markets": [
    { "id": "eu", "name": "EU, EEA and UK", "countries": ["AT", "DE", "FR", "GB"], "rule": "ask_first", "visitor_id": true },
    { "name": "Switzerland", "countries": ["CH"], "rule": "opt_out", "visitor_id": true },
    { "id": "rest", "name": "Rest of world", "rule": "ask_first", "visitor_id": true, "rest": true }
  ]
}

Until the owner saves, GET answers the starting markets with saved: false. The events API (Observability hooks and API) will send each event with this decision and drop the visitor id wherever it is false.