@depict-ai/dpc 1.0.11 → 1.0.12

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/README.md CHANGED
@@ -1,3 +1,3 @@
1
- # Depict Performance Client
2
-
3
- https://docs.depict.ai/docs/performance-client
1
+ # Depict Performance Client
2
+
3
+ https://docs.depict.ai/docs/performance-client
package/dist/index.d.ts CHANGED
@@ -1,152 +1,152 @@
1
- export const version = "1.0.11";
2
- enum ProductCardType {
3
- Recommendation = 0,
4
- SnakeRecommendation = 1,
5
- SearchResult = 2,
6
- ProductListingResult = 3
7
- }
8
- /**
9
- * The Depict Queue function used to register events to be sent to depict.
10
- */
11
- export type DepictQueue = {
12
- (eventName: string, ...args: unknown[]): void;
13
- send_event?: (eventName: string, ...args: unknown[]) => void;
14
- queue: [string, unknown][];
15
- };
16
- /**
17
- * Depict Object containing everything depict-related on the page.
18
- */
19
- export interface DepictObject {
20
- eventHistory: [string, unknown][];
21
- dpq: DepictQueue;
22
- dpc: DPC;
23
- }
24
- declare global {
25
- interface Window {
26
- /**
27
- * Lifted DPQ function for ease-of-use
28
- */
29
- dpq: DepictQueue;
30
- depict: DepictObject;
31
- }
32
- }
33
- /**
34
- * Purchase format used by GA4 when sending a purchase event.
35
- * See https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund
36
- */
37
- interface GA4Purchase {
38
- transaction_id: string;
39
- currency: string;
40
- items: GA4PurchaseItem[];
41
- [additional: string]: unknown;
42
- }
43
- /**
44
- * Product format used by GA4 when sending a purchase event.
45
- * See https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase_item
46
- */
47
- interface GA4PurchaseItem {
48
- item_id: string;
49
- price: number;
50
- quantity: number;
51
- [additional: string]: unknown;
52
- }
53
- interface Options {
54
- /**
55
- * The initial market. Should be specified if the market is known at the point of intializing DPC.
56
- */
57
- market?: string;
58
- /**
59
- * If we should observe for recommendations on creation. If false, you must call `trackRecommendationCard` manually.
60
- */
61
- auto_observe?: boolean;
62
- }
63
- export class DPC {
64
- #private;
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
- */
93
- sendAddToCartEvent(data: any): void;
94
- /**
95
- * Sends a recommendation impression event to Depict
96
- */
97
- sendRecommendationImpressionEvent(recommendation_id: string): void;
98
- /**
99
- * Sends a search result impression event to Depict
100
- */
101
- sendSearchResultImpressionEvent(search_result_id: string): void;
102
- /**
103
- * Sends a product listing result impression event to Depict
104
- */
105
- sendProductListingResultImpressionEvent(product_listing_result_id: string): void;
106
- /**
107
- * Sends a recommendation click event to Depict
108
- */
109
- sendRecommendationClickEvent(recommendation_id: string): void;
110
- /**
111
- * Sends a search result click event to Depict
112
- */
113
- sendSearchResultClickEvent(search_result_id: string): void;
114
- /**
115
- * Sends a product listing result click event to Depict
116
- */
117
- sendProductListingResultClickEvent(product_listing_result_id: string): void;
118
- /**
119
- * Sends a Purchase Event to Depict
120
- */
121
- sendPurchaseEvent(data: GA4Purchase): void;
122
- /**
123
- * Update the market used for tracking events
124
- */
125
- setMarket(market: string): void;
126
- /**
127
- * Update the product_id used for add-to-cart and pageview events on PDPs.
128
- */
129
- setProductId(product_id: string): void;
130
- /**
131
- * Set the user_id for logged in users.
132
- *
133
- * https://docs.depict.ai/docs/performance-client#informing-depict-of-logged-in-users
134
- */
135
- setUserId(user_id?: string | null): void;
136
- /**
137
- * Set the current session_id.
138
- *
139
- * https://docs.depict.ai/docs/performance-client#browser-sessions
140
- */
141
- setSessionId(session_id?: string | null): void;
142
- /**
143
- * Observe the DOM for recommendations to track
144
- */
145
- observeRecommendations(): void;
146
- /**
147
- * Observe the dom for add to cart buttons. Used on product detail pages.
148
- */
149
- observePDPA2CButton(): void;
150
- }
1
+ export const version = "1.0.12";
2
+ enum ProductCardType {
3
+ Recommendation = 0,
4
+ SnakeRecommendation = 1,
5
+ SearchResult = 2,
6
+ ProductListingResult = 3
7
+ }
8
+ /**
9
+ * The Depict Queue function used to register events to be sent to depict.
10
+ */
11
+ export type DepictQueue = {
12
+ (eventName: string, ...args: unknown[]): void;
13
+ send_event?: (eventName: string, ...args: unknown[]) => void;
14
+ queue: [string, unknown][];
15
+ };
16
+ /**
17
+ * Depict Object containing everything depict-related on the page.
18
+ */
19
+ export interface DepictObject {
20
+ eventHistory: [string, unknown][];
21
+ dpq: DepictQueue;
22
+ dpc: DPC;
23
+ }
24
+ declare global {
25
+ interface Window {
26
+ /**
27
+ * Lifted DPQ function for ease-of-use
28
+ */
29
+ dpq: DepictQueue;
30
+ depict: DepictObject;
31
+ }
32
+ }
33
+ /**
34
+ * Purchase format used by GA4 when sending a purchase event.
35
+ * See https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund
36
+ */
37
+ interface GA4Purchase {
38
+ transaction_id: string;
39
+ currency: string;
40
+ items: GA4PurchaseItem[];
41
+ [additional: string]: unknown;
42
+ }
43
+ /**
44
+ * Product format used by GA4 when sending a purchase event.
45
+ * See https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase_item
46
+ */
47
+ interface GA4PurchaseItem {
48
+ item_id: string;
49
+ price: number;
50
+ quantity: number;
51
+ [additional: string]: unknown;
52
+ }
53
+ interface Options {
54
+ /**
55
+ * The initial market. Should be specified if the market is known at the point of intializing DPC.
56
+ */
57
+ market?: string;
58
+ /**
59
+ * If we should observe for recommendations on creation. If false, you must call `trackRecommendationCard` manually.
60
+ */
61
+ auto_observe?: boolean;
62
+ }
63
+ export class DPC {
64
+ #private;
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
+ */
93
+ sendAddToCartEvent(data: any): void;
94
+ /**
95
+ * Sends a recommendation impression event to Depict
96
+ */
97
+ sendRecommendationImpressionEvent(recommendation_id: string): void;
98
+ /**
99
+ * Sends a search result impression event to Depict
100
+ */
101
+ sendSearchResultImpressionEvent(search_result_id: string): void;
102
+ /**
103
+ * Sends a product listing result impression event to Depict
104
+ */
105
+ sendProductListingResultImpressionEvent(product_listing_result_id: string): void;
106
+ /**
107
+ * Sends a recommendation click event to Depict
108
+ */
109
+ sendRecommendationClickEvent(recommendation_id: string): void;
110
+ /**
111
+ * Sends a search result click event to Depict
112
+ */
113
+ sendSearchResultClickEvent(search_result_id: string): void;
114
+ /**
115
+ * Sends a product listing result click event to Depict
116
+ */
117
+ sendProductListingResultClickEvent(product_listing_result_id: string): void;
118
+ /**
119
+ * Sends a Purchase Event to Depict
120
+ */
121
+ sendPurchaseEvent(data: GA4Purchase): void;
122
+ /**
123
+ * Update the market used for tracking events
124
+ */
125
+ setMarket(market: string): void;
126
+ /**
127
+ * Update the product_id used for add-to-cart and pageview events on PDPs.
128
+ */
129
+ setProductId(product_id: string): void;
130
+ /**
131
+ * Set the user_id for logged in users.
132
+ *
133
+ * https://docs.depict.ai/docs/performance-client#informing-depict-of-logged-in-users
134
+ */
135
+ setUserId(user_id?: string | null): void;
136
+ /**
137
+ * Set the current session_id.
138
+ *
139
+ * https://docs.depict.ai/docs/performance-client#browser-sessions
140
+ */
141
+ setSessionId(session_id?: string | null): void;
142
+ /**
143
+ * Observe the DOM for recommendations to track
144
+ */
145
+ observeRecommendations(): void;
146
+ /**
147
+ * Observe the dom for add to cart buttons. Used on product detail pages.
148
+ */
149
+ observePDPA2CButton(): void;
150
+ }
151
151
 
152
152
  //# sourceMappingURL=index.d.ts.map
package/dist/main.js CHANGED
@@ -1,4 +1,4 @@
1
1
  var t=require("@depict-ai/utilishared");function e(t,e,i,r){Object.defineProperty(t,e,{get:i,set:r,enumerable:!0,configurable:!0})}e(module.exports,"DPC",(()=>g)),e(module.exports,"version",(()=>i));
2
2
  /*! Depict PerformanceClient */
3
- const i="1.0.11";let r;var n;(n=r||(r={}))[n.Recommendation=0]="Recommendation",n[n.SnakeRecommendation=1]="SnakeRecommendation",n[n.SearchResult=2]="SearchResult",n[n.ProductListingResult=3]="ProductListingResult";const s={[r.Recommendation]:"recommendation_id",[r.SnakeRecommendation]:"recommendation_id",[r.SearchResult]:"search_result_id",[r.ProductListingResult]:"product_listing_result_id"},o=".depict-add-to-cart",d={"data-recommendation_id":r.SnakeRecommendation,"data-recommendation-id":r.Recommendation,"data-search-result-id":r.SearchResult,"data-product-listing-result-id":r.ProductListingResult};const a=(t,e)=>{if("number"==typeof e&&!isNaN(e))return e.toString();if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`);return e},c=(t,e)=>null==e?e:a(t,e),u=(t,e,i)=>{if("purchase"!==e)"number"==typeof i&&(i=i.toString()),"addToCart"===e?t.sendAddToCartEvent({product_id:a(e,i)}):"setMarket"===e?t.setMarket(a(e,i)):"setProductId"===e?t.setProductId(a(e,i)):"setUserId"===e?t.setUserId(c(e,i)):"setSessionId"===e&&t.setSessionId(c(e,i));else{if(!(t=>{if("object"!=typeof t||null===t)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(t.items))throw new Error("purchase event requires an items array of purchased products");if(!t.currency||"string"!=typeof t.currency)throw new Error("purchase event requires a currency string");if(!t.transaction_id||"string"!=typeof t.transaction_id&&"number"!=typeof t.transaction_id)throw new Error("purchase event requires a transaction_id string");t.transaction_id=t.transaction_id.toString();for(const e of t.items){if(""==e.item_id||isNaN(e.item_id)||"string"!=typeof e.item_id&&"number"!=typeof e.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(e.price))throw new Error("purchase item requires a unit price for the product purchased");if(!e.quantity||isNaN(e.quantity))throw new Error("purchase item requires a quantity of products purchased");e.item_id=e.item_id.toString()}return!0})(i))return;t.sendPurchaseEvent(i)}},h=t=>{const e=window.dpq||=function(){const t=function(){const e=arguments;t.send_event?t.send_event.apply(this,e):t.queue.push(e)};return t.queue=[],t}(),i=window.depict||={};i.dpq=e,i.eventHistory=e.queue,i.dpc=t,e.send_event||(e.send_event=(i,r)=>{e.queue.push([i,r]),u(t,i,r)},e.queue.forEach((([e,i])=>{u(t,e,i)})))};function _(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var l=0;function p(t){return"__private_"+l+++"_"+t}var m=p("parseOptions"),v=p("getContextData"),f=p("sendTrackingEvent"),k=p("getProductId");class g{constructor(e,i){if(Object.defineProperty(this,k,{value:R}),Object.defineProperty(this,f,{value:w}),Object.defineProperty(this,v,{value:b}),Object.defineProperty(this,m,{value:y}),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,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof g)return(0,t.dlog)("DPC already initialized. Returning existing instance."),window.depict.dpc;h(this),this.merchant=e,_(this,m)[m](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}checkIfTracked(e){return this.tracked_elements.has(e)?((0,t.dlog)("Already tracked",e),!0):(this.tracked_elements.add(e),!1)}trackProductCard(e,i,n){if(this.checkIfTracked(e))return;switch(n){case r.Recommendation:case r.SnakeRecommendation:e.addEventListener("click",(()=>this.sendRecommendationClickEvent(i))),(0,t.impression_track_element)({element:e,data:{recommendation_id:i,..._(this,v)[v]()}});break;case r.SearchResult:e.addEventListener("click",(()=>this.sendSearchResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{search_result_id:i,..._(this,v)[v]()}});break;case r.ProductListingResult:e.addEventListener("click",(()=>this.sendProductListingResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{product_listing_result_id:i,..._(this,v)[v]()}})}const s=e.querySelectorAll(o);for(const t of s)this.trackAddToCartButton(t,e)}trackAddToCartButton(e,i){this.checkIfTracked(e)||e.addEventListener("click",(()=>{if(!i){const i=_(this,k)[k]();return i?void this.sendAddToCartEvent({product_id:i}):void(0,t.derror)("No product ID set for add-to-cart click outside of product card",e)}for(const[t,e]of Object.entries(d)){const r=i.getAttribute(t);if(!r)continue;const n={[s[e]]:r};return void this.sendAddToCartEvent(n)}(0,t.derror)("Could not find tracking data for add-to-cart click on button",e)}))}sendAddToCartEvent(t){_(this,f)[f]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){_(this,f)[f]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){_(this,f)[f]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){_(this,f)[f]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(e){(0,t.depict_click)({recommendation_id:e,..._(this,v)[v]()})}sendSearchResultClickEvent(e){(0,t.depict_click)({search_result_id:e,..._(this,v)[v]()})}sendProductListingResultClickEvent(e){(0,t.depict_click)({product_listing_result_id:e,..._(this,v)[v]()})}sendPurchaseEvent(e){if(!this.market)throw Error("Market not set");const{dsid:i,user_id:r,session_id:n}=_(this,v)[v](),s=e.items.map((t=>({sku:t.item_id,price:t.price,quantity:t.quantity}))),o={events:[{event_type:"purchase",dsid:i,user_id:r,session_id:n,transaction_id:e.transaction_id,currency:e.currency,products:s}],tenant:this.merchant,market:this.market};(0,t.foolproof_send_beacon)("https://api.depict.ai/v2/create-events",o)}setMarket(t){this.market=t}setProductId(e){(0,t.dlog)("Setting product id",e),this.latest_product_id=e,this.latest_product_id_path=location.pathname,_(this,f)[f]({type:"page_view",product_id:e})}setUserId(e){(0,t.dlog)("Setting user id",e),this.latest_user_id=e||void 0}setSessionId(e){(0,t.dlog)("Setting session id",e),this.latest_session_id=e||void 0}observeRecommendations(){for(const[e,i]of Object.entries(d)){const r=`[${e}]`;(0,t.dlog)("Starting to observe product cards with selector",r),t.observer.onexists(r,(({element:n})=>{const s=n.getAttribute(e);s?this.trackProductCard(n,s,i):(0,t.derror)(`No tracking id found on element with attribute ${r}`,n)}))}}observePDPA2CButton(){t.observer.onexists(o,(({element:e})=>{(0,t.dlog)("Found pdp add-to-cart button",e),this.trackAddToCartButton(e)}))}}function y(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function b(){const e={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...(0,t.get_tracking_event_context_data)(),...e}}function w(e){(0,t.send_depict_unfiltered)([{...e,..._(this,v)[v]()}],!0)}function R(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}(0,t.add_to_version_info)("dpc",i);
3
+ const i="1.0.12";let r;var n;(n=r||(r={}))[n.Recommendation=0]="Recommendation",n[n.SnakeRecommendation=1]="SnakeRecommendation",n[n.SearchResult=2]="SearchResult",n[n.ProductListingResult=3]="ProductListingResult";const s={[r.Recommendation]:"recommendation_id",[r.SnakeRecommendation]:"recommendation_id",[r.SearchResult]:"search_result_id",[r.ProductListingResult]:"product_listing_result_id"},o=".depict-add-to-cart",d={"data-recommendation_id":r.SnakeRecommendation,"data-recommendation-id":r.Recommendation,"data-search-result-id":r.SearchResult,"data-product-listing-result-id":r.ProductListingResult};const a=(t,e)=>{if("number"==typeof e&&!isNaN(e))return e.toString();if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`);return e},c=(t,e)=>null==e?e:a(t,e),u=(t,e,i)=>{if("purchase"!==e)"number"==typeof i&&(i=i.toString()),"addToCart"===e?t.sendAddToCartEvent({product_id:a(e,i)}):"setMarket"===e?t.setMarket(a(e,i)):"setProductId"===e?t.setProductId(a(e,i)):"setUserId"===e?t.setUserId(c(e,i)):"setSessionId"===e&&t.setSessionId(c(e,i));else{if(!(t=>{if("object"!=typeof t||null===t)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(t.items))throw new Error("purchase event requires an items array of purchased products");if(!t.currency||"string"!=typeof t.currency)throw new Error("purchase event requires a currency string");if(!t.transaction_id||"string"!=typeof t.transaction_id&&"number"!=typeof t.transaction_id)throw new Error("purchase event requires a transaction_id string");t.transaction_id=t.transaction_id.toString();for(const e of t.items){if(""==e.item_id||isNaN(e.item_id)||"string"!=typeof e.item_id&&"number"!=typeof e.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(e.price))throw new Error("purchase item requires a unit price for the product purchased");if(!e.quantity||isNaN(e.quantity))throw new Error("purchase item requires a quantity of products purchased");e.item_id=e.item_id.toString()}return!0})(i))return;t.sendPurchaseEvent(i)}},h=t=>{const e=window.dpq||=function(){const t=function(){const e=arguments;t.send_event?t.send_event.apply(this,e):t.queue.push(e)};return t.queue=[],t}(),i=window.depict||={};i.dpq=e,i.eventHistory=e.queue,i.dpc=t,e.send_event||(e.send_event=(i,r)=>{e.queue.push([i,r]),u(t,i,r)},e.queue.forEach((([e,i])=>{u(t,e,i)})))};function _(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var l=0;function p(t){return"__private_"+l+++"_"+t}var m=p("parseOptions"),v=p("getContextData"),f=p("sendTrackingEvent"),k=p("getProductId");class g{constructor(e,i){if(Object.defineProperty(this,k,{value:R}),Object.defineProperty(this,f,{value:w}),Object.defineProperty(this,v,{value:b}),Object.defineProperty(this,m,{value:y}),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,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof g)return(0,t.dlog)("DPC already initialized. Returning existing instance."),window.depict.dpc;h(this),this.merchant=e,_(this,m)[m](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}checkIfTracked(e){return this.tracked_elements.has(e)?((0,t.dlog)("Already tracked",e),!0):(this.tracked_elements.add(e),!1)}trackProductCard(e,i,n){if(this.checkIfTracked(e))return;switch(n){case r.Recommendation:case r.SnakeRecommendation:e.addEventListener("click",(()=>this.sendRecommendationClickEvent(i))),(0,t.impression_track_element)({element:e,data:{recommendation_id:i,..._(this,v)[v]()}});break;case r.SearchResult:e.addEventListener("click",(()=>this.sendSearchResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{search_result_id:i,..._(this,v)[v]()}});break;case r.ProductListingResult:e.addEventListener("click",(()=>this.sendProductListingResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{product_listing_result_id:i,..._(this,v)[v]()}})}const s=e.querySelectorAll(o);for(const t of s)this.trackAddToCartButton(t,e)}trackAddToCartButton(e,i){this.checkIfTracked(e)||e.addEventListener("click",(()=>{if(!i){const i=_(this,k)[k]();return i?void this.sendAddToCartEvent({product_id:i}):void(0,t.derror)("No product ID set for add-to-cart click outside of product card",e)}for(const[t,e]of Object.entries(d)){const r=i.getAttribute(t);if(!r)continue;const n={[s[e]]:r};return void this.sendAddToCartEvent(n)}(0,t.derror)("Could not find tracking data for add-to-cart click on button",e)}))}sendAddToCartEvent(t){_(this,f)[f]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){_(this,f)[f]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){_(this,f)[f]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){_(this,f)[f]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(e){(0,t.depict_click)({recommendation_id:e,..._(this,v)[v]()})}sendSearchResultClickEvent(e){(0,t.depict_click)({search_result_id:e,..._(this,v)[v]()})}sendProductListingResultClickEvent(e){(0,t.depict_click)({product_listing_result_id:e,..._(this,v)[v]()})}sendPurchaseEvent(e){if(!this.market)throw Error("Market not set");const{dsid:i,user_id:r,session_id:n}=_(this,v)[v](),s=e.items.map((t=>({sku:t.item_id,price:t.price,quantity:t.quantity}))),o={events:[{event_type:"purchase",dsid:i,user_id:r,session_id:n,transaction_id:e.transaction_id,currency:e.currency,products:s}],tenant:this.merchant,market:this.market};(0,t.foolproof_send_beacon)("https://api.depict.ai/v2/create-events",o)}setMarket(t){this.market=t}setProductId(e){(0,t.dlog)("Setting product id",e),this.latest_product_id=e,this.latest_product_id_path=location.pathname,_(this,f)[f]({type:"page_view",product_id:e})}setUserId(e){(0,t.dlog)("Setting user id",e),this.latest_user_id=e||void 0}setSessionId(e){(0,t.dlog)("Setting session id",e),this.latest_session_id=e||void 0}observeRecommendations(){for(const[e,i]of Object.entries(d)){const r=`[${e}]`;(0,t.dlog)("Starting to observe product cards with selector",r),t.observer.onexists(r,(({element:n})=>{const s=n.getAttribute(e);s?this.trackProductCard(n,s,i):(0,t.derror)(`No tracking id found on element with attribute ${r}`,n)}))}}observePDPA2CButton(){t.observer.onexists(o,(({element:e})=>{(0,t.dlog)("Found pdp add-to-cart button",e),this.trackAddToCartButton(e)}))}}function y(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function b(){const e={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...(0,t.get_tracking_event_context_data)(),...e}}function w(e){(0,t.send_depict_unfiltered)([{...e,..._(this,v)[v]()}],!0)}function R(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}(0,t.add_to_version_info)("dpc",i);
4
4
  //# sourceMappingURL=main.js.map
package/dist/module.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import{add_to_version_info as t,dlog as e,impression_track_element as i,derror as r,depict_click as s,foolproof_send_beacon as n,observer as o,get_tracking_event_context_data as d,send_depict_unfiltered as a}from"@depict-ai/utilishared";
2
- /*! Depict PerformanceClient */const c="1.0.11";let u;var h;(h=u||(u={}))[h.Recommendation=0]="Recommendation",h[h.SnakeRecommendation=1]="SnakeRecommendation",h[h.SearchResult=2]="SearchResult",h[h.ProductListingResult=3]="ProductListingResult";const _={[u.Recommendation]:"recommendation_id",[u.SnakeRecommendation]:"recommendation_id",[u.SearchResult]:"search_result_id",[u.ProductListingResult]:"product_listing_result_id"},p=".depict-add-to-cart",m={"data-recommendation_id":u.SnakeRecommendation,"data-recommendation-id":u.Recommendation,"data-search-result-id":u.SearchResult,"data-product-listing-result-id":u.ProductListingResult};const l=(t,e)=>{if("number"==typeof e&&!isNaN(e))return e.toString();if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`);return e},v=(t,e)=>null==e?e:l(t,e),f=(t,e,i)=>{if("purchase"!==e)"number"==typeof i&&(i=i.toString()),"addToCart"===e?t.sendAddToCartEvent({product_id:l(e,i)}):"setMarket"===e?t.setMarket(l(e,i)):"setProductId"===e?t.setProductId(l(e,i)):"setUserId"===e?t.setUserId(v(e,i)):"setSessionId"===e&&t.setSessionId(v(e,i));else{if(!(t=>{if("object"!=typeof t||null===t)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(t.items))throw new Error("purchase event requires an items array of purchased products");if(!t.currency||"string"!=typeof t.currency)throw new Error("purchase event requires a currency string");if(!t.transaction_id||"string"!=typeof t.transaction_id&&"number"!=typeof t.transaction_id)throw new Error("purchase event requires a transaction_id string");t.transaction_id=t.transaction_id.toString();for(const e of t.items){if(""==e.item_id||isNaN(e.item_id)||"string"!=typeof e.item_id&&"number"!=typeof e.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(e.price))throw new Error("purchase item requires a unit price for the product purchased");if(!e.quantity||isNaN(e.quantity))throw new Error("purchase item requires a quantity of products purchased");e.item_id=e.item_id.toString()}return!0})(i))return;t.sendPurchaseEvent(i)}},k=t=>{const e=window.dpq||=function(){const t=function(){const e=arguments;t.send_event?t.send_event.apply(this,e):t.queue.push(e)};return t.queue=[],t}(),i=window.depict||={};i.dpq=e,i.eventHistory=e.queue,i.dpc=t,e.send_event||(e.send_event=(i,r)=>{e.queue.push([i,r]),f(t,i,r)},e.queue.forEach((([e,i])=>{f(t,e,i)})))};function g(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var y=0;function b(t){return"__private_"+y+++"_"+t}var w=b("parseOptions"),R=b("getContextData"),E=b("sendTrackingEvent"),P=b("getProductId");class S{constructor(t,i){if(Object.defineProperty(this,P,{value:A}),Object.defineProperty(this,E,{value:I}),Object.defineProperty(this,R,{value:q}),Object.defineProperty(this,w,{value:C}),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,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof S)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;k(this),this.merchant=t,g(this,w)[w](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}checkIfTracked(t){return this.tracked_elements.has(t)?(e("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackProductCard(t,e,r){if(this.checkIfTracked(t))return;switch(r){case u.Recommendation:case u.SnakeRecommendation:t.addEventListener("click",(()=>this.sendRecommendationClickEvent(e))),i({element:t,data:{recommendation_id:e,...g(this,R)[R]()}});break;case u.SearchResult:t.addEventListener("click",(()=>this.sendSearchResultClickEvent(e))),i({element:t,data:{search_result_id:e,...g(this,R)[R]()}});break;case u.ProductListingResult:t.addEventListener("click",(()=>this.sendProductListingResultClickEvent(e))),i({element:t,data:{product_listing_result_id:e,...g(this,R)[R]()}})}const s=t.querySelectorAll(p);for(const e of s)this.trackAddToCartButton(e,t)}trackAddToCartButton(t,e){this.checkIfTracked(t)||t.addEventListener("click",(()=>{if(!e){const e=g(this,P)[P]();return e?void this.sendAddToCartEvent({product_id:e}):void r("No product ID set for add-to-cart click outside of product card",t)}for(const[t,i]of Object.entries(m)){const r=e.getAttribute(t);if(!r)continue;const s={[_[i]]:r};return void this.sendAddToCartEvent(s)}r("Could not find tracking data for add-to-cart click on button",t)}))}sendAddToCartEvent(t){g(this,E)[E]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){g(this,E)[E]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){g(this,E)[E]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){g(this,E)[E]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(t){s({recommendation_id:t,...g(this,R)[R]()})}sendSearchResultClickEvent(t){s({search_result_id:t,...g(this,R)[R]()})}sendProductListingResultClickEvent(t){s({product_listing_result_id:t,...g(this,R)[R]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:i,session_id:r}=g(this,R)[R](),s=t.items.map((t=>({sku:t.item_id,price:t.price,quantity:t.quantity}))),o={events:[{event_type:"purchase",dsid:e,user_id:i,session_id:r,transaction_id:t.transaction_id,currency:t.currency,products:s}],tenant:this.merchant,market:this.market};n("https://api.depict.ai/v2/create-events",o)}setMarket(t){this.market=t}setProductId(t){e("Setting product id",t),this.latest_product_id=t,this.latest_product_id_path=location.pathname,g(this,E)[E]({type:"page_view",product_id:t})}setUserId(t){e("Setting user id",t),this.latest_user_id=t||void 0}setSessionId(t){e("Setting session id",t),this.latest_session_id=t||void 0}observeRecommendations(){for(const[t,i]of Object.entries(m)){const s=`[${t}]`;e("Starting to observe product cards with selector",s),o.onexists(s,(({element:e})=>{const n=e.getAttribute(t);n?this.trackProductCard(e,n,i):r(`No tracking id found on element with attribute ${s}`,e)}))}}observePDPA2CButton(){o.onexists(p,(({element:t})=>{e("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function C(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function q(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...d(),...t}}function I(t){a([{...t,...g(this,R)[R]()}],!0)}function A(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{S as DPC,c as version};
2
+ /*! Depict PerformanceClient */const c="1.0.12";let u;var h;(h=u||(u={}))[h.Recommendation=0]="Recommendation",h[h.SnakeRecommendation=1]="SnakeRecommendation",h[h.SearchResult=2]="SearchResult",h[h.ProductListingResult=3]="ProductListingResult";const _={[u.Recommendation]:"recommendation_id",[u.SnakeRecommendation]:"recommendation_id",[u.SearchResult]:"search_result_id",[u.ProductListingResult]:"product_listing_result_id"},p=".depict-add-to-cart",m={"data-recommendation_id":u.SnakeRecommendation,"data-recommendation-id":u.Recommendation,"data-search-result-id":u.SearchResult,"data-product-listing-result-id":u.ProductListingResult};const l=(t,e)=>{if("number"==typeof e&&!isNaN(e))return e.toString();if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`);return e},v=(t,e)=>null==e?e:l(t,e),f=(t,e,i)=>{if("purchase"!==e)"number"==typeof i&&(i=i.toString()),"addToCart"===e?t.sendAddToCartEvent({product_id:l(e,i)}):"setMarket"===e?t.setMarket(l(e,i)):"setProductId"===e?t.setProductId(l(e,i)):"setUserId"===e?t.setUserId(v(e,i)):"setSessionId"===e&&t.setSessionId(v(e,i));else{if(!(t=>{if("object"!=typeof t||null===t)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(t.items))throw new Error("purchase event requires an items array of purchased products");if(!t.currency||"string"!=typeof t.currency)throw new Error("purchase event requires a currency string");if(!t.transaction_id||"string"!=typeof t.transaction_id&&"number"!=typeof t.transaction_id)throw new Error("purchase event requires a transaction_id string");t.transaction_id=t.transaction_id.toString();for(const e of t.items){if(""==e.item_id||isNaN(e.item_id)||"string"!=typeof e.item_id&&"number"!=typeof e.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(e.price))throw new Error("purchase item requires a unit price for the product purchased");if(!e.quantity||isNaN(e.quantity))throw new Error("purchase item requires a quantity of products purchased");e.item_id=e.item_id.toString()}return!0})(i))return;t.sendPurchaseEvent(i)}},k=t=>{const e=window.dpq||=function(){const t=function(){const e=arguments;t.send_event?t.send_event.apply(this,e):t.queue.push(e)};return t.queue=[],t}(),i=window.depict||={};i.dpq=e,i.eventHistory=e.queue,i.dpc=t,e.send_event||(e.send_event=(i,r)=>{e.queue.push([i,r]),f(t,i,r)},e.queue.forEach((([e,i])=>{f(t,e,i)})))};function g(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var y=0;function b(t){return"__private_"+y+++"_"+t}var w=b("parseOptions"),R=b("getContextData"),E=b("sendTrackingEvent"),P=b("getProductId");class S{constructor(t,i){if(Object.defineProperty(this,P,{value:A}),Object.defineProperty(this,E,{value:I}),Object.defineProperty(this,R,{value:q}),Object.defineProperty(this,w,{value:C}),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,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof S)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;k(this),this.merchant=t,g(this,w)[w](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}checkIfTracked(t){return this.tracked_elements.has(t)?(e("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackProductCard(t,e,r){if(this.checkIfTracked(t))return;switch(r){case u.Recommendation:case u.SnakeRecommendation:t.addEventListener("click",(()=>this.sendRecommendationClickEvent(e))),i({element:t,data:{recommendation_id:e,...g(this,R)[R]()}});break;case u.SearchResult:t.addEventListener("click",(()=>this.sendSearchResultClickEvent(e))),i({element:t,data:{search_result_id:e,...g(this,R)[R]()}});break;case u.ProductListingResult:t.addEventListener("click",(()=>this.sendProductListingResultClickEvent(e))),i({element:t,data:{product_listing_result_id:e,...g(this,R)[R]()}})}const s=t.querySelectorAll(p);for(const e of s)this.trackAddToCartButton(e,t)}trackAddToCartButton(t,e){this.checkIfTracked(t)||t.addEventListener("click",(()=>{if(!e){const e=g(this,P)[P]();return e?void this.sendAddToCartEvent({product_id:e}):void r("No product ID set for add-to-cart click outside of product card",t)}for(const[t,i]of Object.entries(m)){const r=e.getAttribute(t);if(!r)continue;const s={[_[i]]:r};return void this.sendAddToCartEvent(s)}r("Could not find tracking data for add-to-cart click on button",t)}))}sendAddToCartEvent(t){g(this,E)[E]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){g(this,E)[E]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){g(this,E)[E]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){g(this,E)[E]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(t){s({recommendation_id:t,...g(this,R)[R]()})}sendSearchResultClickEvent(t){s({search_result_id:t,...g(this,R)[R]()})}sendProductListingResultClickEvent(t){s({product_listing_result_id:t,...g(this,R)[R]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:i,session_id:r}=g(this,R)[R](),s=t.items.map((t=>({sku:t.item_id,price:t.price,quantity:t.quantity}))),o={events:[{event_type:"purchase",dsid:e,user_id:i,session_id:r,transaction_id:t.transaction_id,currency:t.currency,products:s}],tenant:this.merchant,market:this.market};n("https://api.depict.ai/v2/create-events",o)}setMarket(t){this.market=t}setProductId(t){e("Setting product id",t),this.latest_product_id=t,this.latest_product_id_path=location.pathname,g(this,E)[E]({type:"page_view",product_id:t})}setUserId(t){e("Setting user id",t),this.latest_user_id=t||void 0}setSessionId(t){e("Setting session id",t),this.latest_session_id=t||void 0}observeRecommendations(){for(const[t,i]of Object.entries(m)){const s=`[${t}]`;e("Starting to observe product cards with selector",s),o.onexists(s,(({element:e})=>{const n=e.getAttribute(t);n?this.trackProductCard(e,n,i):r(`No tracking id found on element with attribute ${s}`,e)}))}}observePDPA2CButton(){o.onexists(p,(({element:t})=>{e("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function C(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function q(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...d(),...t}}function I(t){a([{...t,...g(this,R)[R]()}],!0)}function A(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{S as DPC,c as version};
3
3
  //# sourceMappingURL=module.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@depict-ai/dpc",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "packageManager": "yarn@3.2.2",
5
5
  "source": "index_npm.ts",
6
6
  "main": "dist/main.js",
@@ -37,8 +37,8 @@
37
37
  }
38
38
  },
39
39
  "dependencies": {
40
- "@depict-ai/types": "^1.0.11",
41
- "@depict-ai/utilishared": "^1.0.11"
40
+ "@depict-ai/types": "^1.0.16",
41
+ "@depict-ai/utilishared": "^1.0.12"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@babel/core": "latest",