Skip to content

feat(audience-sdk): add getAnonymousId() and imtbl_aid URL param support#2881

Merged
nattb8 merged 4 commits into
mainfrom
feat/sdk-478-anonymous-id-cross-browser
Jun 9, 2026
Merged

feat(audience-sdk): add getAnonymousId() and imtbl_aid URL param support#2881
nattb8 merged 4 commits into
mainfrom
feat/sdk-478-anonymous-id-cross-browser

Conversation

@nattb8

@nattb8 nattb8 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

When a user clicks a social login option inside an in-app browser (e.g. Facebook), they get booted to the OS browser to complete OAuth — and the Audience SDK mints a fresh anonymous ID in that new context, orphaning all pre-login attribution from the in-app session. This PR fixes that by giving callers the tools to carry the anonymous ID across the browser switch.

  • getAnonymousId() — exposes the current anonymous ID as a public method so integrators can append it to OAuth redirect URLs as ?imtbl_aid=<id>
  • ?imtbl_aid= on init — the SDK reads this param on startup, adopts it as the canonical anonymous ID (writing it to the cookie), and strips it from the URL via history.replaceState

Mirrors the same pattern Segment uses (?ajs_aid=) and what the play repo already implements for Segment. No changes needed to the Pixel — it shares the same cookie and inherits the corrected ID automatically.

Closes SDK-478

Test plan

  • New SDK unit tests cover: imtbl_aid adoption, URL stripping, getAnonymousId() return value matching outgoing event anonymousId, and that imtbl_aid is ignored at none consent
  • All existing SDK tests pass (59 total)

🤖 Generated with Claude Code

…ort (SDK-478)

Enables cross-browser anonymous ID continuity for in-app browser → OS
browser flows (e.g. Facebook in-app browser social login boot-out).
getAnonymousId() exposes the current ID so callers can append it to
OAuth redirect URLs; on init the SDK reads ?imtbl_aid= and adopts it
as the canonical anonymous ID, then strips the param from the URL.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nattb8 nattb8 requested a review from a team as a code owner June 9, 2026 01:42
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

✅ Pixel Bundle Size — @imtbl/pixel

Metric Size Delta vs main
Gzipped 6502 bytes (6.34 KB) +1 bytes
Raw (minified) 18049 bytes 0 bytes

Budget: 10.00 KB gzipped (warn at 8.00 KB)

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown

✅ Audience Bundle Size — @imtbl/audience

Metric Size Delta vs main (844d498)
Gzipped 19017 bytes (18.57 KB) +292 bytes
Raw (minified) 55270 bytes +559 bytes

Budget: 24.00 KB gzipped (warn at 20.00 KB)

@nx-cloud

nx-cloud Bot commented Jun 9, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 1f953e3

Command Status Duration Result
nx run-many -p @imtbl/sdk,@imtbl/checkout-widge... ✅ Succeeded 4s View ↗
nx affected -t build,lint,test ✅ Succeeded 8s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-06-09 04:44:42 UTC

nattb8 and others added 2 commits June 9, 2026 13:52
- Strip imtbl_aid unconditionally (before consent check) so the param
  never lingers in the URL at none consent or gets re-processed on upgrade
- Add JSDoc to adoptAnonymousId noting callers are responsible for consent
- Add URL stripping test for sole-param case (no trailing ?)
- Add test confirming imtbl_aid is stripped even at none consent

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Strip imtbl_aid unconditionally (before consent check) so the param
  never lingers in the URL at none consent or gets re-processed on upgrade
- Move adoptAnonymousId to @imtbl/audience-core/internal subpath export
  so it is no longer public API on the core index
- Drop redundant any casts in new test assertions
- Add URL stripping test for sole-param case (no trailing ?)
- Add test confirming imtbl_aid is stripped even at none consent

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@vqin29 vqin29 requested review from vqin29 and removed request for vqin29 June 9, 2026 03:12
Comment thread packages/audience/sdk/src/sdk.ts Outdated
Comment thread packages/audience/sdk/src/sdk.ts
…state

- Validate imtbl_aid against UUID format before adopting — invalid values
  (empty, non-UUID, excessively long) are stripped from the URL but ignored
- Fix replaceState call to use window.history.state instead of null,
  preserving SPA router state (scroll position, page data, etc.)
- Add tests for invalid imtbl_aid cases and update test fixtures to use
  valid UUID values

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nattb8 nattb8 added this pull request to the merge queue Jun 9, 2026
Merged via the queue into main with commit cb1451d Jun 9, 2026
9 checks passed
@nattb8 nattb8 deleted the feat/sdk-478-anonymous-id-cross-browser branch June 9, 2026 05:19
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.

2 participants