Skip to content

fix(audience): scope tracking state per publishable key - #2947

Merged
JCSanPedro merged 1 commit into
mainfrom
nicholasclancy/sdk-850-audience-web-sdk-leaks-tracking-state-across-games-on
Aug 12, 2026
Merged

JCSanPedro merged 1 commit into
mainfrom
nicholasclancy/sdk-850-audience-web-sdk-leaks-tracking-state-across-games-on

Conversation

@arsenal942

Copy link
Copy Markdown
Contributor

Summary

Scopes the audience SDK's persistent state per publishable key, so a host serving several tenants from one origin no longer leaks tracking state between them.

Linear: SDK-850

Detail and impact of the change

play.immutable.com serves a page per studio, so one browser session spans many publishable keys. None of the SDK's persistent state was keyed by publishable key — the key is only applied as a header at send time — so state crossed tenant boundaries.

Fixed

Attribution replay (fires on every tenant switch). collectSessionAttribution cached under a single __imtbl_attribution sessionStorage key and is read before it is written, so it was never recaptured. The first landing's landing_page, utm_* and click IDs were merged onto every later tenant's first page event. Now cached per publishable key.

Cross-tenant event disclosure via the queue (rare). MessageQueue restored whatever was persisted under one __imtbl_web_queue localStorage key and applied the publishable key only when sending, so a batch left behind by one tenant could be re-sent under another's credential. The queue is now namespaced per key, and the persisted record carries the key it belongs to so a mismatched restore is dropped rather than guessed at. Needs the ~5s flush window to be missed (in-flight flush at unload, >100 backlog, or a failed flush), so it is much rarer than the attribution replay.

Changed

collectSessionAttribution, clearAttribution and setupAutocapture now take the publishable key. Internal to the monorepo — the only call sites are sdk and pixel, both updated here.

Audience.init now reports MULTIPLE_INSTANCES through the existing onError callback alongside the console warning. The condition was already detected, but only via console.warn, which never reaches production monitoring. Deliberately not escalated to a throw: an analytics SDK should not be able to take down its host app, and it would break any consumer currently double-initialising quietly.

Added

clearAttribution and collectPageAttribution are now exported from @imtbl/audience. Both already existed in core — clearAttribution had no callers and was unexported, collectPageAttribution was unreachable from the public surface.

Removed

Pre-namespacing storage keys are deleted on init rather than migrated. Their contents carry no publishable key, so re-homing them would preserve exactly the cross-tenant replay this fixes. The old queue key is only non-empty when the unload flush failed to drain it, so the cost is a small one-time loss per browser.

Anything else worth calling out?

No behaviour change for single-tenant consumers. A studio site only ever has one publishable key, so the namespaced key is stable and behaviour is byte-identical to today. This is the main reason namespacing was chosen over having consumers call clearAttribution imperatively — it is correct by construction and needs no coordination from the consuming app.

Draining the legacy queue was considered and rejected. Persisted messages carry no key (baseMessage stamps messageId, eventTimestamp, anonymousId, surface, context, consentLevel, sessionId), so a drain is necessarily blind and would re-send one tenant's events under whichever key is active — reintroducing the bug once per affected user, via the migration meant to fix it. The persisted record now carries the key so a future migration is not blind.

One known behaviour change worth a second opinion: a campaign landing on a portal's homepage and funnelling into a tenant page no longer passes attribution through, because no instance exists on the homepage to capture it. Today that works only as a side effect of the shared cache.

Testing. 351 core + 137 sdk + 51 pixel tests pass; typecheck clean across all three packages; oxlint clean (pre-existing warnings only). New coverage: per-key attribution scoping, legacy-key clearing, queue key-mismatch and bare-array rejection, MULTIPLE_INSTANCES via onError, and an end-to-end assertion that a second key's first page event carries its own landing page rather than the first key's.

packages/audience/sdk/test/cdn-artifact.test.ts fails in this environment (_imtbl_audience_core is not defined), but fails identically on a clean checkout of main — pre-existing and unrelated.


Generated by Claude Code

A host serving several tenants from one origin (play.immutable.com serves a
page per studio) runs one browser session across many publishable keys. None of
the SDK's persistent state was keyed by publishable key — it is only applied as
a header at send time — so state crossed tenant boundaries.

Attribution: collectSessionAttribution caches under a single sessionStorage
key and is read before it is written, so it is never recaptured. The first
landing's UTM params and click IDs were replayed onto every later tenant's
first page event. Now cached per publishable key.

Queue: MessageQueue restores whatever is persisted under one localStorage key
and applies the publishable key only when sending, so a batch left behind by
one tenant could be re-sent under another's credential. The queue is now
namespaced per key and the persisted record carries the key it belongs to, so
a mismatched restore is dropped rather than guessed at.

Legacy keys are discarded on init, not migrated. Their contents carry no
publishable key, so re-homing them would preserve exactly the cross-tenant
replay this fixes. Only non-empty when the unload flush failed to drain, so the
cost is a small one-time loss.

Also reports MULTIPLE_INSTANCES through onError. The condition was already
detected but only via console.warn, which never reaches production monitoring.

Single-tenant consumers only ever have one key, so behaviour is unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GQSGu6ukLTCTA1SgjFp1Bt
@arsenal942
arsenal942 requested a review from a team as a code owner August 12, 2026 12:28
@github-actions

Copy link
Copy Markdown

✅ Pixel Bundle Size — @imtbl/pixel

Metric Size Delta vs main
Gzipped 7040 bytes (6.87 KB) +72 bytes
Raw (minified) 19599 bytes +274 bytes

Budget: 10.00 KB gzipped (warn at 8.00 KB)

@github-actions

Copy link
Copy Markdown

✅ Audience Bundle Size — @imtbl/audience

Metric Size Delta vs main (f7842da)
Gzipped 11925 bytes (11.64 KB) +135 bytes
Raw (minified) 33600 bytes +452 bytes

Budget: 24.00 KB gzipped (warn at 20.00 KB)

@nx-cloud

nx-cloud Bot commented Aug 12, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit c541876

Command Status Duration Result
nx affected -t build,test ✅ Succeeded 9s View ↗

☁️ Nx Cloud last updated this comment at 2026-08-12 12:29:52 UTC

@JCSanPedro
JCSanPedro added this pull request to the merge queue Aug 12, 2026
Merged via the queue into main with commit 0ffe948 Aug 12, 2026
9 checks passed
@JCSanPedro
JCSanPedro deleted the nicholasclancy/sdk-850-audience-web-sdk-leaks-tracking-state-across-games-on branch August 12, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants