@datadog/browser-rum-shopify 7.11.0 → 7.12.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.
@@ -19,3 +19,8 @@ export interface ShopifyPixelEvent<TData = Record<string, unknown>> {
19
19
  export interface ShopifyAnalyticsApi {
20
20
  subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void;
21
21
  }
22
+ export declare function getPageUrl(event: ShopifyPixelEvent): string | undefined;
23
+ export interface WaitForPageViewedEventOptions {
24
+ timeout?: number;
25
+ }
26
+ export declare function waitForPageViewedEvent(analytics: ShopifyAnalyticsApi, { timeout }?: WaitForPageViewedEventOptions): Promise<ShopifyPixelEvent>;
@@ -0,0 +1,17 @@
1
+ let _datadog_browser_core = require("@datadog/browser-core");
2
+ //#region src/domain/shopifyAnalytics.ts
3
+ function getPageUrl(event) {
4
+ return event.context?.document?.location?.href;
5
+ }
6
+ function waitForPageViewedEvent(analytics, { timeout = 1e3 } = {}) {
7
+ return (0, _datadog_browser_core.waitForThenable)(new Promise((resolve) => {
8
+ analytics.subscribe("page_viewed", (event) => {
9
+ resolve(event);
10
+ });
11
+ }), timeout);
12
+ }
13
+ //#endregion
14
+ exports.getPageUrl = getPageUrl;
15
+ exports.waitForPageViewedEvent = waitForPageViewedEvent;
16
+
17
+ //# sourceMappingURL=shopifyAnalytics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyAnalytics.js","names":["waitForThenable"],"sources":["../../src/domain/shopifyAnalytics.ts"],"sourcesContent":["import { waitForThenable } from '@datadog/browser-core'\n\n/**\n * Payload shape for Shopify Web Pixel standard events.\n * See https://shopify.dev/docs/api/web-pixels-api/standard-events\n */\nexport interface ShopifyPixelEvent<TData = Record<string, unknown>> {\n name: string\n id: string\n timestamp: string\n context?: {\n document?: {\n location?: { href?: string }\n title?: string\n }\n }\n data?: TData\n}\n\nexport interface ShopifyAnalyticsApi {\n subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void\n}\n\nexport function getPageUrl(event: ShopifyPixelEvent): string | undefined {\n return event.context?.document?.location?.href\n}\n\nexport interface WaitForPageViewedEventOptions {\n timeout?: number\n}\nexport function waitForPageViewedEvent(\n analytics: ShopifyAnalyticsApi,\n { timeout = 1000 }: WaitForPageViewedEventOptions = {}\n): Promise<ShopifyPixelEvent> {\n return waitForThenable(\n new Promise((resolve) => {\n analytics.subscribe('page_viewed', (event) => {\n resolve(event)\n })\n }),\n timeout\n )\n}\n"],"mappings":";;AAuBA,SAAgB,WAAW,OAA8C;CACvE,OAAO,MAAM,SAAS,UAAU,UAAU;AAC5C;AAKA,SAAgB,uBACd,WACA,EAAE,UAAU,QAAwC,CAAC,GACzB;CAC5B,QAAA,GAAOA,sBAAAA,gBAAAA,CACL,IAAI,SAAS,YAAY;EACvB,UAAU,UAAU,gBAAgB,UAAU;GAC5C,QAAQ,KAAK;EACf,CAAC;CACH,CAAC,GACD,OACF;AACF"}
@@ -1,5 +1,5 @@
1
1
  import type { RumPublicApi } from '@datadog/browser-rum-core';
2
- import type { ShopifyAnalyticsApi } from './shopifyAnalytics';
2
+ import type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics';
3
3
  export interface ElementData {
4
4
  id?: string;
5
5
  }
@@ -13,6 +13,8 @@ export interface ErrorData {
13
13
  appName?: string;
14
14
  appVersion?: string;
15
15
  }
16
+ export declare const CHECKOUT_PATH_PATTERN: RegExp;
17
+ export declare function isCheckoutPage(event: ShopifyPixelEvent): boolean;
16
18
  /**
17
19
  * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
18
20
  * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
@@ -1,5 +1,10 @@
1
+ const require_shopifyAnalytics = require("./shopifyAnalytics.js");
1
2
  //#region src/domain/shopifyBindings.ts
2
- const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
3
+ const CHECKOUT_PATH_PATTERN = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
4
+ function isCheckoutPage(event) {
5
+ const url = require_shopifyAnalytics.getPageUrl(event);
6
+ return !!(url && CHECKOUT_PATH_PATTERN.test(url));
7
+ }
3
8
  /**
4
9
  * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
5
10
  * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
@@ -7,16 +12,17 @@ const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
7
12
  function initShopifyBindings(rumPublicApi, analytics) {
8
13
  if (!analytics) return;
9
14
  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 });
15
+ if (!isCheckoutPage(event)) return;
16
+ rumPublicApi.startView({ url: require_shopifyAnalytics.getPageUrl(event) });
13
17
  });
14
18
  analytics.subscribe("clicked", (event) => {
19
+ if (!isCheckoutPage(event)) return;
15
20
  const name = (event.data?.element)?.id ?? "element-without-id";
16
21
  rumPublicApi.startAction(name, { type: "click" });
17
22
  rumPublicApi.stopAction(name, { type: "click" });
18
23
  });
19
24
  analytics.subscribe("ui_extension_errored", (event) => {
25
+ if (!isCheckoutPage(event)) return;
20
26
  const error = event.data?.error;
21
27
  const err = new Error(error?.message);
22
28
  err.stack = error?.trace;
@@ -31,6 +37,8 @@ function initShopifyBindings(rumPublicApi, analytics) {
31
37
  });
32
38
  }
33
39
  //#endregion
40
+ exports.CHECKOUT_PATH_PATTERN = CHECKOUT_PATH_PATTERN;
34
41
  exports.initShopifyBindings = initShopifyBindings;
42
+ exports.isCheckoutPage = isCheckoutPage;
35
43
 
36
44
  //# sourceMappingURL=shopifyBindings.js.map
@@ -1 +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 appId?: string\n appName?: string\n appVersion?: 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 extensionName: error?.extensionName,\n extensionTarget: error?.extensionTarget,\n extensionErrorType: error?.type,\n appId: error?.appId,\n appName: error?.appName,\n appVersion: error?.appVersion,\n })\n })\n}\n"],"mappings":";AAsBA,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;GACzB,eAAe,OAAO;GACtB,iBAAiB,OAAO;GACxB,oBAAoB,OAAO;GAC3B,OAAO,OAAO;GACd,SAAS,OAAO;GAChB,YAAY,OAAO;EACrB,CAAC;CACH,CAAC;AACH"}
1
+ {"version":3,"file":"shopifyBindings.js","names":["getPageUrl"],"sources":["../../src/domain/shopifyBindings.ts"],"sourcesContent":["import type { RumPublicApi } from '@datadog/browser-rum-core'\nimport type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics'\nimport { getPageUrl } 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 appId?: string\n appName?: string\n appVersion?: string\n}\n\n// Matches /checkouts/*, /checkout, including locale-prefixed paths.\nexport const CHECKOUT_PATH_PATTERN = /\\/(([a-z]{2}(-[a-z0-9]+)?)\\/)?(checkouts?)(\\/|$)/i\n\nexport function isCheckoutPage(event: ShopifyPixelEvent): boolean {\n const url = getPageUrl(event)\n return !!(url && CHECKOUT_PATH_PATTERN.test(url))\n}\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 if (!isCheckoutPage(event)) {\n return\n }\n\n rumPublicApi.startView({\n url: getPageUrl(event),\n })\n })\n\n analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {\n if (!isCheckoutPage(event)) {\n return\n }\n\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 if (!isCheckoutPage(event)) {\n return\n }\n\n const error = event.data?.error\n const err = new Error(error?.message)\n err.stack = error?.trace\n rumPublicApi.addError(err, {\n extensionName: error?.extensionName,\n extensionTarget: error?.extensionTarget,\n extensionErrorType: error?.type,\n appId: error?.appId,\n appName: error?.appName,\n appVersion: error?.appVersion,\n })\n })\n}\n"],"mappings":";;AAoBA,MAAa,wBAAwB;AAErC,SAAgB,eAAe,OAAmC;CAChE,MAAM,MAAMA,yBAAAA,WAAW,KAAK;CAC5B,OAAO,CAAC,EAAE,OAAO,sBAAsB,KAAK,GAAG;AACjD;;;;;AAMA,SAAgB,oBAAoB,cAA4B,WAA4C;CAC1G,IAAI,CAAC,WACH;CAGF,UAAU,UAAU,gBAAgB,UAAU;EAC5C,IAAI,CAAC,eAAe,KAAK,GACvB;EAGF,aAAa,UAAU,EACrB,KAAKA,yBAAAA,WAAW,KAAK,EACvB,CAAC;CACH,CAAC;CAED,UAAU,UAAU,YAAY,UAAwD;EACtF,IAAI,CAAC,eAAe,KAAK,GACvB;EAIF,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,IAAI,CAAC,eAAe,KAAK,GACvB;EAGF,MAAM,QAAQ,MAAM,MAAM;EAC1B,MAAM,MAAM,IAAI,MAAM,OAAO,OAAO;EACpC,IAAI,QAAQ,OAAO;EACnB,aAAa,SAAS,KAAK;GACzB,eAAe,OAAO;GACtB,iBAAiB,OAAO;GACxB,oBAAoB,OAAO;GAC3B,OAAO,OAAO;GACd,SAAS,OAAO;GAChB,YAAY,OAAO;EACrB,CAAC;CACH,CAAC;AACH"}
@@ -0,0 +1,12 @@
1
+ import type { RumPlugin } from '@datadog/browser-rum-core';
2
+ import type { ShopifyAnalyticsApi } from './shopifyAnalytics';
3
+ export interface ShopifyPluginConfiguration {
4
+ shopifyAnalytics: ShopifyAnalyticsApi;
5
+ }
6
+ /**
7
+ * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the
8
+ * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify
9
+ * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see
10
+ * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.
11
+ */
12
+ export declare function shopifyPlugin(configuration: ShopifyPluginConfiguration): RumPlugin;
@@ -0,0 +1,35 @@
1
+ const require_patchSandboxedIframeApis = require("../boot/patchSandboxedIframeApis.js");
2
+ const require_shopifyAnalytics = require("./shopifyAnalytics.js");
3
+ const require_shopifyBindings = require("./shopifyBindings.js");
4
+ let _datadog_browser_core = require("@datadog/browser-core");
5
+ //#region src/domain/shopifyPlugin.ts
6
+ /**
7
+ * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the
8
+ * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify
9
+ * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see
10
+ * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.
11
+ */
12
+ function shopifyPlugin(configuration) {
13
+ return {
14
+ name: "shopify",
15
+ async onInit({ initConfiguration, publicApi }) {
16
+ const analytics = configuration.shopifyAnalytics;
17
+ if (!analytics) return false;
18
+ const pageViewedEvent = await require_shopifyAnalytics.waitForPageViewedEvent(analytics);
19
+ if (!require_shopifyBindings.isCheckoutPage(pageViewedEvent)) return false;
20
+ (0, _datadog_browser_core.mockable)(require_patchSandboxedIframeApis.patchSandboxedIframeApis)();
21
+ (0, _datadog_browser_core.mockable)(require_shopifyBindings.initShopifyBindings)(publicApi, analytics);
22
+ initConfiguration.trackViewsManually = true;
23
+ initConfiguration.sessionReplaySampleRate = 0;
24
+ initConfiguration.profilingSampleRate = 0;
25
+ initConfiguration.trackUserInteractions = false;
26
+ initConfiguration.trackResources = false;
27
+ initConfiguration.trackLongTasks = false;
28
+ initConfiguration.sessionPersistence = "cookie";
29
+ }
30
+ };
31
+ }
32
+ //#endregion
33
+ exports.shopifyPlugin = shopifyPlugin;
34
+
35
+ //# sourceMappingURL=shopifyPlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyPlugin.js","names":["waitForPageViewedEvent","isCheckoutPage","patchSandboxedIframeApis","initShopifyBindings"],"sources":["../../src/domain/shopifyPlugin.ts"],"sourcesContent":["import { mockable } from '@datadog/browser-core'\nimport type { RumPlugin } from '@datadog/browser-rum-core'\nimport { patchSandboxedIframeApis } from '../boot/patchSandboxedIframeApis'\nimport type { ShopifyAnalyticsApi } from './shopifyAnalytics'\nimport { initShopifyBindings, isCheckoutPage } from './shopifyBindings'\nimport { waitForPageViewedEvent } from './shopifyAnalytics'\n\nexport interface ShopifyPluginConfiguration {\n shopifyAnalytics: ShopifyAnalyticsApi\n}\n\n/**\n * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the\n * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify\n * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see\n * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.\n */\nexport function shopifyPlugin(configuration: ShopifyPluginConfiguration): RumPlugin {\n return {\n name: 'shopify',\n async onInit({ initConfiguration, publicApi }) {\n const analytics = configuration.shopifyAnalytics\n if (!analytics) {\n return false\n }\n\n const pageViewedEvent = await waitForPageViewedEvent(analytics)\n\n if (!isCheckoutPage(pageViewedEvent)) {\n return false\n }\n\n mockable(patchSandboxedIframeApis)()\n mockable(initShopifyBindings)(publicApi, analytics)\n\n initConfiguration.trackViewsManually = true // Views are started explicitly via startView()\n initConfiguration.sessionReplaySampleRate = 0 // Session Replay is not usable in the Pixel sandbox iframe\n initConfiguration.profilingSampleRate = 0 // Profiling is not usable in the Pixel sandbox iframe\n initConfiguration.trackUserInteractions = false // Pixel sandbox iframe has no real DOM to interact with\n initConfiguration.trackResources = false // Iframe resources are not meaningful\n initConfiguration.trackLongTasks = false // PerformanceObserver tracks the empty iframe\n initConfiguration.sessionPersistence = 'cookie'\n },\n }\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAgB,cAAc,eAAsD;CAClF,OAAO;EACL,MAAM;EACN,MAAM,OAAO,EAAE,mBAAmB,aAAa;GAC7C,MAAM,YAAY,cAAc;GAChC,IAAI,CAAC,WACH,OAAO;GAGT,MAAM,kBAAkB,MAAMA,yBAAAA,uBAAuB,SAAS;GAE9D,IAAI,CAACC,wBAAAA,eAAe,eAAe,GACjC,OAAO;GAGT,CAAA,GAAA,sBAAA,SAAA,CAASC,iCAAAA,wBAAwB,CAAC,CAAC;GACnC,CAAA,GAAA,sBAAA,SAAA,CAASC,wBAAAA,mBAAmB,CAAC,CAAC,WAAW,SAAS;GAElD,kBAAkB,qBAAqB;GACvC,kBAAkB,0BAA0B;GAC5C,kBAAkB,sBAAsB;GACxC,kBAAkB,wBAAwB;GAC1C,kBAAkB,iBAAiB;GACnC,kBAAkB,iBAAiB;GACnC,kBAAkB,qBAAqB;EACzC;CACF;AACF"}
@@ -1,11 +1,12 @@
1
- const require_makeShopifyRumPublicApi = require("../boot/makeShopifyRumPublicApi.js");
1
+ const require_shopifyPlugin = require("../domain/shopifyPlugin.js");
2
2
  let _datadog_js_core_util = require("@datadog/js-core/util");
3
3
  let _datadog_browser_core = require("@datadog/browser-core");
4
4
  let _datadog_browser_rum_core = require("@datadog/browser-rum-core");
5
5
  let _datadog_browser_rum_internal = require("@datadog/browser-rum/internal");
6
6
  //#region src/entries/main.ts
7
7
  const global = _datadog_js_core_util.globalObject;
8
- const datadogRum = require_makeShopifyRumPublicApi.makeShopifyRumPublicApi((0, _datadog_browser_rum_core.makeRumPublicApi)((0, _datadog_browser_rum_internal.makeRecorderApi)(), (0, _datadog_browser_rum_internal.makeProfilerApi)(), { sdkName: "rum-shopify" }));
8
+ const datadogRum = (0, _datadog_browser_rum_core.makeRumPublicApi)((0, _datadog_browser_rum_internal.makeRecorderApi)(), (0, _datadog_browser_rum_internal.makeProfilerApi)(), { sdkName: "rum-shopify" });
9
+ datadogRum.shopifyPlugin = require_shopifyPlugin.shopifyPlugin;
9
10
  (0, _datadog_browser_core.defineGlobal)(global, "DD_RUM", datadogRum);
10
11
  //#endregion
11
12
 
@@ -1 +1 @@
1
- {"version":3,"file":"main.js","names":["globalObject","makeShopifyRumPublicApi","makeRumPublicApi","makeRecorderApi","makeProfilerApi","defineGlobal"],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { globalObject } from '@datadog/js-core/util'\nimport { defineGlobal } 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":";;;;;;AAWA,MAAM,SAASA,sBAAAA;AAEf,MAAM,aAAaC,gCAAAA,yBAAAA,GACjBC,0BAAAA,iBAAAA,EAAAA,GAAiBC,8BAAAA,gBAAAA,CAAgB,IAAA,GAAGC,8BAAAA,gBAAAA,CAAgB,GAAG,EACrD,SAAS,cACX,CAAC,CACH;CAEAC,GAAAA,sBAAAA,aAAAA,CAAa,QAAQ,UAAU,UAAU"}
1
+ {"version":3,"file":"main.js","names":["globalObject","makeRumPublicApi","makeRecorderApi","makeProfilerApi","shopifyPlugin","defineGlobal"],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { globalObject } from '@datadog/js-core/util'\nimport { defineGlobal } 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 { shopifyPlugin } from '../domain/shopifyPlugin'\n\ntype ShopifyRumPublicApi = RumPublicApi & { shopifyPlugin: typeof shopifyPlugin }\n\ninterface BrowserWindow {\n DD_RUM?: ShopifyRumPublicApi\n}\n\nconst global = globalObject as BrowserWindow\n\nconst datadogRum = makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {\n sdkName: 'rum-shopify',\n}) as ShopifyRumPublicApi\n\ndatadogRum.shopifyPlugin = shopifyPlugin\n\ndefineGlobal(global, 'DD_RUM', datadogRum)\n"],"mappings":";;;;;;AAaA,MAAM,SAASA,sBAAAA;AAEf,MAAM,cAAA,GAAaC,0BAAAA,iBAAAA,EAAAA,GAAiBC,8BAAAA,gBAAAA,CAAgB,IAAA,GAAGC,8BAAAA,gBAAAA,CAAgB,GAAG,EACxE,SAAS,cACX,CAAC;AAED,WAAW,gBAAgBC,sBAAAA;CAE3BC,GAAAA,sBAAAA,aAAAA,CAAa,QAAQ,UAAU,UAAU"}
@@ -0,0 +1,16 @@
1
+ import { waitForThenable } from "@datadog/browser-core";
2
+ //#region src/domain/shopifyAnalytics.ts
3
+ function getPageUrl(event) {
4
+ return event.context?.document?.location?.href;
5
+ }
6
+ function waitForPageViewedEvent(analytics, { timeout = 1e3 } = {}) {
7
+ return waitForThenable(new Promise((resolve) => {
8
+ analytics.subscribe("page_viewed", (event) => {
9
+ resolve(event);
10
+ });
11
+ }), timeout);
12
+ }
13
+ //#endregion
14
+ export { getPageUrl, waitForPageViewedEvent };
15
+
16
+ //# sourceMappingURL=shopifyAnalytics.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyAnalytics.mjs","names":[],"sources":["../../src/domain/shopifyAnalytics.ts"],"sourcesContent":["import { waitForThenable } from '@datadog/browser-core'\n\n/**\n * Payload shape for Shopify Web Pixel standard events.\n * See https://shopify.dev/docs/api/web-pixels-api/standard-events\n */\nexport interface ShopifyPixelEvent<TData = Record<string, unknown>> {\n name: string\n id: string\n timestamp: string\n context?: {\n document?: {\n location?: { href?: string }\n title?: string\n }\n }\n data?: TData\n}\n\nexport interface ShopifyAnalyticsApi {\n subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void\n}\n\nexport function getPageUrl(event: ShopifyPixelEvent): string | undefined {\n return event.context?.document?.location?.href\n}\n\nexport interface WaitForPageViewedEventOptions {\n timeout?: number\n}\nexport function waitForPageViewedEvent(\n analytics: ShopifyAnalyticsApi,\n { timeout = 1000 }: WaitForPageViewedEventOptions = {}\n): Promise<ShopifyPixelEvent> {\n return waitForThenable(\n new Promise((resolve) => {\n analytics.subscribe('page_viewed', (event) => {\n resolve(event)\n })\n }),\n timeout\n )\n}\n"],"mappings":";;AAuBA,SAAgB,WAAW,OAA8C;CACvE,OAAO,MAAM,SAAS,UAAU,UAAU;AAC5C;AAKA,SAAgB,uBACd,WACA,EAAE,UAAU,QAAwC,CAAC,GACzB;CAC5B,OAAO,gBACL,IAAI,SAAS,YAAY;EACvB,UAAU,UAAU,gBAAgB,UAAU;GAC5C,QAAQ,KAAK;EACf,CAAC;CACH,CAAC,GACD,OACF;AACF"}
@@ -1,5 +1,10 @@
1
+ import { getPageUrl } from "./shopifyAnalytics.mjs";
1
2
  //#region src/domain/shopifyBindings.ts
2
- const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
3
+ const CHECKOUT_PATH_PATTERN = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
4
+ function isCheckoutPage(event) {
5
+ const url = getPageUrl(event);
6
+ return !!(url && CHECKOUT_PATH_PATTERN.test(url));
7
+ }
3
8
  /**
4
9
  * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
5
10
  * `analytics` global (or `undefined` outside the sandbox, in which case bindings are skipped).
@@ -7,16 +12,17 @@ const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i;
7
12
  function initShopifyBindings(rumPublicApi, analytics) {
8
13
  if (!analytics) return;
9
14
  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 });
15
+ if (!isCheckoutPage(event)) return;
16
+ rumPublicApi.startView({ url: getPageUrl(event) });
13
17
  });
14
18
  analytics.subscribe("clicked", (event) => {
19
+ if (!isCheckoutPage(event)) return;
15
20
  const name = (event.data?.element)?.id ?? "element-without-id";
16
21
  rumPublicApi.startAction(name, { type: "click" });
17
22
  rumPublicApi.stopAction(name, { type: "click" });
18
23
  });
19
24
  analytics.subscribe("ui_extension_errored", (event) => {
25
+ if (!isCheckoutPage(event)) return;
20
26
  const error = event.data?.error;
21
27
  const err = new Error(error?.message);
22
28
  err.stack = error?.trace;
@@ -31,6 +37,6 @@ function initShopifyBindings(rumPublicApi, analytics) {
31
37
  });
32
38
  }
33
39
  //#endregion
34
- export { initShopifyBindings };
40
+ export { CHECKOUT_PATH_PATTERN, initShopifyBindings, isCheckoutPage };
35
41
 
36
42
  //# sourceMappingURL=shopifyBindings.mjs.map
@@ -1 +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 appId?: string\n appName?: string\n appVersion?: 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 extensionName: error?.extensionName,\n extensionTarget: error?.extensionTarget,\n extensionErrorType: error?.type,\n appId: error?.appId,\n appName: error?.appName,\n appVersion: error?.appVersion,\n })\n })\n}\n"],"mappings":";AAsBA,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;GACzB,eAAe,OAAO;GACtB,iBAAiB,OAAO;GACxB,oBAAoB,OAAO;GAC3B,OAAO,OAAO;GACd,SAAS,OAAO;GAChB,YAAY,OAAO;EACrB,CAAC;CACH,CAAC;AACH"}
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'\nimport { getPageUrl } 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 appId?: string\n appName?: string\n appVersion?: string\n}\n\n// Matches /checkouts/*, /checkout, including locale-prefixed paths.\nexport const CHECKOUT_PATH_PATTERN = /\\/(([a-z]{2}(-[a-z0-9]+)?)\\/)?(checkouts?)(\\/|$)/i\n\nexport function isCheckoutPage(event: ShopifyPixelEvent): boolean {\n const url = getPageUrl(event)\n return !!(url && CHECKOUT_PATH_PATTERN.test(url))\n}\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 if (!isCheckoutPage(event)) {\n return\n }\n\n rumPublicApi.startView({\n url: getPageUrl(event),\n })\n })\n\n analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {\n if (!isCheckoutPage(event)) {\n return\n }\n\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 if (!isCheckoutPage(event)) {\n return\n }\n\n const error = event.data?.error\n const err = new Error(error?.message)\n err.stack = error?.trace\n rumPublicApi.addError(err, {\n extensionName: error?.extensionName,\n extensionTarget: error?.extensionTarget,\n extensionErrorType: error?.type,\n appId: error?.appId,\n appName: error?.appName,\n appVersion: error?.appVersion,\n })\n })\n}\n"],"mappings":";;AAoBA,MAAa,wBAAwB;AAErC,SAAgB,eAAe,OAAmC;CAChE,MAAM,MAAM,WAAW,KAAK;CAC5B,OAAO,CAAC,EAAE,OAAO,sBAAsB,KAAK,GAAG;AACjD;;;;;AAMA,SAAgB,oBAAoB,cAA4B,WAA4C;CAC1G,IAAI,CAAC,WACH;CAGF,UAAU,UAAU,gBAAgB,UAAU;EAC5C,IAAI,CAAC,eAAe,KAAK,GACvB;EAGF,aAAa,UAAU,EACrB,KAAK,WAAW,KAAK,EACvB,CAAC;CACH,CAAC;CAED,UAAU,UAAU,YAAY,UAAwD;EACtF,IAAI,CAAC,eAAe,KAAK,GACvB;EAIF,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,IAAI,CAAC,eAAe,KAAK,GACvB;EAGF,MAAM,QAAQ,MAAM,MAAM;EAC1B,MAAM,MAAM,IAAI,MAAM,OAAO,OAAO;EACpC,IAAI,QAAQ,OAAO;EACnB,aAAa,SAAS,KAAK;GACzB,eAAe,OAAO;GACtB,iBAAiB,OAAO;GACxB,oBAAoB,OAAO;GAC3B,OAAO,OAAO;GACd,SAAS,OAAO;GAChB,YAAY,OAAO;EACrB,CAAC;CACH,CAAC;AACH"}
@@ -0,0 +1,35 @@
1
+ import { patchSandboxedIframeApis } from "../boot/patchSandboxedIframeApis.mjs";
2
+ import { waitForPageViewedEvent } from "./shopifyAnalytics.mjs";
3
+ import { initShopifyBindings, isCheckoutPage } from "./shopifyBindings.mjs";
4
+ import { mockable } from "@datadog/browser-core";
5
+ //#region src/domain/shopifyPlugin.ts
6
+ /**
7
+ * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the
8
+ * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify
9
+ * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see
10
+ * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.
11
+ */
12
+ function shopifyPlugin(configuration) {
13
+ return {
14
+ name: "shopify",
15
+ async onInit({ initConfiguration, publicApi }) {
16
+ const analytics = configuration.shopifyAnalytics;
17
+ if (!analytics) return false;
18
+ const pageViewedEvent = await waitForPageViewedEvent(analytics);
19
+ if (!isCheckoutPage(pageViewedEvent)) return false;
20
+ mockable(patchSandboxedIframeApis)();
21
+ mockable(initShopifyBindings)(publicApi, analytics);
22
+ initConfiguration.trackViewsManually = true;
23
+ initConfiguration.sessionReplaySampleRate = 0;
24
+ initConfiguration.profilingSampleRate = 0;
25
+ initConfiguration.trackUserInteractions = false;
26
+ initConfiguration.trackResources = false;
27
+ initConfiguration.trackLongTasks = false;
28
+ initConfiguration.sessionPersistence = "cookie";
29
+ }
30
+ };
31
+ }
32
+ //#endregion
33
+ export { shopifyPlugin };
34
+
35
+ //# sourceMappingURL=shopifyPlugin.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shopifyPlugin.mjs","names":[],"sources":["../../src/domain/shopifyPlugin.ts"],"sourcesContent":["import { mockable } from '@datadog/browser-core'\nimport type { RumPlugin } from '@datadog/browser-rum-core'\nimport { patchSandboxedIframeApis } from '../boot/patchSandboxedIframeApis'\nimport type { ShopifyAnalyticsApi } from './shopifyAnalytics'\nimport { initShopifyBindings, isCheckoutPage } from './shopifyBindings'\nimport { waitForPageViewedEvent } from './shopifyAnalytics'\n\nexport interface ShopifyPluginConfiguration {\n shopifyAnalytics: ShopifyAnalyticsApi\n}\n\n/**\n * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the\n * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify\n * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see\n * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.\n */\nexport function shopifyPlugin(configuration: ShopifyPluginConfiguration): RumPlugin {\n return {\n name: 'shopify',\n async onInit({ initConfiguration, publicApi }) {\n const analytics = configuration.shopifyAnalytics\n if (!analytics) {\n return false\n }\n\n const pageViewedEvent = await waitForPageViewedEvent(analytics)\n\n if (!isCheckoutPage(pageViewedEvent)) {\n return false\n }\n\n mockable(patchSandboxedIframeApis)()\n mockable(initShopifyBindings)(publicApi, analytics)\n\n initConfiguration.trackViewsManually = true // Views are started explicitly via startView()\n initConfiguration.sessionReplaySampleRate = 0 // Session Replay is not usable in the Pixel sandbox iframe\n initConfiguration.profilingSampleRate = 0 // Profiling is not usable in the Pixel sandbox iframe\n initConfiguration.trackUserInteractions = false // Pixel sandbox iframe has no real DOM to interact with\n initConfiguration.trackResources = false // Iframe resources are not meaningful\n initConfiguration.trackLongTasks = false // PerformanceObserver tracks the empty iframe\n initConfiguration.sessionPersistence = 'cookie'\n },\n }\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAgB,cAAc,eAAsD;CAClF,OAAO;EACL,MAAM;EACN,MAAM,OAAO,EAAE,mBAAmB,aAAa;GAC7C,MAAM,YAAY,cAAc;GAChC,IAAI,CAAC,WACH,OAAO;GAGT,MAAM,kBAAkB,MAAM,uBAAuB,SAAS;GAE9D,IAAI,CAAC,eAAe,eAAe,GACjC,OAAO;GAGT,SAAS,wBAAwB,CAAC,CAAC;GACnC,SAAS,mBAAmB,CAAC,CAAC,WAAW,SAAS;GAElD,kBAAkB,qBAAqB;GACvC,kBAAkB,0BAA0B;GAC5C,kBAAkB,sBAAsB;GACxC,kBAAkB,wBAAwB;GAC1C,kBAAkB,iBAAiB;GACnC,kBAAkB,iBAAiB;GACnC,kBAAkB,qBAAqB;EACzC;CACF;AACF"}
@@ -1,11 +1,12 @@
1
- import { makeShopifyRumPublicApi } from "../boot/makeShopifyRumPublicApi.mjs";
1
+ import { shopifyPlugin } from "../domain/shopifyPlugin.mjs";
2
2
  import { globalObject } from "@datadog/js-core/util";
3
3
  import { defineGlobal } from "@datadog/browser-core";
4
4
  import { makeRumPublicApi } from "@datadog/browser-rum-core";
5
5
  import { makeProfilerApi, makeRecorderApi } from "@datadog/browser-rum/internal";
6
6
  //#region src/entries/main.ts
7
7
  const global = globalObject;
8
- const datadogRum = makeShopifyRumPublicApi(makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), { sdkName: "rum-shopify" }));
8
+ const datadogRum = makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), { sdkName: "rum-shopify" });
9
+ datadogRum.shopifyPlugin = shopifyPlugin;
9
10
  defineGlobal(global, "DD_RUM", datadogRum);
10
11
  //#endregion
11
12
 
@@ -1 +1 @@
1
- {"version":3,"file":"main.mjs","names":[],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { globalObject } from '@datadog/js-core/util'\nimport { defineGlobal } 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":";;;;;;AAWA,MAAM,SAAS;AAEf,MAAM,aAAa,wBACjB,iBAAiB,gBAAgB,GAAG,gBAAgB,GAAG,EACrD,SAAS,cACX,CAAC,CACH;AAEA,aAAa,QAAQ,UAAU,UAAU"}
1
+ {"version":3,"file":"main.mjs","names":[],"sources":["../../src/entries/main.ts"],"sourcesContent":["import { globalObject } from '@datadog/js-core/util'\nimport { defineGlobal } 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 { shopifyPlugin } from '../domain/shopifyPlugin'\n\ntype ShopifyRumPublicApi = RumPublicApi & { shopifyPlugin: typeof shopifyPlugin }\n\ninterface BrowserWindow {\n DD_RUM?: ShopifyRumPublicApi\n}\n\nconst global = globalObject as BrowserWindow\n\nconst datadogRum = makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {\n sdkName: 'rum-shopify',\n}) as ShopifyRumPublicApi\n\ndatadogRum.shopifyPlugin = shopifyPlugin\n\ndefineGlobal(global, 'DD_RUM', datadogRum)\n"],"mappings":";;;;;;AAaA,MAAM,SAAS;AAEf,MAAM,aAAa,iBAAiB,gBAAgB,GAAG,gBAAgB,GAAG,EACxE,SAAS,cACX,CAAC;AAED,WAAW,gBAAgB;AAE3B,aAAa,QAAQ,UAAU,UAAU"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datadog/browser-rum-shopify",
3
- "version": "7.11.0",
3
+ "version": "7.12.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "cjs/entries/main.js",
6
6
  "module": "esm/entries/main.mjs",
@@ -19,10 +19,10 @@
19
19
  "prepack": "yarn build"
20
20
  },
21
21
  "dependencies": {
22
- "@datadog/browser-core": "7.11.0",
23
- "@datadog/browser-rum": "7.11.0",
24
- "@datadog/browser-rum-core": "7.11.0",
25
- "@datadog/js-core": "0.0.12"
22
+ "@datadog/browser-core": "7.12.0",
23
+ "@datadog/browser-rum": "7.12.0",
24
+ "@datadog/browser-rum-core": "7.12.0",
25
+ "@datadog/js-core": "0.0.13"
26
26
  },
27
27
  "repository": {
28
28
  "type": "git",
@@ -1,3 +1,5 @@
1
+ import { waitForThenable } from '@datadog/browser-core'
2
+
1
3
  /**
2
4
  * Payload shape for Shopify Web Pixel standard events.
3
5
  * See https://shopify.dev/docs/api/web-pixels-api/standard-events
@@ -18,3 +20,24 @@ export interface ShopifyPixelEvent<TData = Record<string, unknown>> {
18
20
  export interface ShopifyAnalyticsApi {
19
21
  subscribe: (eventName: string, callback: (event: ShopifyPixelEvent) => void) => void
20
22
  }
23
+
24
+ export function getPageUrl(event: ShopifyPixelEvent): string | undefined {
25
+ return event.context?.document?.location?.href
26
+ }
27
+
28
+ export interface WaitForPageViewedEventOptions {
29
+ timeout?: number
30
+ }
31
+ export function waitForPageViewedEvent(
32
+ analytics: ShopifyAnalyticsApi,
33
+ { timeout = 1000 }: WaitForPageViewedEventOptions = {}
34
+ ): Promise<ShopifyPixelEvent> {
35
+ return waitForThenable(
36
+ new Promise((resolve) => {
37
+ analytics.subscribe('page_viewed', (event) => {
38
+ resolve(event)
39
+ })
40
+ }),
41
+ timeout
42
+ )
43
+ }
@@ -1,5 +1,6 @@
1
1
  import type { RumPublicApi } from '@datadog/browser-rum-core'
2
2
  import type { ShopifyAnalyticsApi, ShopifyPixelEvent } from './shopifyAnalytics'
3
+ import { getPageUrl } from './shopifyAnalytics'
3
4
 
4
5
  export interface ElementData {
5
6
  id?: string
@@ -17,10 +18,12 @@ export interface ErrorData {
17
18
  }
18
19
 
19
20
  // Matches /checkouts/*, /checkout, including locale-prefixed paths.
20
- // Storefront pages never match, so init() is a no-op there: those pages
21
- // already get a `DD_RUM` instance from the Theme Liquid snippet, and initializing here too would
22
- // create a second, independent SDK instance double-tracking the same page view.
23
- const CHECKOUT_PATH = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i
21
+ export const CHECKOUT_PATH_PATTERN = /\/(([a-z]{2}(-[a-z0-9]+)?)\/)?(checkouts?)(\/|$)/i
22
+
23
+ export function isCheckoutPage(event: ShopifyPixelEvent): boolean {
24
+ const url = getPageUrl(event)
25
+ return !!(url && CHECKOUT_PATH_PATTERN.test(url))
26
+ }
24
27
 
25
28
  /**
26
29
  * Wires Shopify Web Pixel standard events to the RUM public API. `analytics` is the sandbox's
@@ -32,18 +35,20 @@ export function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: Shopi
32
35
  }
33
36
 
34
37
  analytics.subscribe('page_viewed', (event) => {
35
- const url = event.context?.document?.location?.href
36
-
37
- if (!url || !CHECKOUT_PATH.test(url)) {
38
+ if (!isCheckoutPage(event)) {
38
39
  return
39
40
  }
40
41
 
41
42
  rumPublicApi.startView({
42
- url,
43
+ url: getPageUrl(event),
43
44
  })
44
45
  })
45
46
 
46
47
  analytics.subscribe('clicked', (event: ShopifyPixelEvent<{ element?: ElementData }>) => {
48
+ if (!isCheckoutPage(event)) {
49
+ return
50
+ }
51
+
47
52
  const element = event.data?.element
48
53
  const name = element?.id ?? 'element-without-id'
49
54
  rumPublicApi.startAction(name, { type: 'click' })
@@ -52,6 +57,10 @@ export function initShopifyBindings(rumPublicApi: RumPublicApi, analytics: Shopi
52
57
 
53
58
  // Fires when a Shopify checkout UI extension crashes.
54
59
  analytics.subscribe('ui_extension_errored', (event: ShopifyPixelEvent<{ error?: ErrorData }>) => {
60
+ if (!isCheckoutPage(event)) {
61
+ return
62
+ }
63
+
55
64
  const error = event.data?.error
56
65
  const err = new Error(error?.message)
57
66
  err.stack = error?.trace
@@ -0,0 +1,45 @@
1
+ import { mockable } from '@datadog/browser-core'
2
+ import type { RumPlugin } from '@datadog/browser-rum-core'
3
+ import { patchSandboxedIframeApis } from '../boot/patchSandboxedIframeApis'
4
+ import type { ShopifyAnalyticsApi } from './shopifyAnalytics'
5
+ import { initShopifyBindings, isCheckoutPage } from './shopifyBindings'
6
+ import { waitForPageViewedEvent } from './shopifyAnalytics'
7
+
8
+ export interface ShopifyPluginConfiguration {
9
+ shopifyAnalytics: ShopifyAnalyticsApi
10
+ }
11
+
12
+ /**
13
+ * Adapts the RUM SDK to run inside a Shopify Custom Pixel sandbox. When `shopifyAnalytics` (the
14
+ * sandbox's `analytics` global) is provided, patches the sandboxed iframe's APIs, wires Shopify
15
+ * Web Pixel events to the RUM public API, and forces configuration suited to that iframe (see
16
+ * below). Storefront pages don't provide `shopifyAnalytics` and shouldn't include this plugin.
17
+ */
18
+ export function shopifyPlugin(configuration: ShopifyPluginConfiguration): RumPlugin {
19
+ return {
20
+ name: 'shopify',
21
+ async onInit({ initConfiguration, publicApi }) {
22
+ const analytics = configuration.shopifyAnalytics
23
+ if (!analytics) {
24
+ return false
25
+ }
26
+
27
+ const pageViewedEvent = await waitForPageViewedEvent(analytics)
28
+
29
+ if (!isCheckoutPage(pageViewedEvent)) {
30
+ return false
31
+ }
32
+
33
+ mockable(patchSandboxedIframeApis)()
34
+ mockable(initShopifyBindings)(publicApi, analytics)
35
+
36
+ initConfiguration.trackViewsManually = true // Views are started explicitly via startView()
37
+ initConfiguration.sessionReplaySampleRate = 0 // Session Replay is not usable in the Pixel sandbox iframe
38
+ initConfiguration.profilingSampleRate = 0 // Profiling is not usable in the Pixel sandbox iframe
39
+ initConfiguration.trackUserInteractions = false // Pixel sandbox iframe has no real DOM to interact with
40
+ initConfiguration.trackResources = false // Iframe resources are not meaningful
41
+ initConfiguration.trackLongTasks = false // PerformanceObserver tracks the empty iframe
42
+ initConfiguration.sessionPersistence = 'cookie'
43
+ },
44
+ }
45
+ }
@@ -3,18 +3,20 @@ import { defineGlobal } from '@datadog/browser-core'
3
3
  import type { RumPublicApi } from '@datadog/browser-rum-core'
4
4
  import { makeRumPublicApi } from '@datadog/browser-rum-core'
5
5
  import { makeRecorderApi, makeProfilerApi } from '@datadog/browser-rum/internal'
6
- import { makeShopifyRumPublicApi } from '../boot/makeShopifyRumPublicApi'
6
+ import { shopifyPlugin } from '../domain/shopifyPlugin'
7
+
8
+ type ShopifyRumPublicApi = RumPublicApi & { shopifyPlugin: typeof shopifyPlugin }
7
9
 
8
10
  interface BrowserWindow {
9
- DD_RUM?: RumPublicApi
11
+ DD_RUM?: ShopifyRumPublicApi
10
12
  }
11
13
 
12
14
  const global = globalObject as BrowserWindow
13
15
 
14
- const datadogRum = makeShopifyRumPublicApi(
15
- makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {
16
- sdkName: 'rum-shopify',
17
- })
18
- )
16
+ const datadogRum = makeRumPublicApi(makeRecorderApi(), makeProfilerApi(), {
17
+ sdkName: 'rum-shopify',
18
+ }) as ShopifyRumPublicApi
19
+
20
+ datadogRum.shopifyPlugin = shopifyPlugin
19
21
 
20
22
  defineGlobal(global, 'DD_RUM', datadogRum)
@@ -1,8 +0,0 @@
1
- import type { RumPublicApi } from '@datadog/browser-rum-core';
2
- /**
3
- * Wraps `RumPublicApi.init()`. When the caller passes a `shopifyAnalytics` handle (the Custom
4
- * Pixel sandbox's `analytics` global) in the init configuration, patches the sandboxed iframe's
5
- * APIs, wires Shopify Web Pixel events to the RUM public API, and applies default configuration
6
- * suited to that iframe (see below). Otherwise (storefront context), forwards to `init()` as-is.
7
- */
8
- export declare function makeShopifyRumPublicApi(datadogRum: RumPublicApi): RumPublicApi;
@@ -1,36 +0,0 @@
1
- const require_shopifyBindings = require("../domain/shopifyBindings.js");
2
- const require_patchSandboxedIframeApis = require("./patchSandboxedIframeApis.js");
3
- let _datadog_browser_core = require("@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
- (0, _datadog_browser_core.mockable)(require_patchSandboxedIframeApis.patchSandboxedIframeApis)();
17
- (0, _datadog_browser_core.mockable)(require_shopifyBindings.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
- exports.makeShopifyRumPublicApi = makeShopifyRumPublicApi;
35
-
36
- //# sourceMappingURL=makeShopifyRumPublicApi.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"makeShopifyRumPublicApi.js","names":["patchSandboxedIframeApis","initShopifyBindings"],"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,CAAA,GAAA,sBAAA,SAAA,CAASA,iCAAAA,wBAAwB,CAAC,CAAC;GACnC,CAAA,GAAA,sBAAA,SAAA,CAASC,wBAAAA,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"}