Skip to content

feat(shopify): Share the storefront session in the app pixel - #5328

Open
BeltranBulbarellaDD wants to merge 9 commits into
mainfrom
beltran.bulbarella/shopify-session-store
Open

BeltranBulbarellaDD wants to merge 9 commits into
mainfrom
beltran.bulbarella/shopify-session-store

Conversation

@BeltranBulbarellaDD

@BeltranBulbarellaDD BeltranBulbarellaDD commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

The Datadog Shopify app Web Pixel runs the RUM SDK (shopifyAppPixelPlugin) in a sandboxed Web Worker with no document.cookie and no Cookie Store API, so the plugin had to force sessionPersistence: 'memory' — checkout started a new session instead of continuing the storefront one stored in the _dd_s_v2 cookie. Cookies are only reachable there through Shopify's async browser.cookie API, which runs in the top frame.

Stacked on #5327, which adds the createSessionStore option this PR uses.

Refs: https://datadoghq.atlassian.net/wiki/spaces/RUMP/pages/7438730478/Sharing+the+storefront+session+in+the+Shopify+app+pixel

Changes

browser-rum-shopify

  • shopifyAppPixelPlugin self-registers the pixel with Shopify (shopify.extend('WebPixel::Render')), reads the RUM configuration from the rumConfig pixel setting, shares the storefront session through the Web Pixel browser.cookie API, forces the worker configuration, and starts views from Shopify page_viewed events (in the worker, location is the pixel script URL, so the page URL comes from the event context). The app pixel extension only needs:
    self.importScripts(".../datadog-rum-shopify.js")
    self.DD_RUM.init({ plugins: [self.DD_RUM.shopifyAppPixelPlugin()] })
    init must be called synchronously when the pixel script is evaluated, so the pixel is registered in time. @shopify/web-pixels-extension is a types-only dev dependency.
  • shopifyCookieAccess.ts: adapts the Web Pixel browser.cookie API (async, top frame, no change event) to core's CookieAccess, polling for changes from other contexts and notifying only on actual changes.
  • shopifySessionStore.ts: createShopifySessionStore factory — falls back to the previous in-memory session when the Web Pixel browser API is not available, otherwise checks cookie authorization through the Shopify API and builds a cookie session store on _dd_s_v2 without legacy-cookie migration.
  • The bundle entry passes createSessionStore only in a worker. This is already scoped to the app pixel: outside workers it is undefined (the storefront custom pixel keeps the regular cookie strategy through document.cookie), and in a worker preStartRum refuses to start RUM without the shopify-app-pixel plugin, so the factory is only ever invoked when the app pixel plugin is in use.

Note: Atm there is no way to expand the session. This will be worked in a follow-up PR.

browser-core

  • Extracted buildCookieString from setCookie, so the Shopify adapter can write the same cookie string through browser.cookie.set.
  • Extracted createCookieSessionStore from initCookieStrategy, with a readLegacyCookie option (legacy _dd_s migration reads document.cookie, unavailable in the worker).
  • areCookiesAuthorized no longer throws when the access factory itself throws.
  • Exports used by the Shopify package: CookieAccess, CookieAccessFactory, CookieOptions types and createCookieSessionStore, buildCookieString, buildCookieOptions, areCookiesAuthorized, WATCH_COOKIE_INTERVAL_DELAY, initMemorySessionStoreStrategy.

Test instructions

  • New unit tests: plugin aborts outside a Web Pixel extension or with invalid rumConfig, merges rumConfig and forces the worker configuration, hands the Web Pixel browser API to the session store, starts a view with the page URL on page_viewed; Shopify cookie access (read/write/delete/poll/notify); Shopify session store (defers access, reuses the storefront cookie without document.cookie, fresh session without legacy read, no session when cookies can't be persisted).
  • The "dedicated web worker with worker rum" e2e now exercises the self-registration path: a minimal fake Web Pixel sandbox provides shopify.extend, the rumConfig setting and page_viewed events, and the test asserts view events flow.

Checklist

  • Tested locally
  • Tested on staging
  • Added unit tests for this change.
  • Added e2e/integration tests for this change.
  • Updated documentation and/or relevant AGENTS.md file

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Bundles Sizes Evolution

📦 Bundle Name Base Size Local Size 𝚫 𝚫% Status
Rum Salesforce 194.72 KiB 194.81 KiB +98 B +0.05% ✅
Rum 194.70 KiB 194.79 KiB +98 B +0.05% ✅
Rum Salesforce Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Salesforce Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Rum Recorder 32.80 KiB 32.80 KiB 0 B 0.00% ✅
Logs 60.45 KiB 60.54 KiB +98 B +0.16% ✅
Rum Slim 150.09 KiB 150.18 KiB +98 B +0.06% ✅
Worker 24.23 KiB 24.23 KiB 0 B 0.00% ✅
Rum Shopify 226.24 KiB 228.69 KiB +2.45 KiB +1.08% ✅
Rum Shopify Profiler 7.93 KiB 7.93 KiB 0 B 0.00% ✅
Rum Shopify Recorder 4.85 KiB 4.85 KiB 0 B 0.00% ✅
Early Collection 309 B 309 B 0 B 0.00% ✅

@datadog-official

datadog-official Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Pipelines  Tests

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🎯 Code Coverage (details)
• Patch Coverage: 85.48%
• Overall Coverage: 78.08% (+0.02%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 7d8a402 | Docs | View more details | Give us feedback!

@BeltranBulbarellaDD
BeltranBulbarellaDD force-pushed the beltran.bulbarella/shopify-session-store branch from 55f5d74 to 4a9a112 Compare November 26, 2026 14:30
Base automatically changed from beltran.bulbarella/session-store-sdk to main November 30, 2026 09:55
Wire page_viewed, clicked and ui_extension_errored through initShopifyBindings
instead of an inline page_viewed handler, so the app pixel shares the checkout
page filtering with the other Shopify entry points.

Move the session store files into domain/session.

Keep the worker init E2E to init only: Shopify behavior coverage belongs in its
own scenario.
…ersisted

Fall back to an isolated in-memory session when `browser.cookie` cannot
persist the session cookie, as already done when the browser API is missing.

Also simplify the Shopify cookie access and the session store tests.
@BeltranBulbarellaDD
BeltranBulbarellaDD force-pushed the beltran.bulbarella/shopify-session-store branch from b35b15c to fba2599 Compare November 30, 2026 09:55
@BeltranBulbarellaDD
BeltranBulbarellaDD marked this pull request as ready for review November 30, 2026 09:55
@BeltranBulbarellaDD
BeltranBulbarellaDD requested a review from a team as a code owner November 30, 2026 09:55
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T18:13:08.677970Z 7d8a402 New commits
🔒 Security Review ✅ Completed 2026-12-04T18:15:34.677347Z 7d8a402 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: FAIL

Domain-scoped storefront cookies can cause checkout to read stale session state after writes. A rumConfig containing plugins: [] also removes the required app pixel plugin and prevents RUM startup.

Open Bits AI session

🤖 Bits Code Review · Commit fba2599

…xel settings

settings.rumConfig is customer-provided JSON: a plugins value in it would
drop the plugins passed to init(), starting with the shopify app pixel
plugin itself, and the preStartRum worker guard would then reject the
initialization entirely. Plugins can't be serialized to JSON, so any such
value is ignored.

@datadog-official datadog-official Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The PR shares storefront sessions with Shopify’s app pixel through its asynchronous cookie API. Preserving init plugins fixes the worker-startup failure. The cookie-scope finding is closed following the author’s supported-configuration clarification: session splitting with hand-written cross-subdomain configurations is an accepted limitation. No reportable findings remain.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 68ce236

Comment thread test/e2e/scenario/rum/init.scenario.ts Resolved
const browser = shopifyBrowserApi
if (!browser) {
return initMemorySessionStoreStrategy()
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

❓ question: ‏Can this happen? The app pixel plugin should always set browser before we get here.

@BeltranBulbarellaDD BeltranBulbarellaDD Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep I tried removing it but this breaks the init e2e test. I tried to keep this test as a quick check that the Shopify App Pixel plugin inits and for it, it needs to fallback to memory session strategy.
The other alternative I see is to mock the cookie in the test, but since later we will to a complete e2e I don't see the need for it.

Comment on lines +31 to +34
const cookieOptions = buildCookieOptions(configuration)
if (!cookieOptions || !(await areCookiesAuthorized(createAccess, cookieOptions))) {
return initMemorySessionStoreStrategy()
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

💬 suggestion: ‏This check is probably unnecessary in the app pixel context. cookieOptions should always be defined, and we can assume Shopify’s API works

@BeltranBulbarellaDD BeltranBulbarellaDD Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check is because Remote Configuration could enable trackSessionAcrossSubdomains and that would crash. So I opted to guard it just in case. WDYT?

This branch has not been deployed

No deployments
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.

3 participants