Repository navigation
feat(shopify): Share the storefront session in the app pixel - #5328
BeltranBulbarellaDD wants to merge 9 commits into
Conversation
Bundles Sizes Evolution
|
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: 7d8a402 | Docs | View more details | Give us feedback! |
55f5d74 to
4a9a112
Compare
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.
b35b15c to
fba2599
Compare
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…opify-session-store
There was a problem hiding this comment.
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.
🤖 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.
There was a problem hiding this comment.
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.
🤖 Bits Code Review · Commit 68ce236
| const browser = shopifyBrowserApi | ||
| if (!browser) { | ||
| return initMemorySessionStoreStrategy() | ||
| } |
There was a problem hiding this comment.
❓ question: Can this happen? The app pixel plugin should always set browser before we get here.
There was a problem hiding this comment.
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.
| const cookieOptions = buildCookieOptions(configuration) | ||
| if (!cookieOptions || !(await areCookiesAuthorized(createAccess, cookieOptions))) { | ||
| return initMemorySessionStoreStrategy() | ||
| } |
There was a problem hiding this comment.
💬 suggestion: This check is probably unnecessary in the app pixel context. cookieOptions should always be defined, and we can assume Shopify’s API works
There was a problem hiding this comment.
This check is because Remote Configuration could enable trackSessionAcrossSubdomains and that would crash. So I opted to guard it just in case. WDYT?
Motivation
The Datadog Shopify app Web Pixel runs the RUM SDK (
shopifyAppPixelPlugin) in a sandboxed Web Worker with nodocument.cookieand no Cookie Store API, so the plugin had to forcesessionPersistence: 'memory'— checkout started a new session instead of continuing the storefront one stored in the_dd_s_v2cookie. Cookies are only reachable there through Shopify's asyncbrowser.cookieAPI, which runs in the top frame.Stacked on #5327, which adds the
createSessionStoreoption 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
shopifyAppPixelPluginself-registers the pixel with Shopify (shopify.extend('WebPixel::Render')), reads the RUM configuration from therumConfigpixel setting, shares the storefront session through the Web Pixelbrowser.cookieAPI, forces the worker configuration, and starts views from Shopifypage_viewedevents (in the worker,locationis the pixel script URL, so the page URL comes from the event context). The app pixel extension only needs:initmust be called synchronously when the pixel script is evaluated, so the pixel is registered in time.@shopify/web-pixels-extensionis a types-only dev dependency.shopifyCookieAccess.ts: adapts the Web Pixelbrowser.cookieAPI (async, top frame, no change event) to core'sCookieAccess, polling for changes from other contexts and notifying only on actual changes.shopifySessionStore.ts:createShopifySessionStorefactory — falls back to the previous in-memory session when the Web PixelbrowserAPI is not available, otherwise checks cookie authorization through the Shopify API and builds a cookie session store on_dd_s_v2without legacy-cookie migration.createSessionStoreonly in a worker. This is already scoped to the app pixel: outside workers it isundefined(the storefront custom pixel keeps the regular cookie strategy throughdocument.cookie), and in a workerpreStartRumrefuses to start RUM without theshopify-app-pixelplugin, 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
buildCookieStringfromsetCookie, so the Shopify adapter can write the same cookie string throughbrowser.cookie.set.createCookieSessionStorefrominitCookieStrategy, with areadLegacyCookieoption (legacy_dd_smigration readsdocument.cookie, unavailable in the worker).areCookiesAuthorizedno longer throws when the access factory itself throws.CookieAccess,CookieAccessFactory,CookieOptionstypes andcreateCookieSessionStore,buildCookieString,buildCookieOptions,areCookiesAuthorized,WATCH_COOKIE_INTERVAL_DELAY,initMemorySessionStoreStrategy.Test instructions
rumConfig, mergesrumConfigand forces the worker configuration, hands the Web Pixel browser API to the session store, starts a view with the page URL onpage_viewed; Shopify cookie access (read/write/delete/poll/notify); Shopify session store (defers access, reuses the storefront cookie withoutdocument.cookie, fresh session without legacy read, no session when cookies can't be persisted).shopify.extend, therumConfigsetting andpage_viewedevents, and the test asserts view events flow.Checklist