@depict-ai/dpc 1.0.30 → 1.0.32

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 ADDED
@@ -0,0 +1,300 @@
1
+ var e = require("@depict-ai/utilishared/ES10"),
2
+ t = require("@babel/runtime/helpers/classPrivateFieldLooseBase"),
3
+ i = require("@babel/runtime/helpers/classPrivateFieldLooseKey");
4
+ 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
11
+ });
12
+ }
13
+ function s(e) {
14
+ return e && e.__esModule ? e.default : e;
15
+ }
16
+ r(module.exports, "DPC", () => y), r(module.exports, "version", () => n);
17
+ /*! 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
34
+ };
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);
60
+ }
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
+ }
104
+ checkIfTracked(t) {
105
+ return this.tracked_elements.has(t) ? ((0, e.dlog)("Already tracked", t), !0) : (this.tracked_elements.add(t), !1);
106
+ }
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,
114
+ data: {
115
+ recommendation_id: r,
116
+ ...s(t)(this, k)[k]()
117
+ }
118
+ });
119
+ break;
120
+ case o.SearchResult:
121
+ i.addEventListener("click", () => this.sendSearchResultClickEvent(r)), (0, e.impression_track_element)({
122
+ element: i,
123
+ data: {
124
+ search_result_id: r,
125
+ ...s(t)(this, k)[k]()
126
+ }
127
+ });
128
+ break;
129
+ case o.ProductListingResult:
130
+ i.addEventListener("click", () => this.sendProductListingResultClickEvent(r)), (0, e.impression_track_element)({
131
+ element: i,
132
+ data: {
133
+ product_listing_result_id: r,
134
+ ...s(t)(this, k)[k]()
135
+ }
136
+ });
137
+ }
138
+ const d = i.querySelectorAll(c);
139
+ for (const e of d) this.trackAddToCartButton(e, i);
140
+ }
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);
148
+ }
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
154
+ };
155
+ return void this.sendAddToCartEvent(s);
156
+ }
157
+ (0, e.derror)("Could not find tracking data for add-to-cart click on button", i);
158
+ });
159
+ }
160
+ sendAddToCartEvent(e) {
161
+ s(t)(this, g)[g]({
162
+ type: "add_to_cart",
163
+ ...e
164
+ });
165
+ }
166
+ sendRecommendationImpressionEvent(e) {
167
+ s(t)(this, g)[g]({
168
+ type: "element_visible",
169
+ recommendation_id: e
170
+ });
171
+ }
172
+ sendSearchResultImpressionEvent(e) {
173
+ s(t)(this, g)[g]({
174
+ type: "element_visible",
175
+ search_result_id: e
176
+ });
177
+ }
178
+ sendProductListingResultImpressionEvent(e) {
179
+ s(t)(this, g)[g]({
180
+ type: "element_visible",
181
+ product_listing_result_id: e
182
+ });
183
+ }
184
+ sendRecommendationClickEvent(i) {
185
+ (0, e.depict_click)({
186
+ recommendation_id: i,
187
+ ...s(t)(this, k)[k]()
188
+ });
189
+ }
190
+ sendSearchResultClickEvent(i) {
191
+ (0, e.depict_click)({
192
+ search_result_id: i,
193
+ ...s(t)(this, k)[k]()
194
+ });
195
+ }
196
+ sendProductListingResultClickEvent(i) {
197
+ (0, e.depict_click)({
198
+ product_listing_result_id: i,
199
+ ...s(t)(this, k)[k]()
200
+ });
201
+ }
202
+ sendPurchaseEvent(i) {
203
+ if (!this.market) throw Error("Market not set");
204
+ 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);
228
+ }
229
+ setMarket(e) {
230
+ this.market = e;
231
+ }
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);
237
+ }
238
+ setUserId(t) {
239
+ (0, e.dlog)("Setting user id", t), this.latest_user_id = t || void 0;
240
+ }
241
+ setSessionId(t) {
242
+ (0, e.dlog)("Setting session id", t), this.latest_session_id = t || void 0;
243
+ }
244
+ 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 => {
248
+ 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);
253
+ });
254
+ }
255
+ }
256
+ observePDPA2CButton() {
257
+ e.observer.onexists(c, t => {
258
+ 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
+ });
265
+ }
266
+ }
267
+ function w() {
268
+ let i = location.href;
269
+ e.instant_exec_on_suspect_history_change.add((0, e.catchify)(() => {
270
+ const {
271
+ href: e
272
+ } = location;
273
+ i !== e && (s(t)(this, m)[m] = !1, i = e);
274
+ }));
275
+ }
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));
278
+ }
279
+ function P() {
280
+ const t = {
281
+ market: this.market,
282
+ tenant: this.merchant,
283
+ user_id: this.latest_user_id,
284
+ session_id: this.latest_session_id
285
+ };
286
+ return {
287
+ ...(0, e.get_tracking_event_context_data)(),
288
+ ...t
289
+ };
290
+ }
291
+ function E(i) {
292
+ (0, e.send_depict_unfiltered)([{
293
+ ...i,
294
+ ...s(t)(this, k)[k]()
295
+ }], !0);
296
+ }
297
+ function S() {
298
+ if (this.latest_product_id_path === location.pathname) return this.latest_product_id;
299
+ }
300
+ (0, e.add_to_version_info)("dpc", n);
@@ -0,0 +1,153 @@
1
+ export const version = "1.0.32";
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
+ }