@datadog/browser-rum-shopify 0.0.0 → 7.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +15 -1
  3. package/bundle/chunks/datadogProfiler-34fb088461c1f74ab1c4-datadog-rum-shopify.js +1 -0
  4. package/bundle/chunks/datadogRecorder-c4b4900255dcf6e7d61e-datadog-rum-shopify.js +1 -0
  5. package/bundle/datadog-rum-shopify.js +1 -0
  6. package/cjs/boot/makeShopifyRumPublicApi.d.ts +8 -0
  7. package/cjs/boot/makeShopifyRumPublicApi.js +36 -0
  8. package/cjs/boot/makeShopifyRumPublicApi.js.map +1 -0
  9. package/cjs/boot/patchSandboxedIframeApis.d.ts +15 -0
  10. package/cjs/boot/patchSandboxedIframeApis.js +37 -0
  11. package/cjs/boot/patchSandboxedIframeApis.js.map +1 -0
  12. package/cjs/domain/getSessionReplayLink.d.ts +2 -0
  13. package/cjs/domain/shopifyAnalytics.d.ts +21 -0
  14. package/cjs/domain/shopifyBindings.d.ts +18 -0
  15. package/cjs/domain/shopifyBindings.js +34 -0
  16. package/cjs/domain/shopifyBindings.js.map +1 -0
  17. package/cjs/entries/main.d.ts +1 -0
  18. package/cjs/entries/main.js +9 -0
  19. package/cjs/entries/main.js.map +1 -0
  20. package/esm/boot/makeShopifyRumPublicApi.mjs +36 -0
  21. package/esm/boot/makeShopifyRumPublicApi.mjs.map +1 -0
  22. package/esm/boot/patchSandboxedIframeApis.mjs +37 -0
  23. package/esm/boot/patchSandboxedIframeApis.mjs.map +1 -0
  24. package/esm/domain/shopifyBindings.mjs +34 -0
  25. package/esm/domain/shopifyBindings.mjs.map +1 -0
  26. package/esm/entries/main.mjs +9 -0
  27. package/esm/entries/main.mjs.map +1 -0
  28. package/package.json +32 -1
  29. package/src/boot/makeShopifyRumPublicApi.ts +42 -0
  30. package/src/boot/patchSandboxedIframeApis.ts +35 -0
  31. package/src/domain/getSessionReplayLink.ts +6 -0
  32. package/src/domain/shopifyAnalytics.ts +20 -0
  33. package/src/domain/shopifyBindings.ts +65 -0
  34. package/src/entries/main.ts +19 -0
@@ -0,0 +1,15 @@
1
+ /**
2
+ * A Shopify Custom Pixel runs in a "lax" sandboxed iframe (`sandbox="allow-scripts"`, no
3
+ * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an
4
+ * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on
5
+ * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,
6
+ * which silently breaks session tracking and view/URL resolution:
7
+ * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.
8
+ * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is
9
+ * denied without Permissions Policy delegation), but the SDK only catches an async rejection.
10
+ * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that
11
+ * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.
12
+ * Shimming these away makes the SDK fall back to code paths that do work in this context
13
+ * (`document.cookie`, same-document promise chaining, `globalObject.URL`).
14
+ */
15
+ export declare function patchSandboxedIframeApis(): void;
@@ -0,0 +1,37 @@
1
+ let _datadog_browser_core = require("@datadog/browser-core");
2
+ //#region src/boot/patchSandboxedIframeApis.ts
3
+ /**
4
+ * A Shopify Custom Pixel runs in a "lax" sandboxed iframe (`sandbox="allow-scripts"`, no
5
+ * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an
6
+ * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on
7
+ * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,
8
+ * which silently breaks session tracking and view/URL resolution:
9
+ * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.
10
+ * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is
11
+ * denied without Permissions Policy delegation), but the SDK only catches an async rejection.
12
+ * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that
13
+ * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.
14
+ * Shimming these away makes the SDK fall back to code paths that do work in this context
15
+ * (`document.cookie`, same-document promise chaining, `globalObject.URL`).
16
+ */
17
+ function patchSandboxedIframeApis() {
18
+ disableProperty(_datadog_browser_core.globalObject, "cookieStore");
19
+ disableProperty(navigator, "locks");
20
+ Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
21
+ get() {
22
+ return null;
23
+ },
24
+ configurable: true
25
+ });
26
+ }
27
+ function disableProperty(target, key) {
28
+ Object.defineProperty(target, key, {
29
+ value: void 0,
30
+ configurable: true,
31
+ writable: true
32
+ });
33
+ }
34
+ //#endregion
35
+ exports.patchSandboxedIframeApis = patchSandboxedIframeApis;
36
+
37
+ //# sourceMappingURL=patchSandboxedIframeApis.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patchSandboxedIframeApis.js","names":["globalObject"],"sources":["../../src/boot/patchSandboxedIframeApis.ts"],"sourcesContent":["import { globalObject } from '@datadog/browser-core'\n\n/**\n * A Shopify Custom Pixel runs in a \"lax\" sandboxed iframe (`sandbox=\"allow-scripts\"`, no\n * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an\n * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on\n * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,\n * which silently breaks session tracking and view/URL resolution:\n * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.\n * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is\n * denied without Permissions Policy delegation), but the SDK only catches an async rejection.\n * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that\n * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.\n * Shimming these away makes the SDK fall back to code paths that do work in this context\n * (`document.cookie`, same-document promise chaining, `globalObject.URL`).\n */\nexport function patchSandboxedIframeApis() {\n disableProperty(globalObject, 'cookieStore')\n disableProperty(navigator, 'locks')\n\n Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', {\n get() {\n return null\n },\n configurable: true,\n })\n}\n\nfunction disableProperty(target: object, key: string) {\n Object.defineProperty(target, key, {\n value: undefined,\n configurable: true,\n writable: true,\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAgB,2BAA2B;CACzC,gBAAgBA,sBAAAA,cAAc,aAAa;CAC3C,gBAAgB,WAAW,OAAO;CAElC,OAAO,eAAe,kBAAkB,WAAW,iBAAiB;EAClE,MAAM;GACJ,OAAO;EACT;EACA,cAAc;CAChB,CAAC;AACH;AAEA,SAAS,gBAAgB,QAAgB,KAAa;CACpD,OAAO,eAAe,QAAQ,KAAK;EACjC,OAAO,KAAA;EACP,cAAc;EACd,UAAU;CACZ,CAAC;AACH"}
@@ -0,0 +1,2 @@
1
+ import type { RumConfiguration } from '@datadog/browser-rum-core';
2
+ export declare function getSessionReplayLink(configuration: RumConfiguration): string | undefined;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Payload shape for Shopify Web Pixel standard events.
3
+ * See https://shopify.dev/docs/api/web-pixels-api/standard-events
4
+ */
5
+ export interface ShopifyPixelEvent<TData = Record<string, unknown>> {
6
+ name: string;
7
+ id: string;
8
+ timestamp: string;
9
+ context?: {
10
+ document?: {
11
+ location?: {
12
+ href?: string;
13
+ };
14
+ title?: string;
15
+ };
16
+ };
17
+ data?: TData;
18
+ }
19
+ export interface ShopifyAnalyticsApi {
20
+ subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void;
21
+ }
@@ -0,0 +1,18 @@
1
+ import type { RumPublicApi } from '@datadog/browser-rum-core';
2
+ import type { ShopifyAnalyticsApi } from './shopifyAnalytics';
3
+ export interface ElementData {
4
+ id?: string;
5
+ }
6
+ export interface ErrorData {
7
+ message?: string;
8
+ trace?: string;
9
+ extensionName?: string;
10
+ extensionTarget?: string;
11
+ type?: string;
12
+ appName?: string;
13
+ }
14
+ /**
15
+ * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
16
+ * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
17
+ */
18
+ export declare function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: ShopifyAnalyticsApi | undefined): void;
@@ -0,0 +1,34 @@
1
+ //#region src/domain/shopifyBindings.ts
2
+ const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
3
+ /**
4
+ * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
5
+ * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
6
+ */
7
+ function initShopifyBindings(rumPublicApi, analytics) {
8
+ if (!analytics) return;
9
+ analytics.subscribe("page_viewed", (event) => {
10
+ const url = event.context?.document?.location?.href;
11
+ if (!url || !CHECKOUT_PATH.test(url)) return;
12
+ rumPublicApi.startView({ url });
13
+ });
14
+ analytics.subscribe("clicked", (event) => {
15
+ const name = (event.data?.element)?.id ?? "element-without-id";
16
+ rumPublicApi.startAction(name, { type: "click" });
17
+ rumPublicApi.stopAction(name, { type: "click" });
18
+ });
19
+ analytics.subscribe("ui_extension_errored", (event) => {
20
+ const error = event.data?.error;
21
+ const err = new Error(error?.message);
22
+ err.stack = error?.trace;
23
+ rumPublicApi.addError(err, { extension: {
24
+ name: error?.extensionName,
25
+ target: error?.extensionTarget,
26
+ type: error?.type,
27
+ appName: error?.appName
28
+ } });
29
+ });
30
+ }
31
+ //#endregion
32
+ exports.initShopifyBindings = initShopifyBindings;
33
+
34
+ //# sourceMappingURL=shopifyBindings.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyBindings.js","names":[],"sources":["../../src/domain/shopifyBindings.ts"],"sourcesContent":["import type { RumPublicApi } from '@datadog/browser-rum-core'\nimport type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics'\n\nexport interface ElementData {\n id?: string\n}\n\nexport interface ErrorData {\n message?: string\n trace?: string\n extensionName?: string\n extensionTarget?: string\n type?: string\n appName?: string\n}\n\n// Matches /checkouts/*, /checkout, including locale-prefixed paths.\n// Storefront pages never match, so init() is a no-op there: those pages\n// already get a `DD_RUM` instance from the Theme Liquid snippet, and initializing here too would\n// create a second, independent SDK instance double-tracking the same page view.\nconst CHECKOUT_PATH = /\\/(([a-z]{2}(-[a-z0-9]+)?)\\/)?(checkouts?)(\\/|$)/i\n\n/**\n * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's\n * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).\n */\nexport function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: ShopifyAnalyticsApi | undefined) {\n if (!analytics) {\n return\n }\n\n analytics.subscribe('page_viewed', (event) => {\n const url = event.context?.document?.location?.href\n\n if (!url || !CHECKOUT_PATH.test(url)) {\n return\n }\n\n rumPublicApi.startView({\n url,\n })\n })\n\n analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {\n const element = event.data?.element\n const name = element?.id ?? 'element-without-id'\n rumPublicApi.startAction(name, { type: 'click' })\n rumPublicApi.stopAction(name, { type: 'click' })\n })\n\n // Fires when a Shopify checkout UI extension crashes.\n analytics.subscribe('ui_extension_errored', (event: ShopifyPixelEvent<{ error?: ErrorData }>) => {\n const error = event.data?.error\n const err = new Error(error?.message)\n err.stack = error?.trace\n rumPublicApi.addError(err, {\n extension: {\n name: error?.extensionName,\n target: error?.extensionTarget,\n type: error?.type,\n appName: error?.appName,\n },\n })\n })\n}\n"],"mappings":";AAoBA,MAAM,gBAAgB;;;;;AAMtB,SAAgB,oBAAoB,cAA4B,WAA4C;CAC1G,IAAI,CAAC,WACH;CAGF,UAAU,UAAU,gBAAgB,UAAU;EAC5C,MAAM,MAAM,MAAM,SAAS,UAAU,UAAU;EAE/C,IAAI,CAAC,OAAO,CAAC,cAAc,KAAK,GAAG,GACjC;EAGF,aAAa,UAAU,EACrB,IACF,CAAC;CACH,CAAC;CAED,UAAU,UAAU,YAAY,UAAwD;EAEtF,MAAM,QADU,MAAM,MAAM,QAAA,EACN,MAAM;EAC5B,aAAa,YAAY,MAAM,EAAE,MAAM,QAAQ,CAAC;EAChD,aAAa,WAAW,MAAM,EAAE,MAAM,QAAQ,CAAC;CACjD,CAAC;CAGD,UAAU,UAAU,yBAAyB,UAAoD;EAC/F,MAAM,QAAQ,MAAM,MAAM;EAC1B,MAAM,MAAM,IAAI,MAAM,OAAO,OAAO;EACpC,IAAI,QAAQ,OAAO;EACnB,aAAa,SAAS,KAAK,EACzB,WAAW;GACT,MAAM,OAAO;GACb,QAAQ,OAAO;GACf,MAAM,OAAO;GACb,SAAS,OAAO;EAClB,EACF,CAAC;CACH,CAAC;AACH"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ const require_makeShopifyRumPublicApi = require("../boot/makeShopifyRumPublicApi.js");
2
+ let _datadog_browser_core = require("@datadog/browser-core");
3
+ let _datadog_browser_rum_core = require("@datadog/browser-rum-core");
4
+ let _datadog_browser_rum_internal = require("@datadog/browser-rum/internal");
5
+ //#region src/entries/main.ts
6
+ (0, _datadog_browser_core.defineGlobal)(_datadog_browser_core.globalObject, "DD_RUM", require_makeShopifyRumPublicApi.makeShopifyRumPublicApi((0, _datadog_browser_rum_core.makeRumPublicApi)((0, _datadog_browser_rum_internal.makeRecorderApi)(), (0, _datadog_browser_rum_internal.makeProfilerApi)(), { sdkName: "rum-shopify" })));
7
+ //#endregion
8
+
9
+ //# sourceMappingURL=main.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.js","names":["global","makeShopifyRumPublicApi"],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { defineGlobal, globalObject } from '@datadog/browser-core'\nimport type { RumPublicApi } from '@datadog/browser-rum-core'\nimport { makeRumPublicApi } from '@datadog/browser-rum-core'\nimport { makeRecorderApi, makeProfilerApi } from '@datadog/browser-rum/internal'\nimport { makeShopifyRumPublicApi } from '../boot/makeShopifyRumPublicApi'\n\ninterface BrowserWindow {\n DD_RUM?: RumPublicApi\n}\n\nconst global = globalObject as BrowserWindow\n\nconst datadogRum = makeShopifyRumPublicApi(\n makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {\n sdkName: 'rum-shopify',\n })\n)\n\ndefineGlobal(global, 'DD_RUM', datadogRum)\n"],"mappings":";;;;;CAkBaA,GAAAA,sBAAAA,aAAAA,CAAAA,sBAAAA,cAAQ,UANFC,gCAAAA,yBAAAA,GAAAA,0BAAAA,iBAAAA,EAAAA,GAAAA,8BAAAA,gBAAAA,CACgB,IAAA,GAAA,8BAAA,gBAAA,CAAmB,GAAG,EACrD,SAAS,cACX,CAAC,CAGqC,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { initShopifyBindings } from "../domain/shopifyBindings.mjs";
2
+ import { patchSandboxedIframeApis } from "./patchSandboxedIframeApis.mjs";
3
+ import { mockable } from "@datadog/browser-core";
4
+ //#region src/boot/makeShopifyRumPublicApi.ts
5
+ /**
6
+ * Wraps `RumPublicApi.init()`. When the caller passes a `shopifyAnalytics` handle (the Custom
7
+ * Pixel sandbox's `analytics` global) in the init configuration, patches the sandboxed iframe's
8
+ * APIs, wires Shopify Web Pixel events to the RUM public API, and applies default configuration
9
+ * suited to that iframe (see below). Otherwise (storefront context), forwards to `init()` as-is.
10
+ */
11
+ function makeShopifyRumPublicApi(datadogRum) {
12
+ const originalInit = datadogRum.init.bind(datadogRum);
13
+ datadogRum.init = function(initConfiguration) {
14
+ if ("shopifyAnalytics" in initConfiguration && initConfiguration.shopifyAnalytics) {
15
+ const { shopifyAnalytics: analytics, ...initOptions } = initConfiguration;
16
+ mockable(patchSandboxedIframeApis)();
17
+ mockable(initShopifyBindings)(datadogRum, analytics);
18
+ return originalInit({
19
+ ...initOptions,
20
+ trackViewsManually: true,
21
+ sessionReplaySampleRate: 0,
22
+ profilingSampleRate: 0,
23
+ trackUserInteractions: false,
24
+ trackResources: false,
25
+ trackLongTasks: false,
26
+ sessionPersistence: "cookie"
27
+ });
28
+ }
29
+ return originalInit(initConfiguration);
30
+ };
31
+ return datadogRum;
32
+ }
33
+ //#endregion
34
+ export { makeShopifyRumPublicApi };
35
+
36
+ //# sourceMappingURL=makeShopifyRumPublicApi.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"makeShopifyRumPublicApi.mjs","names":[],"sources":["../../src/boot/makeShopifyRumPublicApi.ts"],"sourcesContent":["import { mockable } from '@datadog/browser-core'\nimport type { RumInitConfiguration, RumPublicApi } from '@datadog/browser-rum-core'\nimport type { ShopifyAnalyticsApi } from '../domain/shopifyAnalytics'\nimport { initShopifyBindings } from '../domain/shopifyBindings'\nimport { patchSandboxedIframeApis } from './patchSandboxedIframeApis'\n\ninterface ShopifyInitConfiguration extends RumInitConfiguration {\n shopifyAnalytics?: ShopifyAnalyticsApi\n}\n\n/**\n * Wraps `RumPublicApi.init()`. When the caller passes a `shopifyAnalytics` handle (the Custom\n * Pixel sandbox's `analytics` global) in the init configuration, patches the sandboxed iframe's\n * APIs, wires Shopify Web Pixel events to the RUM public API, and applies default configuration\n * suited to that iframe (see below). Otherwise (storefront context), forwards to `init()` as-is.\n */\nexport function makeShopifyRumPublicApi(datadogRum: RumPublicApi): RumPublicApi {\n const originalInit = datadogRum.init.bind(datadogRum)\n\n datadogRum.init = function (initConfiguration: RumInitConfiguration | ShopifyInitConfiguration) {\n if ('shopifyAnalytics' in initConfiguration && initConfiguration.shopifyAnalytics) {\n const { shopifyAnalytics: analytics, ...initOptions } = initConfiguration\n mockable(patchSandboxedIframeApis)()\n mockable(initShopifyBindings)(datadogRum, analytics)\n\n return originalInit({\n ...initOptions,\n trackViewsManually: true, // Views are started explicitly via startView()\n sessionReplaySampleRate: 0, // Session Replay is not usable in the Pixel sandbox iframe\n profilingSampleRate: 0, // Profiling is not usable in the Pixel sandbox iframe\n trackUserInteractions: false, // Pixel sandbox iframe has no real DOM to interact with\n trackResources: false, // Iframe resources are not meaningful\n trackLongTasks: false, // PerformanceObserver tracks the empty iframe\n sessionPersistence: 'cookie',\n })\n }\n\n return originalInit(initConfiguration)\n }\n\n return datadogRum\n}\n"],"mappings":";;;;;;;;;;AAgBA,SAAgB,wBAAwB,YAAwC;CAC9E,MAAM,eAAe,WAAW,KAAK,KAAK,UAAU;CAEpD,WAAW,OAAO,SAAU,mBAAoE;EAC9F,IAAI,sBAAsB,qBAAqB,kBAAkB,kBAAkB;GACjF,MAAM,EAAE,kBAAkB,WAAW,GAAG,gBAAgB;GACxD,SAAS,wBAAwB,CAAC,CAAC;GACnC,SAAS,mBAAmB,CAAC,CAAC,YAAY,SAAS;GAEnD,OAAO,aAAa;IAClB,GAAG;IACH,oBAAoB;IACpB,yBAAyB;IACzB,qBAAqB;IACrB,uBAAuB;IACvB,gBAAgB;IAChB,gBAAgB;IAChB,oBAAoB;GACtB,CAAC;EACH;EAEA,OAAO,aAAa,iBAAiB;CACvC;CAEA,OAAO;AACT"}
@@ -0,0 +1,37 @@
1
+ import { globalObject } from "@datadog/browser-core";
2
+ //#region src/boot/patchSandboxedIframeApis.ts
3
+ /**
4
+ * A Shopify Custom Pixel runs in a "lax" sandboxed iframe (`sandbox="allow-scripts"`, no
5
+ * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an
6
+ * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on
7
+ * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,
8
+ * which silently breaks session tracking and view/URL resolution:
9
+ * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.
10
+ * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is
11
+ * denied without Permissions Policy delegation), but the SDK only catches an async rejection.
12
+ * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that
13
+ * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.
14
+ * Shimming these away makes the SDK fall back to code paths that do work in this context
15
+ * (`document.cookie`, same-document promise chaining, `globalObject.URL`).
16
+ */
17
+ function patchSandboxedIframeApis() {
18
+ disableProperty(globalObject, "cookieStore");
19
+ disableProperty(navigator, "locks");
20
+ Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
21
+ get() {
22
+ return null;
23
+ },
24
+ configurable: true
25
+ });
26
+ }
27
+ function disableProperty(target, key) {
28
+ Object.defineProperty(target, key, {
29
+ value: void 0,
30
+ configurable: true,
31
+ writable: true
32
+ });
33
+ }
34
+ //#endregion
35
+ export { patchSandboxedIframeApis };
36
+
37
+ //# sourceMappingURL=patchSandboxedIframeApis.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"patchSandboxedIframeApis.mjs","names":[],"sources":["../../src/boot/patchSandboxedIframeApis.ts"],"sourcesContent":["import { globalObject } from '@datadog/browser-core'\n\n/**\n * A Shopify Custom Pixel runs in a \"lax\" sandboxed iframe (`sandbox=\"allow-scripts\"`, no\n * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an\n * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on\n * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,\n * which silently breaks session tracking and view/URL resolution:\n * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.\n * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is\n * denied without Permissions Policy delegation), but the SDK only catches an async rejection.\n * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that\n * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.\n * Shimming these away makes the SDK fall back to code paths that do work in this context\n * (`document.cookie`, same-document promise chaining, `globalObject.URL`).\n */\nexport function patchSandboxedIframeApis() {\n disableProperty(globalObject, 'cookieStore')\n disableProperty(navigator, 'locks')\n\n Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', {\n get() {\n return null\n },\n configurable: true,\n })\n}\n\nfunction disableProperty(target: object, key: string) {\n Object.defineProperty(target, key, {\n value: undefined,\n configurable: true,\n writable: true,\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,SAAgB,2BAA2B;CACzC,gBAAgB,cAAc,aAAa;CAC3C,gBAAgB,WAAW,OAAO;CAElC,OAAO,eAAe,kBAAkB,WAAW,iBAAiB;EAClE,MAAM;GACJ,OAAO;EACT;EACA,cAAc;CAChB,CAAC;AACH;AAEA,SAAS,gBAAgB,QAAgB,KAAa;CACpD,OAAO,eAAe,QAAQ,KAAK;EACjC,OAAO,KAAA;EACP,cAAc;EACd,UAAU;CACZ,CAAC;AACH"}
@@ -0,0 +1,34 @@
1
+ //#region src/domain/shopifyBindings.ts
2
+ const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
3
+ /**
4
+ * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
5
+ * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
6
+ */
7
+ function initShopifyBindings(rumPublicApi, analytics) {
8
+ if (!analytics) return;
9
+ analytics.subscribe("page_viewed", (event) => {
10
+ const url = event.context?.document?.location?.href;
11
+ if (!url || !CHECKOUT_PATH.test(url)) return;
12
+ rumPublicApi.startView({ url });
13
+ });
14
+ analytics.subscribe("clicked", (event) => {
15
+ const name = (event.data?.element)?.id ?? "element-without-id";
16
+ rumPublicApi.startAction(name, { type: "click" });
17
+ rumPublicApi.stopAction(name, { type: "click" });
18
+ });
19
+ analytics.subscribe("ui_extension_errored", (event) => {
20
+ const error = event.data?.error;
21
+ const err = new Error(error?.message);
22
+ err.stack = error?.trace;
23
+ rumPublicApi.addError(err, { extension: {
24
+ name: error?.extensionName,
25
+ target: error?.extensionTarget,
26
+ type: error?.type,
27
+ appName: error?.appName
28
+ } });
29
+ });
30
+ }
31
+ //#endregion
32
+ export { initShopifyBindings };
33
+
34
+ //# sourceMappingURL=shopifyBindings.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyBindings.mjs","names":[],"sources":["../../src/domain/shopifyBindings.ts"],"sourcesContent":["import type { RumPublicApi } from '@datadog/browser-rum-core'\nimport type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics'\n\nexport interface ElementData {\n id?: string\n}\n\nexport interface ErrorData {\n message?: string\n trace?: string\n extensionName?: string\n extensionTarget?: string\n type?: string\n appName?: string\n}\n\n// Matches /checkouts/*, /checkout, including locale-prefixed paths.\n// Storefront pages never match, so init() is a no-op there: those pages\n// already get a `DD_RUM` instance from the Theme Liquid snippet, and initializing here too would\n// create a second, independent SDK instance double-tracking the same page view.\nconst CHECKOUT_PATH = /\\/(([a-z]{2}(-[a-z0-9]+)?)\\/)?(checkouts?)(\\/|$)/i\n\n/**\n * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's\n * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).\n */\nexport function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: ShopifyAnalyticsApi | undefined) {\n if (!analytics) {\n return\n }\n\n analytics.subscribe('page_viewed', (event) => {\n const url = event.context?.document?.location?.href\n\n if (!url || !CHECKOUT_PATH.test(url)) {\n return\n }\n\n rumPublicApi.startView({\n url,\n })\n })\n\n analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {\n const element = event.data?.element\n const name = element?.id ?? 'element-without-id'\n rumPublicApi.startAction(name, { type: 'click' })\n rumPublicApi.stopAction(name, { type: 'click' })\n })\n\n // Fires when a Shopify checkout UI extension crashes.\n analytics.subscribe('ui_extension_errored', (event: ShopifyPixelEvent<{ error?: ErrorData }>) => {\n const error = event.data?.error\n const err = new Error(error?.message)\n err.stack = error?.trace\n rumPublicApi.addError(err, {\n extension: {\n name: error?.extensionName,\n target: error?.extensionTarget,\n type: error?.type,\n appName: error?.appName,\n },\n })\n })\n}\n"],"mappings":";AAoBA,MAAM,gBAAgB;;;;;AAMtB,SAAgB,oBAAoB,cAA4B,WAA4C;CAC1G,IAAI,CAAC,WACH;CAGF,UAAU,UAAU,gBAAgB,UAAU;EAC5C,MAAM,MAAM,MAAM,SAAS,UAAU,UAAU;EAE/C,IAAI,CAAC,OAAO,CAAC,cAAc,KAAK,GAAG,GACjC;EAGF,aAAa,UAAU,EACrB,IACF,CAAC;CACH,CAAC;CAED,UAAU,UAAU,YAAY,UAAwD;EAEtF,MAAM,QADU,MAAM,MAAM,QAAA,EACN,MAAM;EAC5B,aAAa,YAAY,MAAM,EAAE,MAAM,QAAQ,CAAC;EAChD,aAAa,WAAW,MAAM,EAAE,MAAM,QAAQ,CAAC;CACjD,CAAC;CAGD,UAAU,UAAU,yBAAyB,UAAoD;EAC/F,MAAM,QAAQ,MAAM,MAAM;EAC1B,MAAM,MAAM,IAAI,MAAM,OAAO,OAAO;EACpC,IAAI,QAAQ,OAAO;EACnB,aAAa,SAAS,KAAK,EACzB,WAAW;GACT,MAAM,OAAO;GACb,QAAQ,OAAO;GACf,MAAM,OAAO;GACb,SAAS,OAAO;EAClB,EACF,CAAC;CACH,CAAC;AACH"}
@@ -0,0 +1,9 @@
1
+ import { makeShopifyRumPublicApi } from "../boot/makeShopifyRumPublicApi.mjs";
2
+ import { defineGlobal, globalObject } from "@datadog/browser-core";
3
+ import { makeRumPublicApi } from "@datadog/browser-rum-core";
4
+ import { makeProfilerApi, makeRecorderApi } from "@datadog/browser-rum/internal";
5
+ //#region src/entries/main.ts
6
+ defineGlobal(globalObject, "DD_RUM", makeShopifyRumPublicApi(makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), { sdkName: "rum-shopify" })));
7
+ //#endregion
8
+
9
+ //# sourceMappingURL=main.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.mjs","names":["global"],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { defineGlobal, globalObject } from '@datadog/browser-core'\nimport type { RumPublicApi } from '@datadog/browser-rum-core'\nimport { makeRumPublicApi } from '@datadog/browser-rum-core'\nimport { makeRecorderApi, makeProfilerApi } from '@datadog/browser-rum/internal'\nimport { makeShopifyRumPublicApi } from '../boot/makeShopifyRumPublicApi'\n\ninterface BrowserWindow {\n DD_RUM?: RumPublicApi\n}\n\nconst global = globalObject as BrowserWindow\n\nconst datadogRum = makeShopifyRumPublicApi(\n makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {\n sdkName: 'rum-shopify',\n })\n)\n\ndefineGlobal(global, 'DD_RUM', datadogRum)\n"],"mappings":";;;;;AAkBA,aAAaA,cAAQ,UANF,wBACjB,iBAAiB,gBAAgB,GAAG,gBAAgB,GAAG,EACrD,SAAS,cACX,CAAC,CAGqC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "@datadog/browser-rum-shopify",
3
- "version": "0.0.0",
3
+ "version": "7.8.0",
4
+ "license": "Apache-2.0",
5
+ "main": "cjs/entries/main.js",
6
+ "module": "esm/entries/main.mjs",
7
+ "types": "cjs/entries/main.d.ts",
8
+ "files": [
9
+ "bundle/**/*.js",
10
+ "cjs",
11
+ "esm",
12
+ "src",
13
+ "!src/**/*.spec.ts",
14
+ "!src/**/*.specHelper.ts"
15
+ ],
16
+ "scripts": {
17
+ "build": "node ../../scripts/build/build-package.ts --modules --bundle datadog-rum-shopify.js",
18
+ "build:bundle": "node ../../scripts/build/build-package.ts --bundle datadog-rum-shopify.js",
19
+ "prepack": "yarn build"
20
+ },
21
+ "dependencies": {
22
+ "@datadog/browser-core": "7.8.0",
23
+ "@datadog/browser-rum": "7.8.0",
24
+ "@datadog/browser-rum-core": "7.8.0",
25
+ "@datadog/js-core": "0.0.9"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/DataDog/browser-sdk.git",
30
+ "directory": "packages/browser-rum-shopify"
31
+ },
32
+ "volta": {
33
+ "extends": "../../package.json"
34
+ },
4
35
  "publishConfig": {
5
36
  "access": "public"
6
37
  }
@@ -0,0 +1,42 @@
1
+ import { mockable } from '@datadog/browser-core'
2
+ import type { RumInitConfiguration, RumPublicApi } from '@datadog/browser-rum-core'
3
+ import type { ShopifyAnalyticsApi } from '../domain/shopifyAnalytics'
4
+ import { initShopifyBindings } from '../domain/shopifyBindings'
5
+ import { patchSandboxedIframeApis } from './patchSandboxedIframeApis'
6
+
7
+ interface ShopifyInitConfiguration extends RumInitConfiguration {
8
+ shopifyAnalytics?: ShopifyAnalyticsApi
9
+ }
10
+
11
+ /**
12
+ * Wraps `RumPublicApi.init()`. When the caller passes a `shopifyAnalytics` handle (the Custom
13
+ * Pixel sandbox's `analytics` global) in the init configuration, patches the sandboxed iframe's
14
+ * APIs, wires Shopify Web Pixel events to the RUM public API, and applies default configuration
15
+ * suited to that iframe (see below). Otherwise (storefront context), forwards to `init()` as-is.
16
+ */
17
+ export function makeShopifyRumPublicApi(datadogRum: RumPublicApi): RumPublicApi {
18
+ const originalInit = datadogRum.init.bind(datadogRum)
19
+
20
+ datadogRum.init = function (initConfiguration: RumInitConfiguration | ShopifyInitConfiguration) {
21
+ if ('shopifyAnalytics' in initConfiguration && initConfiguration.shopifyAnalytics) {
22
+ const { shopifyAnalytics: analytics, ...initOptions } = initConfiguration
23
+ mockable(patchSandboxedIframeApis)()
24
+ mockable(initShopifyBindings)(datadogRum, analytics)
25
+
26
+ return originalInit({
27
+ ...initOptions,
28
+ trackViewsManually: true, // Views are started explicitly via startView()
29
+ sessionReplaySampleRate: 0, // Session Replay is not usable in the Pixel sandbox iframe
30
+ profilingSampleRate: 0, // Profiling is not usable in the Pixel sandbox iframe
31
+ trackUserInteractions: false, // Pixel sandbox iframe has no real DOM to interact with
32
+ trackResources: false, // Iframe resources are not meaningful
33
+ trackLongTasks: false, // PerformanceObserver tracks the empty iframe
34
+ sessionPersistence: 'cookie',
35
+ })
36
+ }
37
+
38
+ return originalInit(initConfiguration)
39
+ }
40
+
41
+ return datadogRum
42
+ }
@@ -0,0 +1,35 @@
1
+ import { globalObject } from '@datadog/browser-core'
2
+
3
+ /**
4
+ * A Shopify Custom Pixel runs in a "lax" sandboxed iframe (`sandbox="allow-scripts"`, no
5
+ * `allow-same-origin`): it shares the parent page's cookie jar (same domain) but has an
6
+ * opaque/`null` origin for its own window/document. Several browser APIs the RUM SDK relies on
7
+ * either don't work there or throw synchronously in ways the SDK doesn't fully guard against,
8
+ * which silently breaks session tracking and view/URL resolution:
9
+ * - `cookieStore.getAll()` is blocked; `document.cookie` works fine as a fallback.
10
+ * - `navigator.locks.request()` throws a synchronous `SecurityError` (the Web Locks API is
11
+ * denied without Permissions Policy delegation), but the SDK only catches an async rejection.
12
+ * - `getPristineWindow()` creates a nested iframe to read an unpatched `URL` constructor; that
13
+ * nested iframe's `contentWindow` is cross-origin here, and reading a property off it throws.
14
+ * Shimming these away makes the SDK fall back to code paths that do work in this context
15
+ * (`document.cookie`, same-document promise chaining, `globalObject.URL`).
16
+ */
17
+ export function patchSandboxedIframeApis() {
18
+ disableProperty(globalObject, 'cookieStore')
19
+ disableProperty(navigator, 'locks')
20
+
21
+ Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', {
22
+ get() {
23
+ return null
24
+ },
25
+ configurable: true,
26
+ })
27
+ }
28
+
29
+ function disableProperty(target: object, key: string) {
30
+ Object.defineProperty(target, key, {
31
+ value: undefined,
32
+ configurable: true,
33
+ writable: true,
34
+ })
35
+ }
@@ -0,0 +1,6 @@
1
+ import type { RumConfiguration } from '@datadog/browser-rum-core'
2
+ import { getSessionReplayUrl } from '@datadog/browser-rum-core'
3
+
4
+ export function getSessionReplayLink(configuration: RumConfiguration): string | undefined {
5
+ return getSessionReplayUrl(configuration, { errorType: 'shopify-custom-pixel-package' })
6
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Payload shape for Shopify Web Pixel standard events.
3
+ * See https://shopify.dev/docs/api/web-pixels-api/standard-events
4
+ */
5
+ export interface ShopifyPixelEvent<TData = Record<string, unknown>> {
6
+ name: string
7
+ id: string
8
+ timestamp: string
9
+ context?: {
10
+ document?: {
11
+ location?: { href?: string }
12
+ title?: string
13
+ }
14
+ }
15
+ data?: TData
16
+ }
17
+
18
+ export interface ShopifyAnalyticsApi {
19
+ subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void
20
+ }
@@ -0,0 +1,65 @@
1
+ import type { RumPublicApi } from '@datadog/browser-rum-core'
2
+ import type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics'
3
+
4
+ export interface ElementData {
5
+ id?: string
6
+ }
7
+
8
+ export interface ErrorData {
9
+ message?: string
10
+ trace?: string
11
+ extensionName?: string
12
+ extensionTarget?: string
13
+ type?: string
14
+ appName?: string
15
+ }
16
+
17
+ // Matches /checkouts/*, /checkout, including locale-prefixed paths.
18
+ // Storefront pages never match, so init() is a no-op there: those pages
19
+ // already get a `DD_RUM` instance from the Theme Liquid snippet, and initializing here too would
20
+ // create a second, independent SDK instance double-tracking the same page view.
21
+ const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i
22
+
23
+ /**
24
+ * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
25
+ * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
26
+ */
27
+ export function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: ShopifyAnalyticsApi | undefined) {
28
+ if (!analytics) {
29
+ return
30
+ }
31
+
32
+ analytics.subscribe('page_viewed', (event) => {
33
+ const url = event.context?.document?.location?.href
34
+
35
+ if (!url || !CHECKOUT_PATH.test(url)) {
36
+ return
37
+ }
38
+
39
+ rumPublicApi.startView({
40
+ url,
41
+ })
42
+ })
43
+
44
+ analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {
45
+ const element = event.data?.element
46
+ const name = element?.id ?? 'element-without-id'
47
+ rumPublicApi.startAction(name, { type: 'click' })
48
+ rumPublicApi.stopAction(name, { type: 'click' })
49
+ })
50
+
51
+ // Fires when a Shopify checkout UI extension crashes.
52
+ analytics.subscribe('ui_extension_errored', (event: ShopifyPixelEvent<{ error?: ErrorData }>) => {
53
+ const error = event.data?.error
54
+ const err = new Error(error?.message)
55
+ err.stack = error?.trace
56
+ rumPublicApi.addError(err, {
57
+ extension: {
58
+ name: error?.extensionName,
59
+ target: error?.extensionTarget,
60
+ type: error?.type,
61
+ appName: error?.appName,
62
+ },
63
+ })
64
+ })
65
+ }
@@ -0,0 +1,19 @@
1
+ import { defineGlobal, globalObject } from '@datadog/browser-core'
2
+ import type { RumPublicApi } from '@datadog/browser-rum-core'
3
+ import { makeRumPublicApi } from '@datadog/browser-rum-core'
4
+ import { makeRecorderApi, makeProfilerApi } from '@datadog/browser-rum/internal'
5
+ import { makeShopifyRumPublicApi } from '../boot/makeShopifyRumPublicApi'
6
+
7
+ interface BrowserWindow {
8
+ DD_RUM?: RumPublicApi
9
+ }
10
+
11
+ const global = globalObject as BrowserWindow
12
+
13
+ const datadogRum = makeShopifyRumPublicApi(
14
+ makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {
15
+ sdkName: 'rum-shopify',
16
+ })
17
+ )
18
+
19
+ defineGlobal(global, 'DD_RUM', datadogRum)