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