Repository navigation
fix(audience): scope tracking state per publishable key - #2947
Merged
JCSanPedro merged 1 commit intoAug 12, 2026
Merged
JCSanPedro merged 1 commit into
JCSanPedro merged 1 commit into
Conversation
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
✅ Pixel Bundle Size — @imtbl/pixel
Budget: 10.00 KB gzipped (warn at 8.00 KB) |
✅ Audience Bundle Size — @imtbl/audience
Budget: 24.00 KB gzipped (warn at 20.00 KB) |
|
View your CI Pipeline Execution ↗ for commit c541876
☁️ Nx Cloud last updated this comment at |
JCSanPedro
approved these changes
Aug 12, 2026
JCSanPedro
deleted the
nicholasclancy/sdk-850-audience-web-sdk-leaks-tracking-state-across-games-on
branch
August 12, 2026 21:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.comserves 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).
collectSessionAttributioncached under a single__imtbl_attributionsessionStorage key and is read before it is written, so it was never recaptured. The first landing'slanding_page,utm_*and click IDs were merged onto every later tenant's firstpageevent. Now cached per publishable key.Cross-tenant event disclosure via the queue (rare).
MessageQueuerestored whatever was persisted under one__imtbl_web_queuelocalStorage 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,clearAttributionandsetupAutocapturenow take the publishable key. Internal to the monorepo — the only call sites aresdkandpixel, both updated here.Audience.initnow reportsMULTIPLE_INSTANCESthrough the existingonErrorcallback alongside the console warning. The condition was already detected, but only viaconsole.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
clearAttributionandcollectPageAttributionare now exported from@imtbl/audience. Both already existed in core —clearAttributionhad no callers and was unexported,collectPageAttributionwas 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
clearAttributionimperatively — 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 (
baseMessagestampsmessageId,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;
oxlintclean (pre-existing warnings only). New coverage: per-key attribution scoping, legacy-key clearing, queue key-mismatch and bare-array rejection,MULTIPLE_INSTANCESviaonError, and an end-to-end assertion that a second key's firstpageevent carries its own landing page rather than the first key's.packages/audience/sdk/test/cdn-artifact.test.tsfails in this environment (_imtbl_audience_core is not defined), but fails identically on a clean checkout ofmain— pre-existing and unrelated.Generated by Claude Code