Skip to content

feat: namespace page-view localStorage key and migrate legacy key - #113

Merged
alexs-mparticle merged 12 commits into
developmentfrom
feat/page-view-storage-migration
Aug 12, 2026
Merged

feat: namespace page-view localStorage key and migrate legacy key#113
alexs-mparticle merged 12 commits into
developmentfrom
feat/page-view-storage-migration

Conversation

@alexs-mparticle

@alexs-mparticle alexs-mparticle commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

What

Moves the kit's persisted page-view history out of the unprefixed, top-level mpPageViews localStorage key into a single kit-owned namespace object stored under mp-rokt-kit, with page views held in its pageViews field. This establishes one mp-rokt-kit object where future kit-owned state can live as sibling fields, and adds a one-time migration so existing history survives the move.

Changes

  • Namespace object: page views are now stored as mp-rokt-kit{ "pageViews": [...] }. Future kit-owned state becomes additional fields on the same object rather than new top-level keys.
  • Generalized storage accessors (src/storage.ts): key-agnostic JSON read/write/remove helpers, plus namespaced-field helpers that read-modify-write a single field while preserving its siblings. Page-view semantics stay in the callers.
  • One-time legacy migration: existing mpPageViews history is adopted into the new field on first read, and the legacy key is swept. The migration is a self-contained shim marked for removal after 2027-02-11.
  • Best-effort persistence: page-view storage failures no longer surface as reported errors — selection proceeds with page events omitted.

Migration semantics

The stored field value is a bare array of page views; the only new structure is the enclosing mp-rokt-kit object.

Legacy mpPageViews New mp-rokt-kit.pageViews Result
present absent new field adopts legacy value; legacy key swept
present present new wins; legacy key swept (discarded)
absent either no-op

Note: with targeting disabled, an orphaned legacy mpPageViews may remain until the next read or SessionEnd sweeps it — benign.

Tests

Added a legacy-migration suite (adopt, new-wins, no-op, SessionEnd sweep) and src/storage.spec.ts covering the storage helpers (round-trip, malformed JSON, access/quota errors, sibling preservation, namespace cleanup). Existing page-view tests repointed to the new key/field shape.

Verify: lint · build · tests all pass.

Rename the persisted page-view key from the unprefixed 'mpPageViews' to
'mp-rokt-kit.pageViews', adopting an 'mp-rokt-kit.*' namespace for
kit-owned localStorage keys.

- Generalize the storage accessors to key-agnostic readJSON/writeJSON/
  removeKey so future kit keys can reuse them. All three swallow storage
  failures (private mode, quota, access denied): persisted page views
  are a best-effort cache and a failed read/write/remove must never
  break the caller.
- Centralize reads behind loadPageViews(), which runs a one-time,
  unconditional legacy migration (adopt-if-empty, always-sweep) before
  reading.
- Sweep the legacy key on SessionEnd before clearing the new key.
- Confine all migration logic to migrateLegacyPageViewStorage() +
  LEGACY_PAGE_VIEWS_KEY, marked with a TODO removal date. It works on
  the opaque stored string (raw localStorage) so malformed legacy data
  is still swept, not adopted.
- Guard the selectPlacements read path: the migration touches
  localStorage directly and can throw, which must not break placement
  selection.

The stored value stays a bare array; capacity semantics are unchanged.
@alexs-mparticle
alexs-mparticle marked this pull request as ready for review August 11, 2026 21:30
@alexs-mparticle
alexs-mparticle force-pushed the feat/page-view-storage-migration branch from ef4b617 to 726e4cf Compare August 11, 2026 21:30
Comment thread src/Rokt-Kit.ts Outdated
Route page-view read/write/capture failures through loggingService.log
(INFO diagnostic) instead of errorReportingService.report, matching the
best-effort-cache posture. writeJSON now returns a success flag so a
failed persist is observable without breaking the caller.

Add a test pinning the intentional behavior that the targeting-disabled
init clear does not sweep the legacy mpPageViews key.
Move the key-agnostic localStorage wrappers (readJSON/writeJSON/removeKey)
out of Rokt-Kit.ts into a dedicated src/storage.ts module and import them.
Page-view semantics (keys, migration, load/write/clear) stay in Rokt-Kit.ts.

- Add test/src/storage.spec.ts with dedicated unit tests for the helpers
  (happy paths plus getItem/setItem/removeItem throwing).
- Trim comments in storage.ts to the one load-bearing "never throw /
  best-effort" rationale.
- Update AGENTS.md: reflect the multi-module source layout, note co-located
  src/**/*.spec.ts are run by Vitest, and add a no-unnecessary-comments
  convention.
@alexs-mparticle
alexs-mparticle force-pushed the feat/page-view-storage-migration branch from 67c15c6 to 7b4c9fb Compare August 12, 2026 15:09
Comment thread src/Rokt-Kit.ts Outdated
Comment thread src/Rokt-Kit.ts Outdated
Comment thread src/storage.ts

@jaissica12 jaissica12 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! reminder: can we also make same PR on mP core sdk to align the kit for V3 release

Group kit-owned localStorage state as fields on one JSON object under the
'mp-rokt-kit' key instead of a flat 'mp-rokt-kit.pageViews' key, so future
items can share the namespace. Add read/write/removeNamespacedField helpers
and migrate the legacy 'mpPageViews' array into the pageViews field.
Comment thread src/Rokt-Kit.ts Outdated
Comment thread src/Rokt-Kit.ts Outdated
Comment thread src/Rokt-Kit.ts Outdated
Comment thread test/src/tests.spec.ts Outdated
Comment thread test/src/tests.spec.ts Outdated
Comment thread test/src/tests.spec.ts Outdated
- migrateLegacyPageViewStorage reads the legacy key once via readJSON
  instead of a getItem + readJSON pair
- tests reuse storage.ts helpers (readJSON/readNamespacedField/
  writeNamespacedField) instead of re-implementing localStorage parsing
Comment thread src/Rokt-Kit.ts Outdated
migrateLegacyPageViewStorage takes the logging service and emits a
diagnostic INFO log (retaining the legacy key for retry) instead of
throwing on a failed adopt. loadPageViews forwards the logger. With the
migration no longer throwing, the purpose-built try/catch blocks in
selectPlacements and the SessionEnd handler are removed.
@alexs-mparticle
alexs-mparticle merged commit b428b15 into development Aug 12, 2026
4 of 5 checks passed
@alexs-mparticle
alexs-mparticle deleted the feat/page-view-storage-migration branch August 12, 2026 18:03
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.

4 participants