@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/ES10/index.cjs CHANGED
@@ -1,300 +1,478 @@
1
- var e = require("@depict-ai/utilishared/ES10"),
2
- t = require("@babel/runtime/helpers/classPrivateFieldLooseBase"),
3
- i = require("@babel/runtime/helpers/classPrivateFieldLooseKey");
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");
4
5
  require("../multi_import_deduplicator.cjs").register_es_version("performance-client", "ES10");
5
- function r(e, t, i, r) {
6
- Object.defineProperty(e, t, {
7
- get: i,
8
- set: r,
9
- enumerable: !0,
10
- configurable: !0
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
11
12
  });
12
13
  }
13
- function s(e) {
14
- return e && e.__esModule ? e.default : e;
14
+ function $parcel$interopDefault(a) {
15
+ return a && a.__esModule ? a.default : a;
15
16
  }
16
- r(module.exports, "DPC", () => y), r(module.exports, "version", () => n);
17
+ $parcel$export(module.exports, "DPC", () => $89ae37e3a26189b4$export$820526014323765f);
18
+ $parcel$export(module.exports, "version", () => $fcc8287091fb489d$export$83d89fbfd8236492);
17
19
  /*! 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
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);
34
62
  };
35
- const h = (e, t) => {
36
- if ("number" == typeof t && !isNaN(t)) return t.toString();
37
- if ("string" != typeof t) throw new Error("".concat(e, " event requires a string as an argument"));
38
- return t;
39
- },
40
- l = (e, t) => null == t ? t : h(e, t),
41
- _ = (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(l(t, i)) : "setSessionId" === t && e.setSessionId(l(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);
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();
60
176
  }
61
- },
62
- p = e => {
63
- var t, i;
64
- const r = (t = window).dpq || (t.dpq = function () {
65
- const e = function () {
66
- const t = arguments;
67
- e.send_event ? e.send_event.apply(this, t) : e.queue.push(t);
68
- };
69
- return e.queue = [], e;
70
- }()),
71
- s = (i = window).depict || (i.depict = {});
72
- s.dpq = r, s.eventHistory = r.queue, s.dpc = e, r.send_event || (r.send_event = (t, i) => {
73
- r.queue.push([t, i]), _(e, t, i);
74
- }, r.queue.forEach(t => {
75
- let [i, r] = t;
76
- _(e, i, r);
77
- }));
78
- };
79
- var m = s(i)("has_sent_pageview_for_current_href"),
80
- v = s(i)("resetSentPageViewForCurrentHref"),
81
- f = s(i)("parseOptions"),
82
- k = s(i)("getContextData"),
83
- g = s(i)("sendTrackingEvent"),
84
- b = s(i)("getProductId");
85
- class y {
86
- constructor(i, r) {
87
- var n;
88
- if (Object.defineProperty(this, b, {
89
- value: S
90
- }), Object.defineProperty(this, g, {
91
- value: E
92
- }), Object.defineProperty(this, k, {
93
- value: P
94
- }), Object.defineProperty(this, f, {
95
- value: R
96
- }), Object.defineProperty(this, v, {
97
- value: w
98
- }), 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, m, {
99
- writable: !0,
100
- value: !1
101
- }), this.tracked_elements = new WeakSet(), (null === (n = window.depict) || void 0 === n ? void 0 : n.dpc) instanceof y) return (0, e.dlog)("DPC already initialized. Returning existing instance."), window.depict.dpc;
102
- p(this), this.merchant = i, s(t)(this, f)[f](r), s(t)(this, v)[v](), this.auto_observe && (this.observeRecommendations(), this.observePDPA2CButton());
103
177
  }
104
- checkIfTracked(t) {
105
- return this.tracked_elements.has(t) ? ((0, e.dlog)("Already tracked", t), !0) : (this.tracked_elements.add(t), !1);
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;
106
188
  }
107
- trackProductCard(i, r, n) {
108
- if (this.checkIfTracked(i)) return;
109
- switch (n) {
110
- case o.Recommendation:
111
- case o.SnakeRecommendation:
112
- i.addEventListener("click", () => this.sendRecommendationClickEvent(r)), (0, e.impression_track_element)({
113
- element: i,
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,
114
200
  data: {
115
- recommendation_id: r,
116
- ...s(t)(this, k)[k]()
201
+ recommendation_id: tracking_id,
202
+ ...$89ae37e3a26189b4$var$_classPrivateMethodGet(this, $89ae37e3a26189b4$var$_getContextData, $89ae37e3a26189b4$var$_getContextData2).call(this)
117
203
  }
118
204
  });
119
205
  break;
120
- case o.SearchResult:
121
- i.addEventListener("click", () => this.sendSearchResultClickEvent(r)), (0, e.impression_track_element)({
122
- element: i,
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,
123
210
  data: {
124
- search_result_id: r,
125
- ...s(t)(this, k)[k]()
211
+ search_result_id: tracking_id,
212
+ ...$89ae37e3a26189b4$var$_classPrivateMethodGet(this, $89ae37e3a26189b4$var$_getContextData, $89ae37e3a26189b4$var$_getContextData2).call(this)
126
213
  }
127
214
  });
128
215
  break;
129
- case o.ProductListingResult:
130
- i.addEventListener("click", () => this.sendProductListingResultClickEvent(r)), (0, e.impression_track_element)({
131
- element: i,
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,
132
220
  data: {
133
- product_listing_result_id: r,
134
- ...s(t)(this, k)[k]()
221
+ product_listing_result_id: tracking_id,
222
+ ...$89ae37e3a26189b4$var$_classPrivateMethodGet(this, $89ae37e3a26189b4$var$_getContextData, $89ae37e3a26189b4$var$_getContextData2).call(this)
135
223
  }
136
224
  });
225
+ break;
137
226
  }
138
- const d = i.querySelectorAll(c);
139
- for (const e of d) this.trackAddToCartButton(e, i);
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);
140
230
  }
141
- trackAddToCartButton(i, r) {
142
- this.checkIfTracked(i) || i.addEventListener("click", () => {
143
- if (!r) {
144
- const r = s(t)(this, b)[b]();
145
- return r ? void this.sendAddToCartEvent({
146
- product_id: r
147
- }) : void (0, e.derror)("No product ID set for add-to-cart click outside of product card", i);
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;
148
248
  }
149
- for (const [e, t] of Object.entries(u)) {
150
- const i = r.getAttribute(e);
151
- if (!i) continue;
152
- const s = {
153
- [a[t]]: i
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
154
255
  };
155
- return void this.sendAddToCartEvent(s);
256
+ this.sendAddToCartEvent(tracking_data);
257
+ return;
156
258
  }
157
- (0, e.derror)("Could not find tracking data for add-to-cart click on button", i);
259
+ (0, $9AWcE$depictaiutilishared.derror)("Could not find tracking data for add-to-cart click on button", button);
158
260
  });
159
261
  }
160
- sendAddToCartEvent(e) {
161
- s(t)(this, g)[g]({
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, {
162
270
  type: "add_to_cart",
163
- ...e
271
+ ...data
164
272
  });
165
273
  }
166
- sendRecommendationImpressionEvent(e) {
167
- s(t)(this, g)[g]({
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, {
168
279
  type: "element_visible",
169
- recommendation_id: e
280
+ recommendation_id: recommendation_id
170
281
  });
171
282
  }
172
- sendSearchResultImpressionEvent(e) {
173
- s(t)(this, g)[g]({
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, {
174
288
  type: "element_visible",
175
- search_result_id: e
289
+ search_result_id: search_result_id
176
290
  });
177
291
  }
178
- sendProductListingResultImpressionEvent(e) {
179
- s(t)(this, g)[g]({
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, {
180
297
  type: "element_visible",
181
- product_listing_result_id: e
298
+ product_listing_result_id: product_listing_result_id
182
299
  });
183
300
  }
184
- sendRecommendationClickEvent(i) {
185
- (0, e.depict_click)({
186
- recommendation_id: i,
187
- ...s(t)(this, k)[k]()
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)
188
308
  });
189
309
  }
190
- sendSearchResultClickEvent(i) {
191
- (0, e.depict_click)({
192
- search_result_id: i,
193
- ...s(t)(this, k)[k]()
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)
194
317
  });
195
318
  }
196
- sendProductListingResultClickEvent(i) {
197
- (0, e.depict_click)({
198
- product_listing_result_id: i,
199
- ...s(t)(this, k)[k]()
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)
200
326
  });
201
327
  }
202
- sendPurchaseEvent(i) {
328
+ /**
329
+ * Sends a Purchase Event to Depict
330
+ */
331
+ sendPurchaseEvent(data) {
203
332
  if (!this.market) throw Error("Market not set");
204
333
  const {
205
- dsid: r,
206
- user_id: n,
207
- session_id: o
208
- } = s(t)(this, k)[k](),
209
- d = i.items.map(e => ({
210
- sku: e.item_id,
211
- price: e.price,
212
- quantity: e.quantity
213
- })),
214
- a = {
215
- events: [{
216
- event_type: "purchase",
217
- dsid: r,
218
- user_id: n,
219
- session_id: o,
220
- transaction_id: i.transaction_id,
221
- currency: i.currency,
222
- products: d
223
- }],
224
- tenant: this.merchant,
225
- market: this.market
226
- };
227
- (0, e.foolproof_send_beacon)(process.env.BASE_URL + "/v2/create-events", a);
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);
228
358
  }
229
- setMarket(e) {
230
- this.market = e;
359
+ /**
360
+ * Update the market used for tracking events
361
+ */
362
+ setMarket(market) {
363
+ this.market = market;
231
364
  }
232
- setProductId(i) {
233
- (0, e.dlog)("Setting product id", i), this.latest_product_id = i, this.latest_product_id_path = location.pathname, s(t)(this, m)[m] || (s(t)(this, g)[g]({
234
- type: "page_view",
235
- product_id: i
236
- }), s(t)(this, m)[m] = !0);
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
+ }
237
379
  }
238
- setUserId(t) {
239
- (0, e.dlog)("Setting user id", t), this.latest_user_id = t || void 0;
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;
240
388
  }
241
- setSessionId(t) {
242
- (0, e.dlog)("Setting session id", t), this.latest_session_id = t || void 0;
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;
243
397
  }
398
+ /**
399
+ * Observe the DOM for recommendations to track
400
+ */
244
401
  observeRecommendations() {
245
- for (const [t, i] of Object.entries(u)) {
246
- const r = "[".concat(t, "]");
247
- (0, e.dlog)("Starting to observe product cards with selector", r), e.observer.onexists(r, s => {
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 => {
248
406
  let {
249
- element: n
250
- } = s;
251
- const o = n.getAttribute(t);
252
- o ? this.trackProductCard(n, o, i) : (0, e.derror)("No tracking id found on element with attribute ".concat(r), n);
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);
253
415
  });
254
416
  }
255
417
  }
418
+ /**
419
+ * Observe the dom for add to cart buttons. Used on product detail pages.
420
+ */
256
421
  observePDPA2CButton() {
257
- e.observer.onexists(c, t => {
422
+ const handler = _ref2 => {
258
423
  let {
259
- element: i
260
- } = t;
261
- const r = Object.keys(u).map(e => "[".concat(e, "]")).join(","),
262
- s = i.closest(r) || void 0;
263
- (0, e.dlog)("Found ".concat(s ? "product card" : "pdp", " add-to-cart button"), i), this.trackAddToCartButton(i, s);
264
- });
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);
265
433
  }
266
434
  }
267
- function w() {
268
- let i = location.href;
269
- e.instant_exec_on_suspect_history_change.add((0, e.catchify)(() => {
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)(() => {
270
438
  const {
271
- href: e
439
+ href: new_href
272
440
  } = location;
273
- i !== e && (s(t)(this, m)[m] = !1, i = e);
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
+ }
274
445
  }));
275
446
  }
276
- function R(e) {
277
- void 0 !== e && (void 0 !== e.auto_observe && (this.auto_observe = e.auto_observe), void 0 !== e.market && (this.market = e.market));
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
+ }
278
452
  }
279
- function P() {
280
- const t = {
453
+ function $89ae37e3a26189b4$var$_getContextData2() {
454
+ const data = {
281
455
  market: this.market,
282
456
  tenant: this.merchant,
283
457
  user_id: this.latest_user_id,
284
458
  session_id: this.latest_session_id
285
459
  };
286
460
  return {
287
- ...(0, e.get_tracking_event_context_data)(),
288
- ...t
461
+ ...(0, $9AWcE$depictaiutilishared.get_tracking_event_context_data)(),
462
+ ...data
289
463
  };
290
464
  }
291
- function E(i) {
292
- (0, e.send_depict_unfiltered)([{
293
- ...i,
294
- ...s(t)(this, k)[k]()
295
- }], !0);
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);
296
473
  }
297
- function S() {
298
- if (this.latest_product_id_path === location.pathname) return this.latest_product_id;
474
+ function $89ae37e3a26189b4$var$_getProductId2() {
475
+ if (this.latest_product_id_path !== location.pathname) return undefined;
476
+ return this.latest_product_id;
299
477
  }
300
- (0, e.add_to_version_info)("dpc", n);
478
+ (0, $9AWcE$depictaiutilishared.add_to_version_info)("dpc", (0, $fcc8287091fb489d$export$83d89fbfd8236492));