Skip to content

FX-083 T4: script side of the signed visitor token (all settings off) - #31

Open
czypnt wants to merge 16 commits into
mainfrom
fx083-script-token
Open

czypnt wants to merge 16 commits into
mainfrom
fx083-script-token

Conversation

@czypnt

@czypnt czypnt commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

FX-083 unit T4: the script side of the signed visitor token. Spec: docs/factory/v1-baseline/whole/FX-083-VISITOR-TOKEN-SPEC.md (sections 2 to 5, the script tests in 6, owner answers in 7).

Everything ships off. New settings (init options, and the dashboard config through the remote-config allowlist): visitorToken: { stripe, whop, calendly, iclosed, checkoutchamp, rawIdFallback }, consentHeader, edgeCookieV2, optOutDeletesVisitorId. With all off: no new request, no link changed, getters unchanged (tests in src/visitor-token-sdk.test.ts, "all settings off", plus an "off" test in each C section and src/queue-consent-header.test.ts).

What is built

  • src/visitor-token.ts (new): the token client and the link decorator.
    • Client: ingest's POST /v1/visitor-token (ingest.datalyr.com, or the configured endpoint when it is a DATALYR one; never the own-domain edge, no cookies) with { workspace_id, visitor_id, consent: 'granted' } in the body (no consent header: ingest's preflight does not allow it). Only anon_<uuid> ids, never while tracking is off or marketing consent is denied. The token is opaque (dv2, visitor id encrypted): accepted as ^[A-Za-z0-9_-]{16,64}$; expiry from expiresAt, at most 2 hours. Cached in memory and sessionStorage dl_vt (bound to the visitor id); renewed when 15 minutes old; one request at a time. 404 / other 4xx / 204 / not JSON / no token: no more minting for the tab session; network, 429 and 5xx retry after a minute.
    • Decorator: only empty fields (a value the site set is kept), exact-host or listed-subdomain matching, re-stamp on click, renewal every 15 minutes while a decoratable link is on the page (no request on a page without one), each element's original value kept per attribute so stop(true) restores it (removal uses the stamped token, not a format test). Iframes are stamped once and left alone by reset() re-issue (rewriting src reloads the widget). Paused on pagehide, re-armed on a back-forward-cache pageshow.
  • src/index.ts: getVisitorToken(), the getters, wiring, reset / opt-out / withdrawal.
  • src/queue.ts: the consent signal (C18).
  • src/identity.ts: forgetAnonymousId() (C21).
  • An inbound dl_vt in the page URL is never adopted (test). It is not stripped from the URL either: the CheckoutChamp funnel page needs it.

Changes to what the SDK sends (each its own commit, each behind a setting that is off)

Commit Change Setting
SEND CHANGE C17 getVisitorToken() and the POST /v1/visitor-token call (only on pages with a decoratable link, or when a getter is used); reset() clears the cache visitorToken.*
SEND CHANGE C13 Stripe Payment Links / pricing table / buy button: client_reference_id = token, not the raw id. A raw id stamped before a late dashboard config arrives is replaced; stripePaymentLinks: false still turns it off; prefilled_email unchanged visitorToken.stripe
SEND CHANGE C14 getStripeMetadata() returns client_reference_id = token and metadata.dl_vt; getWhopCheckoutMetadata() returns dl_vt. Before the token is ready: raw id while raw ids are accepted (rawIdFallback, default true), else empty (owner answer 8). metadata.visitor_id keeps the raw id while accepted visitorToken.stripe / .whop
SEND CHANGE C15 Calendly links / data-url / iframes: salesforce_uuid; iClosed: dl_vt visitorToken.calendly / .iclosed
SEND CHANGE C16 dl_vt on links to checkoutChampDomains (raw _dl_* bridge stays off) visitorToken.checkoutchamp
SEND CHANGE C18 `X-DL-Consent: granted deniedon first-party event requests,dl_consent=` on their beacons
SEND CHANGE C19 Script side: opt-out / withdrawal (setConsent and Shopify decline) stop decorating, restore stamped hrefs, clear the token; POST <endpoint>/v/clear (no body, no custom header, credentials included) once, only on a first-party endpoint edgeCookieV2 for the call
SEND CHANGE C19 (script side) With no stored visitor id (storage wiped), the first event request to a first-party endpoint omits visitor_id / anonymous_id / distinct_id / canonical_id equal to the id minted on this page; the SDK adopts the visitor_id the edge answers with (from __dl_vid_h, T3 contract) and persists it; events queued before are rewritten; token cache and stamped links follow. Never over a stored id, never after reset(); beacons are not stripped edgeCookieV2
SEND CHANGE C17 (fix) Review fixes: opaque token check, mint at ingest with consent in the body, expiresAt, stop for the session; decorator: iframes skipped on re-issue, bfcache re-arm, per-attribute records as above
SEND CHANGE C18 (fix) edgeCookieV2 on implies the consent header (the edge clears __dl_vid_h only when it sees denied) edgeCookieV2
SEND CHANGE C19 (fix) Edge adoption: one id-less request in flight (critical sends included), the rest wait for its answer; an answer without visitor_id keeps the local id; beacons stripped while pending; the minted id inside event_data stripped, then rewritten; a browser with __dl_opt_out (either value) or an optOut() on the page never adopts edgeCookieV2
SEND CHANGE C21 optOut() deletes the stored visitor id (cookie + storage); a new id is persisted after opt-in (owner answer 9) optOutDeletesVisitorId

No Whop link decoration (owner answer 5). C20 is server-side and not in this PR.

Tests

  • src/visitor-token.test.ts: client (request shape, cache, 15-minute renewal, expiry, consent, anon_<uuid> only, other-visitor cache dropped, clear, malformed token, inbound dl_vt not adopted) and decorator (only empty fields, exact host, re-stamp on click, 15-minute renewal while a link is present and none after, iframes once, no consent no request, restore, raw-id replace).
  • src/visitor-token-sdk.test.ts: full init with network mocked; all off; each C change on and off; reset() gives the new visitor's token; withdrawal strips stamped links; /v/clear only with edgeCookieV2 and a first-party endpoint.
  • src/queue-consent-header.test.ts: C18 headers and beacon URL, off byte-identical; edge adoption at queue level.
  • Edge adoption at SDK level: storage empty + edge answer uses the edge id; a stored id is kept; off is today's behaviour.

Rollout order

  1. Server handlers (T2) accept tokens (shadow, then on) BEFORE any visitorToken provider setting goes on: a decorated Stripe link carries only the token, no raw id.
  2. Ingest minting (T1) on.
  3. The edge switch (EDGE_VISITOR_COOKIE_V2) and the script settings edgeCookieV2 (which implies the consent header) go on together.
  4. Then visitorToken providers one at a time, canary hosts first; optOutDeletesVisitorId separately.

Notes for review

  • Core bundle: datalyr.min.js 146954 B min / 40435 B gzip on main; 163134 B min / 44766 B gzip here (+16180 B min, +4331 B gzip). The link decorator is not lazy-loaded yet: that needs a new published bundle (dl.tokens.<v>.js) in datalyr-v2's tracking build, so it is left for a follow-up.

  • X-DL-Consent goes only on event requests (and the new token call); never on /container-scripts or /sdk-consent-policy.

  • Edge adoption: ingest must accept events whose only visitor field is the visitor_id edge fills. A raw-id Stripe link stamped before adoption keeps the minted id (only when the Stripe token is off).

  • Commits depend on the first two (module, C17); C13 to C21 can each be dropped alone.

  • The consent header is not added to the GET requests for /container-scripts and /sdk-consent-policy (a custom header would add a CORS preflight to them).

  • Server pieces this relies on (ingest route, edge /v/clear, preflight for X-DL-Consent, handlers) are other FX-083 units.

Not done

  • Nothing skipped from the T4 list. Built and tested in CI only (no local runs, per the shared-machine rule).

🤖 Generated with Claude Code

czypnt and others added 16 commits November 22, 2026 18:22
…ed, all off)

New module src/visitor-token.ts: the token client (POST /v1/visitor-token,
cache in memory and sessionStorage dl_vt, 15-minute renewal, anon_<uuid> only)
and the link decorator (empty fields only, re-stamp on click, renewal while a
link is on the page, original hrefs kept so stamps can be removed). Settings
visitorToken, consentHeader, edgeCookieV2 and optOutDeletesVisitorId added to
the init options and the remote config allowlist; nothing reads them yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l (behind visitorToken, off)

New public getVisitorToken(): Promise<string|null>. Only when a provider is
switched on in the visitorToken setting, tracking is allowed and marketing
consent is not denied, for anon_<uuid> ids: POST <endpoint>/v1/visitor-token
with { workspace_id, visitor_id } and X-DL-Consent: granted. Cached in memory
and sessionStorage dl_vt until 15 minutes old. reset() clears the cache so
the next token names the new visitor. With the setting off nothing is created
or sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…en in client_reference_id (behind visitorToken.stripe, off)

With visitorToken.stripe on, buy.stripe.com / stripeLinkDomains links and the
stripe-pricing-table / stripe-buy-button embeds get client_reference_id =
the signed token instead of the raw visitor id: only when the field is empty
(a raw id an older stamp wrote is replaced), re-stamped on click, renewed
every 15 minutes while such a link is on the page, re-issued after reset().
The raw-id decorator stops writing client_reference_id while the token is
on; prefilled_email is unchanged. stripePaymentLinks: false still turns all
Stripe decoration off. Off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…turn the signed token (behind visitorToken.stripe / .whop, off)

When the provider is on and the token is ready: getStripeMetadata() returns
client_reference_id = token and metadata.dl_vt = token; getWhopCheckoutMetadata()
returns dl_vt = token. Before the token is ready (owner answer 8): the raw id
(today's value) while raw ids are accepted (visitorToken.rawIdFallback, default
true), else empty values; the first call starts the token request. Return types
gain an optional dl_vt. Off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t dl_vt (behind visitorToken.calendly / .iclosed, off)

With visitorToken.calendly on, calendly.com (and subdomain) links, inline-widget
data-url values and iframes get salesforce_uuid = the signed token; with
visitorToken.iclosed on, iclosed.io links, data-url values and iframes get
dl_vt = the token. Only empty fields; iframes are stamped once (rewriting src
reloads the widget). Off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…und links to checkoutChampDomains (behind visitorToken.checkoutchamp, off)

With visitorToken.checkoutchamp on and checkoutChampDomains set, links to those
domains (and their subdomains) get dl_vt = the signed token, only when empty.
The raw _dl_* bridge stays off and an inbound dl_vt is never adopted by the
funnel page's script. Off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l_consent= on their beacons (behind consentHeader, off)

With consentHeader on, event requests to a first-party endpoint (the
customer's own domain, never *.datalyr.com) carry X-DL-Consent: granted or
denied (denied when tracking is off or marketing consent is withdrawn), and
their beacons carry dl_consent= in the URL (beacons cannot set headers). The
token request always sends X-DL-Consent: granted (it is only made when
allowed). Off: headers and beacon URL byte-identical to today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and ask the edge to clear its copy (script side; POST /v/clear behind edgeCookieV2, off)

optOut(), setConsent() withdrawal (analytics or marketing) and a Shopify
decline now stop the token decorator, put every link it stamped back to its
original href, and clear the cached token (memory and sessionStorage dl_vt).
With edgeCookieV2 on and a first-party endpoint, they also call
POST <endpoint>/v/clear once (credentials included, no body, no custom
header) so the edge clears __dl_vid_h. With every setting off nothing new is
sent and no link changes (no decorator exists).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tDeletesVisitorId, off)

Owner answer 9. With optOutDeletesVisitorId on, optOut() deletes the
__dl_visitor_id cookie (root domain and other domain forms) and the
localStorage copy, and keeps a new anon_<uuid> in memory only; optIn() or a
consent grant persists the new id, so a visitor who opts in again starts as
a new visitor. Off: the id is kept, as today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dge collect names none and the SDK adopts the edge's id (behind edgeCookieV2, off)

When the browser has no stored visitor id (no cookie, no storage, no live
session record, no handoff; e.g. Safari wiped it) and edgeCookieV2 is on, the
first event request to a first-party endpoint goes without visitor_id,
anonymous_id, distinct_id and canonical_id equal to the id minted on this
page, so the edge fills the visitor it kept (__dl_vid_h). The SDK adopts the
visitor_id the edge answers with (persisted like any id); events queued
before that are rewritten to it; the token cache and stamped links follow.
Never over a stored id, never after reset(). Beacons cannot read the answer,
so they are not stripped. Off: payloads byte-identical to today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he body, read expiresAt, stop for the session when there is no mint route; decorator fixes

Review fixes for web#31:
- The token is opaque (dv2, visitor id encrypted): checked only as
  ^[A-Za-z0-9_-]{16,64}$ on mint and cache; removing a stamp uses the token
  kept in the stamp record, not a format test.
- Minting always goes to ingest (ingest.datalyr.com, or the configured
  endpoint when it is a DATALYR one), never the own-domain edge; no cookies.
  Consent goes in the JSON body (consent: 'granted'), no X-DL-Consent header
  on this call. Expiry from T1's expiresAt.
- 404 / other 4xx / 204 / not JSON / no token: stop minting for the tab
  session (sessionStorage dl_vt_off); only network, 429 and 5xx retry after a
  minute.
- Decorator (C13/C15 area): reissue() leaves iframes alone (no second widget
  reload; opt-out still strips them); pagehide pauses and a back-forward-cache
  pageshow re-arms; records are per attribute, so href and data-url on one
  element are both kept and restored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The edge clears __dl_vid_h only when it sees denied, so with edgeCookieV2 on
first-party event requests carry X-DL-Consent (and beacons dl_consent=) even
when consentHeader is off. Both off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… an id keep the local one, inner ids handled, opted-out browsers never adopt

Review fixes for the edge adoption (behind edgeCookieV2, off):
- Every send goes through one gate: while the page's minted id is pending
  only ONE id-less request is in flight (critical sends included); the rest
  wait for its answer and go with the adopted id (or the local one).
- An answer naming no visitor_id (old edge, switch off) stops the stripping;
  the local id is used. An answer that names one is adopted even on 5xx (the
  edge kept that id for the retry).
- Beacons while pending go without the id too.
- The minted id inside event_data (two levels: $identify / $alias data) is
  stripped while pending and rewritten to the adopted id after.
- A browser with a __dl_opt_out cookie (either value) at init, or that calls
  optOut() on this page, never adopts the edge's id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
While the page's minted id waits for the edge's answer (edgeCookieV2, own-
domain endpoint, nothing stored), the token client's get() and peek() return
null: nothing is minted for an id the server may never see, and the getters
fall back as before a token is ready. Once the edge id is adopted, or the edge
answers without one, the decorator re-stamps and the token is minted for the
settled id. Off: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…alendly salesforce_uuid / iClosed dl_vt with the token

Today's setup has customers put the raw visitor id in salesforce_uuid (and
iClosed links), so 'only empty fields' never gave those links a token. A field
whose value is this browser's current raw visitor id, or one it had earlier on
this page (before reset(), or before an adopted edge id), is now replaced by
the token; any other value is kept. Stripe's replaceable rule uses the same
test. Behind visitorToken.calendly / .iclosed (off).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant