Skip to content

Consent on headless storefronts: call the store's API host directly - #250

Merged
dminch merged 2 commits into
masterfrom
feature/attribution-headless
Nov 12, 2026
Merged

dminch merged 2 commits into
masterfrom
feature/attribution-headless

Conversation

@dminch

@dminch dminch commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Headless storefronts serve their pages themselves, so the storefront gateway never sees the ad landing and the consent call to the page's origin goes nowhere. With this change, swell-js records consent and ad landings on those storefronts by calling the store's API host directly.

  • consent.set() calls the page's origin first, as before. It switches to PUT <store>.swell.store/api/session/consent with direct: true only when the origin answers but not as Swell:

    • a 404 or 405;
    • a page that isn't Swell's answer;
    • Swell's refusal of a call the storefront's proxy forwarded, which has its own code, storefront_origin_required. Any other refusal doesn't switch.

    It never switches on a 5xx or a 429 (whatever the code), on a network failure, or from a page on *.swell.store, whose origin Swell serves.

  • The direct call sends the page's ad cookies (_fbp, _fbc, _ttp, …) in the body. It gets the visitor token back and sets it as the swell-visitor cookie on the page's domain, which swell-js already sends as X-Visitor.

  • Only Swell's API host. The direct call goes only to a host on .swell.store. With a url that points at the storefront's own server, the server would forward the call with its own IP and country, so nothing goes direct.

  • init() records an ad landing (a click id or UTM tags) without an answer, once per landing and store. It waits for the page's load, so the ad pixels have set their cookies first.

  • Per tab and API host, sessionStorage keeps that the calls go direct (swell-consent-direct:<origin>) and that the store records nothing (swell-consent-off:<origin>, after an answer with capture: false). The second one stops the landing calls, and consent.set() still goes. The Consent type has capture?: false.

  • A failing cookie adapter no longer fails a call that Swell already recorded.

  • api.js now sets err.status on connection errors too, so that 404 and 405 can be told from other failures.

The gateway side is in swell-admin: schema/swell-admin!3468. The gateway accepts a direct call only when the browser made it: Sec-Fetch-Site: cross-site, no CF-Worker, and no proxy named in X-Forwarded-For before Cloudflare's address. Deploy the gateway first.

Limits

  • Safari keeps a cookie set by a script for 7 days, or 24 hours after a link with a click id.
  • Browsers that send no Sec-Fetch-Site (Safari before 16.4) aren't recorded.
  • On a headless storefront, the first call of a tab gets a 404 from the storefront's own server.

Version

The dashboard and the docs say "swell-js 5.10.0 or later". Please confirm the release version, or tell me which one to use.

Testing

  • npm test: 457 tests pass. New tests cover:
    • the fallback, and the cases that don't switch (another refusal, 5xx, 429, network);
    • the per-tab switch for each API host;
    • capture: false stopping the landings;
    • landing on init;
    • a failing cookie adapter.
  • Live run on a dev environment with the UMD build in headless Chromium, against an HTTPS *.swell.store host through the gateway: landing, consent, visitor cookie and order snapshot all checked. A url of a proxy makes no direct call.

🤖 Generated with Claude Code

…rigin isn't Swell's

consent.set() calls the page's origin first. It switches to
`PUT <store>.swell.store/api/session/consent` with `direct: true` and the
page's ad cookies only when the origin answers but not as Swell: a 404 or
405, a page that isn't Swell's answer, or Swell's refusal of a forwarded
call (`storefront_origin_required`). Never on a 5xx, a 429 or a network
failure, and only to a host on `.swell.store`. It keeps the visitor token it
gets in the `swell-visitor` cookie of the page's domain.

init() records an ad landing (a click id or UTM tags) without an answer,
after the page's load, once per landing and store.

The tab keeps, per API host in sessionStorage, that its calls go direct and
that the store records nothing (`capture: false`), which stops the landings.

A failing cookie adapter no longer fails a recorded call, and connection
errors carry `err.status`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dminch
dminch force-pushed the feature/attribution-headless branch from d8b933c to 6a166c9 Compare November 12, 2026 14:21
- A 429 or a 5xx never switches to the API host, whatever its code, and an
  answer with no effect counts as not Swell's.
- A page on Swell's own domain never switches: an answer that seems not to
  be Swell's there is a failure, such as a body cut off.
- A late answer keeps its flags for the API host it was sent to, not the
  one the client was initialized for since.
- A landing queued behind an answer that nothing is recorded isn't sent,
  nor one whose client a later init pointed at the page's origin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dminch
dminch merged commit 472230a into master Nov 12, 2026
1 check passed
@dminch
dminch deleted the feature/attribution-headless branch November 12, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants