@depict-ai/dpc 1.0.32 → 1.0.34

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