@depict-ai/dpc 1.0.31 → 1.0.33

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.
package/latest/index.js CHANGED
@@ -1,285 +1,433 @@
1
- import { add_to_version_info as t, dlog as e, impression_track_element as i, derror as s, depict_click as r, foolproof_send_beacon as n, observer as o, instant_exec_on_suspect_history_change as a, catchify as d, get_tracking_event_context_data as c, send_depict_unfiltered as u } from "@depict-ai/utilishared/latest";
2
- import h from "@babel/runtime/helpers/classPrivateFieldLooseBase";
3
- import _ from "@babel/runtime/helpers/classPrivateFieldLooseKey";
1
+ import { add_to_version_info as $lbGMC$add_to_version_info, dlog as $lbGMC$dlog, instant_exec_on_suspect_history_change as $lbGMC$instant_exec_on_suspect_history_change, catchify as $lbGMC$catchify, get_tracking_event_context_data as $lbGMC$get_tracking_event_context_data, impression_track_element as $lbGMC$impression_track_element, derror as $lbGMC$derror, send_depict_unfiltered as $lbGMC$send_depict_unfiltered, depict_click as $lbGMC$depict_click, foolproof_send_beacon as $lbGMC$foolproof_send_beacon, base_url as $lbGMC$base_url, observer as $lbGMC$observer } from "@depict-ai/utilishared/latest";
2
+
4
3
  /*! Depict PerformanceClient */
5
4
  import { register_es_version } from "../multi_import_deduplicator.mjs";
6
5
  register_es_version("performance-client", "latest");
7
- const l = "1.0.31";
8
- let m;
9
- var p;
10
- (p = m || (m = {}))[p.Recommendation = 0] = "Recommendation", p[p.SnakeRecommendation = 1] = "SnakeRecommendation", p[p.SearchResult = 2] = "SearchResult", p[p.ProductListingResult = 3] = "ProductListingResult";
11
- const v = {
12
- [m.Recommendation]: "recommendation_id",
13
- [m.SnakeRecommendation]: "recommendation_id",
14
- [m.SearchResult]: "search_result_id",
15
- [m.ProductListingResult]: "product_listing_result_id"
16
- },
17
- f = ".depict-add-to-cart",
18
- k = {
19
- "data-recommendation_id": m.SnakeRecommendation,
20
- "data-recommendation-id": m.Recommendation,
21
- "data-search-result-id": m.SearchResult,
22
- "data-product-listing-result-id": m.ProductListingResult
6
+ const $3b068891cefe570d$export$83d89fbfd8236492 = "1.0.33";
7
+ let $ba8dc1a552d27224$export$8ae64f3d447c14f7;
8
+ (function (ProductCardType) {
9
+ ProductCardType[ProductCardType["Recommendation"] = 0] = "Recommendation";
10
+ ProductCardType[ProductCardType["SnakeRecommendation"] = 1] = "SnakeRecommendation";
11
+ ProductCardType[ProductCardType["SearchResult"] = 2] = "SearchResult";
12
+ ProductCardType[ProductCardType["ProductListingResult"] = 3] = "ProductListingResult";
13
+ })($ba8dc1a552d27224$export$8ae64f3d447c14f7 || ($ba8dc1a552d27224$export$8ae64f3d447c14f7 = {}));
14
+ const $ba8dc1a552d27224$export$7c03f17546479d1d = {
15
+ [$ba8dc1a552d27224$export$8ae64f3d447c14f7.Recommendation]: "recommendation_id",
16
+ [$ba8dc1a552d27224$export$8ae64f3d447c14f7.SnakeRecommendation]: "recommendation_id",
17
+ [$ba8dc1a552d27224$export$8ae64f3d447c14f7.SearchResult]: "search_result_id",
18
+ [$ba8dc1a552d27224$export$8ae64f3d447c14f7.ProductListingResult]: "product_listing_result_id"
19
+ }; /**
20
+ * The Depict Queue function used to register events to be sent to depict.
21
+ */ /**
22
+ * Depict Object containing everything depict-related on the page.
23
+ */ /**
24
+ * Purchase format used by GA4 when sending a purchase event.
25
+ * See https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund
26
+ */ /**
27
+ * Product format used by GA4 when sending a purchase event.
28
+ * See https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase_item
29
+ */
30
+
31
+ const $f956499541bad8b0$export$6f11e212918dbb31 = ".depict-add-to-cart";
32
+ const $f956499541bad8b0$export$79f7900b4eea40cf = {
33
+ // We sadly have customers in the wild using both _ and -
34
+ "data-recommendation_id": (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).SnakeRecommendation,
35
+ "data-recommendation-id": (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).Recommendation,
36
+ // All of these product cards are Depict rendered via js or react ui, so we know the data attribute
37
+ "data-search-result-id": (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).SearchResult,
38
+ "data-product-listing-result-id": (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).ProductListingResult
39
+ };
40
+
41
+ /**
42
+ * Creates an event queue (depict queue)
43
+ */
44
+ function $b1ff6e3aa9b3b030$var$create_dpq() {
45
+ const fn = function () {
46
+ const args = arguments;
47
+ if (fn.send_event) fn.send_event.apply(this, args);else fn.queue.push(args);
23
48
  };
24
- const g = (t, e) => {
25
- if ("number" == typeof e && !isNaN(e)) return e.toString();
26
- if ("string" != typeof e) throw new Error(`${t} event requires a string as an argument`);
27
- return e;
28
- },
29
- y = (t, e) => null == e ? e : g(t, e),
30
- b = (t, e, i) => {
31
- if ("purchase" !== e) "number" == typeof i && (i = i.toString()), "addToCart" === e ? t.sendAddToCartEvent({
32
- product_id: g(e, i)
33
- }) : "setMarket" === e ? t.setMarket(g(e, i)) : "setProductId" === e ? t.setProductId(g(e, i)) : "setUserId" === e ? t.setUserId(y(e, i)) : "setSessionId" === e && t.setSessionId(y(e, i));else {
34
- if (!(t => {
35
- if ("object" != typeof t || null === t) throw new Error("purchase event requires an object as an argument");
36
- if (!Array.isArray(t.items)) throw new Error("purchase event requires an items array of purchased products");
37
- if (!t.currency || "string" != typeof t.currency) throw new Error("purchase event requires a currency string");
38
- if (!t.transaction_id || "string" != typeof t.transaction_id && "number" != typeof t.transaction_id) throw new Error("purchase event requires a transaction_id string");
39
- t.transaction_id = t.transaction_id.toString();
40
- for (const e of t.items) {
41
- if ("" == e.item_id || "string" != typeof e.item_id && ("number" != typeof e.item_id || isNaN(e.item_id))) throw new Error("purchased item requires an item_id of the purchased product");
42
- if (isNaN(e.price)) throw new Error("purchase item requires a unit price for the product purchased");
43
- if (!e.quantity || isNaN(e.quantity)) throw new Error("purchase item requires a quantity of products purchased");
44
- e.item_id = e.item_id.toString();
45
- }
46
- return !0;
47
- })(i)) return;
48
- t.sendPurchaseEvent(i);
49
+ fn.queue = [];
50
+ return fn;
51
+ }
52
+ const $b1ff6e3aa9b3b030$var$string_guard = (eventName, eventData) => {
53
+ if (typeof eventData == "number" && !isNaN(eventData)) return eventData.toString();
54
+ if (typeof eventData !== "string") throw new Error(`${eventName} event requires a string as an argument`);
55
+ return eventData;
56
+ };
57
+ const $b1ff6e3aa9b3b030$var$nullable_undefined_string_guard = (eventName, eventData) => {
58
+ if (eventData === null || eventData === undefined) return eventData;
59
+ return $b1ff6e3aa9b3b030$var$string_guard(eventName, eventData);
60
+ };
61
+ const $b1ff6e3aa9b3b030$var$ga4_purchase_guard = purchaseEvent => {
62
+ if (typeof purchaseEvent !== "object" || purchaseEvent === null) throw new Error("purchase event requires an object as an argument");
63
+ if (!Array.isArray(purchaseEvent.items)) throw new Error("purchase event requires an items array of purchased products");else if (!purchaseEvent.currency || typeof purchaseEvent.currency !== "string") throw new Error("purchase event requires a currency string");else if (!purchaseEvent.transaction_id || !(typeof purchaseEvent.transaction_id == "string" || typeof purchaseEvent.transaction_id == "number")) throw new Error("purchase event requires a transaction_id string");
64
+ // Transaction id could be either number or string at this point, automatically convert the latter
65
+ purchaseEvent.transaction_id = purchaseEvent.transaction_id.toString();
66
+ for (const item of purchaseEvent.items) {
67
+ if (item.item_id == "" || !(typeof item.item_id == "string" || typeof item.item_id == "number" && !isNaN(item.item_id))) throw new Error("purchased item requires an item_id of the purchased product");else if (isNaN(item.price))
68
+ // ^ 0 cost items are valid
69
+ throw new Error("purchase item requires a unit price for the product purchased");else if (!item.quantity || isNaN(item.quantity))
70
+ // ^ But 0 quantity items are not
71
+ throw new Error("purchase item requires a quantity of products purchased");
72
+ // Item id could be either number or string at this point, automatically convert the latter
73
+ item.item_id = item.item_id.toString();
74
+ }
75
+ return true;
76
+ };
77
+ /**
78
+ * Handle events from dpq with the global dpc instance.
79
+ */
80
+ const $b1ff6e3aa9b3b030$var$event_handler = (dpc, eventName, eventData) => {
81
+ // take passed DPC since the window.depict object might have been overridden
82
+ if (eventName === "purchase") {
83
+ if (!$b1ff6e3aa9b3b030$var$ga4_purchase_guard(eventData))
84
+ // Inaccessible since ga4_purchase_guard will throw an error, TS doesn't know that
85
+ return;
86
+ dpc.sendPurchaseEvent(eventData);
87
+ return;
88
+ }
89
+ // All events below require strings as argument, accept numbers and automatically convert them
90
+ if (typeof eventData == "number") eventData = eventData.toString();
91
+ if (eventName === "addToCart") dpc.sendAddToCartEvent({
92
+ product_id: $b1ff6e3aa9b3b030$var$string_guard(eventName, eventData)
93
+ });else if (eventName === "setMarket") dpc.setMarket($b1ff6e3aa9b3b030$var$string_guard(eventName, eventData));else if (eventName === "setProductId") dpc.setProductId($b1ff6e3aa9b3b030$var$string_guard(eventName, eventData));else if (eventName === "setUserId") dpc.setUserId($b1ff6e3aa9b3b030$var$nullable_undefined_string_guard(eventName, eventData));else if (eventName === "setSessionId") dpc.setSessionId($b1ff6e3aa9b3b030$var$nullable_undefined_string_guard(eventName, eventData));
94
+ };
95
+ const $b1ff6e3aa9b3b030$export$7a1bb429756ced60 = dpc => {
96
+ const dpq = window.dpq ||= $b1ff6e3aa9b3b030$var$create_dpq();
97
+ const depict = window.depict ||= {};
98
+ depict.dpq = dpq; // refrencing dpq into the depict object
99
+ depict.eventHistory = dpq.queue; // making this more visible in the depict object
100
+ depict.dpc = dpc;
101
+ // Hook up the depict queue to the PerformanceClient
102
+ if (!dpq.send_event) {
103
+ dpq.send_event = (eventName, eventData) => {
104
+ dpq.queue.push([eventName, eventData]); // push to queue as usual to keep history
105
+ $b1ff6e3aa9b3b030$var$event_handler(dpc, eventName, eventData); // hand-over to the PerformanceClient
106
+ };
107
+ // Handle any events queued before this code ran.
108
+ dpq.queue.forEach(([eventName, eventData]) => {
109
+ $b1ff6e3aa9b3b030$var$event_handler(dpc, eventName, eventData);
110
+ });
111
+ }
112
+ };
113
+ class $fe563348936ab010$export$820526014323765f {
114
+ auto_observe = true;
115
+ /** Latest product_id found in the DOM. Used in a2c & pageview events on PDPs. */ /** The value of location.pathname when latest_product_id was set */ /** Latest session_id set on the client. Used for all events. */ /** Latest user_id found in the DOM. Used for all events. */ /** if we've sent a page view event for the current href */
116
+ #has_sent_pageview_for_current_href = false;
117
+ constructor(merchant, options) {
118
+ // Make sure we do not construct dpc more than once.
119
+ if (window.depict?.dpc instanceof $fe563348936ab010$export$820526014323765f) {
120
+ (0, $lbGMC$dlog)("DPC already initialized. Returning existing instance.");
121
+ return window.depict.dpc;
49
122
  }
50
- },
51
- w = t => {
52
- const e = window.dpq ||= function () {
53
- const t = function () {
54
- const e = arguments;
55
- t.send_event ? t.send_event.apply(this, e) : t.queue.push(e);
56
- };
57
- return t.queue = [], t;
58
- }(),
59
- i = window.depict ||= {};
60
- i.dpq = e, i.eventHistory = e.queue, i.dpc = t, e.send_event || (e.send_event = (i, s) => {
61
- e.queue.push([i, s]), b(t, i, s);
62
- }, e.queue.forEach(([e, i]) => {
63
- b(t, e, i);
123
+ (0, $b1ff6e3aa9b3b030$export$7a1bb429756ced60)(this);
124
+ this.merchant = merchant;
125
+ this.#parseOptions(options);
126
+ this.#resetSentPageViewForCurrentHref();
127
+ if (this.auto_observe) {
128
+ this.observeRecommendations();
129
+ this.observePDPA2CButton();
130
+ }
131
+ }
132
+ #resetSentPageViewForCurrentHref() {
133
+ let url = location.href;
134
+ (0, $lbGMC$instant_exec_on_suspect_history_change).add((0, $lbGMC$catchify)(() => {
135
+ const {
136
+ href: new_href
137
+ } = location;
138
+ if (url !== new_href) {
139
+ this.#has_sent_pageview_for_current_href = false;
140
+ url = new_href;
141
+ }
64
142
  }));
65
- };
66
- var R = _("has_sent_pageview_for_current_href"),
67
- E = _("resetSentPageViewForCurrentHref"),
68
- P = _("parseOptions"),
69
- S = _("getContextData"),
70
- C = _("sendTrackingEvent"),
71
- q = _("getProductId");
72
- class I {
73
- constructor(t, i) {
74
- if (Object.defineProperty(this, q, {
75
- value: N
76
- }), Object.defineProperty(this, C, {
77
- value: T
78
- }), Object.defineProperty(this, S, {
79
- value: j
80
- }), Object.defineProperty(this, P, {
81
- value: L
82
- }), Object.defineProperty(this, E, {
83
- value: A
84
- }), this.merchant = void 0, this.auto_observe = !0, this.market = void 0, this.latest_product_id = void 0, this.latest_product_id_path = void 0, this.latest_session_id = void 0, this.latest_user_id = void 0, Object.defineProperty(this, R, {
85
- writable: !0,
86
- value: !1
87
- }), this.tracked_elements = new WeakSet(), window.depict?.dpc instanceof I) return e("DPC already initialized. Returning existing instance."), window.depict.dpc;
88
- w(this), this.merchant = t, h(this, P)[P](i), h(this, E)[E](), this.auto_observe && (this.observeRecommendations(), this.observePDPA2CButton());
89
143
  }
90
- checkIfTracked(t) {
91
- return this.tracked_elements.has(t) ? (e("Already tracked", t), !0) : (this.tracked_elements.add(t), !1);
144
+ #parseOptions(options) {
145
+ if (options !== undefined) {
146
+ if (options.auto_observe !== undefined) this.auto_observe = options.auto_observe;
147
+ if (options.market !== undefined) this.market = options.market;
148
+ }
149
+ }
150
+ #getContextData() {
151
+ const data = {
152
+ market: this.market,
153
+ tenant: this.merchant,
154
+ user_id: this.latest_user_id,
155
+ session_id: this.latest_session_id
156
+ };
157
+ return {
158
+ ...(0, $lbGMC$get_tracking_event_context_data)(),
159
+ ...data
160
+ };
92
161
  }
93
- trackProductCard(t, e, s) {
94
- if (this.checkIfTracked(t)) return;
95
- switch (s) {
96
- case m.Recommendation:
97
- case m.SnakeRecommendation:
98
- t.addEventListener("click", () => this.sendRecommendationClickEvent(e)), i({
99
- element: t,
162
+ tracked_elements = new WeakSet();
163
+ /**
164
+ * Make sure the tracking is not set up twice on the same element.
165
+ */
166
+ checkIfTracked(element) {
167
+ if (this.tracked_elements.has(element)) {
168
+ (0, $lbGMC$dlog)("Already tracked", element);
169
+ return true;
170
+ }
171
+ this.tracked_elements.add(element);
172
+ return false;
173
+ }
174
+ /**
175
+ * Adds click and impression tracking to a product card
176
+ */
177
+ trackProductCard(card, tracking_id, type) {
178
+ if (this.checkIfTracked(card)) return;
179
+ switch (type) {
180
+ case (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).Recommendation:
181
+ case (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).SnakeRecommendation:
182
+ card.addEventListener("click", () => this.sendRecommendationClickEvent(tracking_id));
183
+ (0, $lbGMC$impression_track_element)({
184
+ element: card,
100
185
  data: {
101
- recommendation_id: e,
102
- ...h(this, S)[S]()
186
+ recommendation_id: tracking_id,
187
+ ...this.#getContextData()
103
188
  }
104
189
  });
105
190
  break;
106
- case m.SearchResult:
107
- t.addEventListener("click", () => this.sendSearchResultClickEvent(e)), i({
108
- element: t,
191
+ case (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).SearchResult:
192
+ card.addEventListener("click", () => this.sendSearchResultClickEvent(tracking_id));
193
+ (0, $lbGMC$impression_track_element)({
194
+ element: card,
109
195
  data: {
110
- search_result_id: e,
111
- ...h(this, S)[S]()
196
+ search_result_id: tracking_id,
197
+ ...this.#getContextData()
112
198
  }
113
199
  });
114
200
  break;
115
- case m.ProductListingResult:
116
- t.addEventListener("click", () => this.sendProductListingResultClickEvent(e)), i({
117
- element: t,
201
+ case (0, $ba8dc1a552d27224$export$8ae64f3d447c14f7).ProductListingResult:
202
+ card.addEventListener("click", () => this.sendProductListingResultClickEvent(tracking_id));
203
+ (0, $lbGMC$impression_track_element)({
204
+ element: card,
118
205
  data: {
119
- product_listing_result_id: e,
120
- ...h(this, S)[S]()
206
+ product_listing_result_id: tracking_id,
207
+ ...this.#getContextData()
121
208
  }
122
209
  });
210
+ break;
123
211
  }
124
- const r = t.querySelectorAll(f);
125
- for (const e of r) this.trackAddToCartButton(e, t);
212
+ // If the card has optional a2c buttons, we track those as well.
213
+ const a2c_buttons = card.querySelectorAll((0, $f956499541bad8b0$export$6f11e212918dbb31));
214
+ for (const a2c_button of a2c_buttons) this.trackAddToCartButton(a2c_button, card);
126
215
  }
127
- trackAddToCartButton(t, e) {
128
- this.checkIfTracked(t) || t.addEventListener("click", () => {
129
- if (!e) {
130
- const e = h(this, q)[q]();
131
- return e ? void this.sendAddToCartEvent({
132
- product_id: e
133
- }) : void s("No product ID set for add-to-cart click outside of product card", t);
216
+ /**
217
+ * Adds tracking to an Add to cart button.
218
+ */
219
+ trackAddToCartButton(button, productCard) {
220
+ if (this.checkIfTracked(button)) return;
221
+ button.addEventListener("click", () => {
222
+ if (!productCard) {
223
+ // If the button is not nested in a product card, it's a PDP button. Product ID set on the page is required.
224
+ const product_id = this.#getProductId();
225
+ if (!product_id) {
226
+ (0, $lbGMC$derror)("No product ID set for add-to-cart click outside of product card", button);
227
+ return;
228
+ }
229
+ this.sendAddToCartEvent({
230
+ product_id: product_id
231
+ });
232
+ return;
134
233
  }
135
- for (const [t, i] of Object.entries(k)) {
136
- const s = e.getAttribute(t);
137
- if (!s) continue;
138
- const r = {
139
- [v[i]]: s
234
+ // If the button is nested in a product card, we need to find the tracking ID. It can set in one of several attributes.
235
+ for (const [attribute, type] of Object.entries((0, $f956499541bad8b0$export$79f7900b4eea40cf))) {
236
+ const tracking_id = productCard.getAttribute(attribute);
237
+ if (!tracking_id) continue;
238
+ const tracking_data = {
239
+ [(0, $ba8dc1a552d27224$export$7c03f17546479d1d)[type]]: tracking_id
140
240
  };
141
- return void this.sendAddToCartEvent(r);
241
+ this.sendAddToCartEvent(tracking_data);
242
+ return;
142
243
  }
143
- s("Could not find tracking data for add-to-cart click on button", t);
244
+ (0, $lbGMC$derror)("Could not find tracking data for add-to-cart click on button", button);
144
245
  });
145
246
  }
146
- sendAddToCartEvent(t) {
147
- h(this, C)[C]({
247
+ /**
248
+ * Sends a tracking event to depict
249
+ */
250
+ #sendTrackingEvent(payload) {
251
+ // Todo. check consent. For some events, delay until consent decision, with possible timeout.
252
+ // For others, decide immediately based on current consent. (We shouldn't track a long history of events from before the user consented)
253
+ // If no consent, don't send at all, or send anonymous? I think the latter is valid, since no ID in cookie or localStorage.
254
+ (0, $lbGMC$send_depict_unfiltered)([{
255
+ ...payload,
256
+ ...this.#getContextData()
257
+ }], true);
258
+ }
259
+ /**
260
+ * Sends an Add to Cart Event to depict
261
+ * If the add to cart unmistakably happened due to a click on a product card shown because of Depict (for example the add to cart button on a product card), set recommendation_id / search_result_id / product_listing_result_id (whatever applicable). If it happened on a PDP, set product_id instead.
262
+ */
263
+ sendAddToCartEvent(data) {
264
+ this.#sendTrackingEvent({
148
265
  type: "add_to_cart",
149
- ...t
266
+ ...data
150
267
  });
151
268
  }
152
- sendRecommendationImpressionEvent(t) {
153
- h(this, C)[C]({
269
+ /**
270
+ * Sends a recommendation impression event to Depict
271
+ */
272
+ sendRecommendationImpressionEvent(recommendation_id) {
273
+ this.#sendTrackingEvent({
154
274
  type: "element_visible",
155
- recommendation_id: t
275
+ recommendation_id: recommendation_id
156
276
  });
157
277
  }
158
- sendSearchResultImpressionEvent(t) {
159
- h(this, C)[C]({
278
+ /**
279
+ * Sends a search result impression event to Depict
280
+ */
281
+ sendSearchResultImpressionEvent(search_result_id) {
282
+ this.#sendTrackingEvent({
160
283
  type: "element_visible",
161
- search_result_id: t
284
+ search_result_id: search_result_id
162
285
  });
163
286
  }
164
- sendProductListingResultImpressionEvent(t) {
165
- h(this, C)[C]({
287
+ /**
288
+ * Sends a product listing result impression event to Depict
289
+ */
290
+ sendProductListingResultImpressionEvent(product_listing_result_id) {
291
+ this.#sendTrackingEvent({
166
292
  type: "element_visible",
167
- product_listing_result_id: t
293
+ product_listing_result_id: product_listing_result_id
168
294
  });
169
295
  }
170
- sendRecommendationClickEvent(t) {
171
- r({
172
- recommendation_id: t,
173
- ...h(this, S)[S]()
296
+ /**
297
+ * Sends a recommendation click event to Depict
298
+ */
299
+ sendRecommendationClickEvent(recommendation_id) {
300
+ (0, $lbGMC$depict_click)({
301
+ recommendation_id: recommendation_id,
302
+ ...this.#getContextData()
174
303
  });
175
304
  }
176
- sendSearchResultClickEvent(t) {
177
- r({
178
- search_result_id: t,
179
- ...h(this, S)[S]()
305
+ /**
306
+ * Sends a search result click event to Depict
307
+ */
308
+ sendSearchResultClickEvent(search_result_id) {
309
+ (0, $lbGMC$depict_click)({
310
+ search_result_id: search_result_id,
311
+ ...this.#getContextData()
180
312
  });
181
313
  }
182
- sendProductListingResultClickEvent(t) {
183
- r({
184
- product_listing_result_id: t,
185
- ...h(this, S)[S]()
314
+ /**
315
+ * Sends a product listing result click event to Depict
316
+ */
317
+ sendProductListingResultClickEvent(product_listing_result_id) {
318
+ (0, $lbGMC$depict_click)({
319
+ product_listing_result_id: product_listing_result_id,
320
+ ...this.#getContextData()
186
321
  });
187
322
  }
188
- sendPurchaseEvent(t) {
323
+ /**
324
+ * Sends a Purchase Event to Depict
325
+ */
326
+ sendPurchaseEvent(data) {
189
327
  if (!this.market) throw Error("Market not set");
190
328
  const {
191
- dsid: e,
192
- user_id: i,
193
- session_id: s
194
- } = h(this, S)[S](),
195
- r = t.items.map(t => ({
196
- sku: t.item_id,
197
- price: t.price,
198
- quantity: t.quantity
199
- })),
200
- o = {
201
- events: [{
202
- event_type: "purchase",
203
- dsid: e,
204
- user_id: i,
205
- session_id: s,
206
- transaction_id: t.transaction_id,
207
- currency: t.currency,
208
- products: r
209
- }],
210
- tenant: this.merchant,
211
- market: this.market
212
- };
213
- n(process.env.BASE_URL + "/v2/create-events", o);
329
+ dsid: dsid,
330
+ user_id: user_id,
331
+ session_id: session_id
332
+ } = this.#getContextData();
333
+ const items = data.items.map(item => ({
334
+ sku: item.item_id,
335
+ price: item.price,
336
+ quantity: item.quantity
337
+ }));
338
+ const request = {
339
+ events: [{
340
+ event_type: "purchase",
341
+ dsid: dsid,
342
+ user_id: user_id,
343
+ session_id: session_id,
344
+ transaction_id: data.transaction_id,
345
+ currency: data.currency,
346
+ // This "At least one item" interface is too cumbersome to expose to end users, they get a plain array typing instead.
347
+ products: items
348
+ }],
349
+ tenant: this.merchant,
350
+ market: this.market
351
+ };
352
+ (0, $lbGMC$foolproof_send_beacon)((0, $lbGMC$base_url) + "/v2/create-events", request);
353
+ }
354
+ /**
355
+ * Update the market used for tracking events
356
+ */
357
+ setMarket(market) {
358
+ this.market = market;
214
359
  }
215
- setMarket(t) {
216
- this.market = t;
360
+ /**
361
+ * Update the product_id used for add-to-cart and pageview events on PDPs.
362
+ */
363
+ setProductId(product_id) {
364
+ (0, $lbGMC$dlog)("Setting product id", product_id);
365
+ this.latest_product_id = product_id;
366
+ this.latest_product_id_path = location.pathname;
367
+ if (!this.#has_sent_pageview_for_current_href) {
368
+ this.#sendTrackingEvent({
369
+ type: "page_view",
370
+ product_id: product_id
371
+ });
372
+ this.#has_sent_pageview_for_current_href = true;
373
+ }
217
374
  }
218
- setProductId(t) {
219
- e("Setting product id", t), this.latest_product_id = t, this.latest_product_id_path = location.pathname, h(this, R)[R] || (h(this, C)[C]({
220
- type: "page_view",
221
- product_id: t
222
- }), h(this, R)[R] = !0);
375
+ #getProductId() {
376
+ if (this.latest_product_id_path !== location.pathname) return undefined;
377
+ return this.latest_product_id;
223
378
  }
224
- setUserId(t) {
225
- e("Setting user id", t), this.latest_user_id = t || void 0;
379
+ /**
380
+ * Set the user_id for logged in users.
381
+ *
382
+ * https://docs.depict.ai/docs/performance-client#informing-depict-of-logged-in-users
383
+ */
384
+ setUserId(user_id) {
385
+ (0, $lbGMC$dlog)("Setting user id", user_id);
386
+ this.latest_user_id = user_id || undefined;
226
387
  }
227
- setSessionId(t) {
228
- e("Setting session id", t), this.latest_session_id = t || void 0;
388
+ /**
389
+ * Set the current session_id.
390
+ *
391
+ * https://docs.depict.ai/docs/performance-client#browser-sessions
392
+ */
393
+ setSessionId(session_id) {
394
+ (0, $lbGMC$dlog)("Setting session id", session_id);
395
+ this.latest_session_id = session_id || undefined;
229
396
  }
397
+ /**
398
+ * Observe the DOM for recommendations to track
399
+ */
230
400
  observeRecommendations() {
231
- for (const [t, i] of Object.entries(k)) {
232
- const r = `[${t}]`;
233
- e("Starting to observe product cards with selector", r), o.onexists(r, ({
234
- element: e
401
+ for (const [attribute, type] of Object.entries((0, $f956499541bad8b0$export$79f7900b4eea40cf))) {
402
+ const selector = `[${attribute}]`;
403
+ (0, $lbGMC$dlog)("Starting to observe product cards with selector", selector);
404
+ (0, $lbGMC$observer).onexists(selector, ({
405
+ element: element
235
406
  }) => {
236
- const n = e.getAttribute(t);
237
- n ? this.trackProductCard(e, n, i) : s(`No tracking id found on element with attribute ${r}`, e);
407
+ const tracking_id = element.getAttribute(attribute);
408
+ if (!tracking_id) {
409
+ (0, $lbGMC$derror)(`No tracking id found on element with attribute ${selector}`, element);
410
+ return;
411
+ }
412
+ this.trackProductCard(element, tracking_id, type);
238
413
  });
239
414
  }
240
415
  }
416
+ /**
417
+ * Observe the dom for add to cart buttons. Used on product detail pages.
418
+ */
241
419
  observePDPA2CButton() {
242
- o.onexists(f, ({
243
- element: t
420
+ const handler = ({
421
+ element: element
244
422
  }) => {
245
- const i = Object.keys(k).map(t => `[${t}]`).join(","),
246
- s = t.closest(i) || void 0;
247
- e(`Found ${s ? "product card" : "pdp"} add-to-cart button`, t), this.trackAddToCartButton(t, s);
248
- });
423
+ const depict_card_selector = Object.keys((0, $f956499541bad8b0$export$79f7900b4eea40cf)).map(attribute => `[${attribute}]`).join(",");
424
+ // it's an add to cart button on a product card that possibly was added to the product card at a later point, if parent_depict_card is defined
425
+ const parent_depict_card = element.closest(depict_card_selector) || undefined;
426
+ (0, $lbGMC$dlog)(`Found ${parent_depict_card ? "product card" : "pdp"} add-to-cart button`, element);
427
+ this.trackAddToCartButton(element, parent_depict_card);
428
+ };
429
+ (0, $lbGMC$observer).onexists((0, $f956499541bad8b0$export$6f11e212918dbb31), handler);
249
430
  }
250
431
  }
251
- function A() {
252
- let t = location.href;
253
- a.add(d(() => {
254
- const {
255
- href: e
256
- } = location;
257
- t !== e && (h(this, R)[R] = !1, t = e);
258
- }));
259
- }
260
- function L(t) {
261
- void 0 !== t && (void 0 !== t.auto_observe && (this.auto_observe = t.auto_observe), void 0 !== t.market && (this.market = t.market));
262
- }
263
- function j() {
264
- const t = {
265
- market: this.market,
266
- tenant: this.merchant,
267
- user_id: this.latest_user_id,
268
- session_id: this.latest_session_id
269
- };
270
- return {
271
- ...c(),
272
- ...t
273
- };
274
- }
275
- function T(t) {
276
- u([{
277
- ...t,
278
- ...h(this, S)[S]()
279
- }], !0);
280
- }
281
- function N() {
282
- if (this.latest_product_id_path === location.pathname) return this.latest_product_id;
283
- }
284
- t("dpc", l);
285
- export { I as DPC, l as version };
432
+ (0, $lbGMC$add_to_version_info)("dpc", (0, $3b068891cefe570d$export$83d89fbfd8236492));
433
+ export { $fe563348936ab010$export$820526014323765f as DPC, $3b068891cefe570d$export$83d89fbfd8236492 as version };