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