@depict-ai/dpc 0.0.9 → 0.0.10

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,134 +1,134 @@
1
- export const version = "0.0.9";
2
- /**
3
- * The Depict Queue function used to register events to be sent to depict.
4
- */
5
- export type DepictQueue = {
6
- (eventName: string, ...args: unknown[]): void;
7
- send_event?: (eventName: string, ...args: unknown[]) => void;
8
- queue: [string, unknown][];
9
- };
10
- /**
11
- * Depict Object containing everything depict-related on the page.
12
- */
13
- export interface DepictObject {
14
- eventHistory: [string, unknown][];
15
- dpq: DepictQueue;
16
- dpc: DPC;
17
- }
18
- declare global {
19
- interface Window {
20
- /**
21
- * Lifted DPQ function for ease-of-use
22
- */
23
- dpq: DepictQueue;
24
- depict: DepictObject;
25
- }
26
- }
27
- /**
28
- * Purchase format used by GA4 when sending a purchase event.
29
- * See https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund
30
- */
31
- interface GA4Purchase {
32
- transaction_id: string;
33
- currency: string;
34
- items: GA4PurchaseItem[];
35
- [additional: string]: unknown;
36
- }
37
- /**
38
- * Product format used by GA4 when sending a purchase event.
39
- * See https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase_item
40
- */
41
- interface GA4PurchaseItem {
42
- item_id: string;
43
- price: number;
44
- quantity: number;
45
- [additional: string]: unknown;
46
- }
47
- interface Options {
48
- /**
49
- * The initial market. Should be specified if the market is known at the point of intializing DPC.
50
- */
51
- market?: string;
52
- /**
53
- * If we should observe for recommendations on creation. If false, you must call `trackRecommendationCard` manually.
54
- */
55
- auto_observe?: boolean;
56
- }
57
- export class DPC {
58
- #private;
59
- merchant: string;
60
- auto_observe: boolean;
61
- market?: string;
62
- /** Latest product_id found in the DOM. Used in a2c & pageview events on PDPs. */
63
- latest_product_id?: string;
64
- /** The value of location.pathname when latest_product_id was set */
65
- latest_product_id_path?: string;
66
- /** Latest session_id set on the client. Used for all events. */
67
- latest_session_id?: string;
68
- /** Latest user_id found in the DOM. Used for all events. */
69
- latest_user_id?: string;
70
- constructor(merchant: string, options?: Options);
71
- /**
72
- * Adds click and impression tracking to a recommendation card
73
- */
74
- trackRecommendationCard(card: HTMLElement): void;
75
- tracked_elements: WeakSet<HTMLElement>;
76
- /**
77
- * Make sure the tracking is not set up twice on the same element.
78
- */
79
- checkIfTracked(element: HTMLElement): boolean;
80
- /**
81
- * Adds tracking to an Add to cart button.
82
- */
83
- trackAddToCartButton(button: HTMLElement): void;
84
- /**
85
- * Sends an Add to Cart Event to depict
86
- */
87
- sendAddToCartEvent(data: any): void;
88
- /**
89
- * Sends an impression event to Depict
90
- */
91
- sendRecommendationImpressionEvent({ recommendation_id }: {
92
- recommendation_id: string;
93
- }): void;
94
- /**
95
- * Sends a recommendation click event to Depict
96
- */
97
- sendRecommendationClickEvent({ recommendation_id }: {
98
- recommendation_id: string;
99
- }): void;
100
- /**
101
- * Sends a Purchase Event to Depict
102
- */
103
- sendPurchaseEvent(data: GA4Purchase): void;
104
- /**
105
- * Update the market used for tracking events
106
- */
107
- setMarket(market: string): void;
108
- /**
109
- * Update the product_id used for add-to-cart and pageview events on PDPs.
110
- */
111
- setProductId(product_id: string): void;
112
- /**
113
- * Set the user_id for logged in users.
114
- *
115
- * https://docs.depict.ai/docs/performance-client#informing-depict-of-logged-in-users
116
- */
117
- setUserId(user_id: string): void;
118
- /**
119
- * Set the current session_id.
120
- *
121
- * https://docs.depict.ai/docs/performance-client#browser-sessions
122
- */
123
- setSessionId(session_id: string): void;
124
- /**
125
- * Observe the dom for recommendations to track
126
- */
127
- observeRecommendations(): void;
128
- /**
129
- * Observe the dom for add to cart buttons. Used on product detail pages.
130
- */
131
- observePDPA2CButton(): void;
132
- }
1
+ export const version = "0.0.9";
2
+ /**
3
+ * The Depict Queue function used to register events to be sent to depict.
4
+ */
5
+ export type DepictQueue = {
6
+ (eventName: string, ...args: unknown[]): void;
7
+ send_event?: (eventName: string, ...args: unknown[]) => void;
8
+ queue: [string, unknown][];
9
+ };
10
+ /**
11
+ * Depict Object containing everything depict-related on the page.
12
+ */
13
+ export interface DepictObject {
14
+ eventHistory: [string, unknown][];
15
+ dpq: DepictQueue;
16
+ dpc: DPC;
17
+ }
18
+ declare global {
19
+ interface Window {
20
+ /**
21
+ * Lifted DPQ function for ease-of-use
22
+ */
23
+ dpq: DepictQueue;
24
+ depict: DepictObject;
25
+ }
26
+ }
27
+ /**
28
+ * Purchase format used by GA4 when sending a purchase event.
29
+ * See https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?client_type=gtag#make_a_purchase_or_issue_a_refund
30
+ */
31
+ interface GA4Purchase {
32
+ transaction_id: string;
33
+ currency: string;
34
+ items: GA4PurchaseItem[];
35
+ [additional: string]: unknown;
36
+ }
37
+ /**
38
+ * Product format used by GA4 when sending a purchase event.
39
+ * See https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#purchase_item
40
+ */
41
+ interface GA4PurchaseItem {
42
+ item_id: string;
43
+ price: number;
44
+ quantity: number;
45
+ [additional: string]: unknown;
46
+ }
47
+ interface Options {
48
+ /**
49
+ * The initial market. Should be specified if the market is known at the point of intializing DPC.
50
+ */
51
+ market?: string;
52
+ /**
53
+ * If we should observe for recommendations on creation. If false, you must call `trackRecommendationCard` manually.
54
+ */
55
+ auto_observe?: boolean;
56
+ }
57
+ export class DPC {
58
+ #private;
59
+ merchant: string;
60
+ auto_observe: boolean;
61
+ market?: string;
62
+ /** Latest product_id found in the DOM. Used in a2c & pageview events on PDPs. */
63
+ latest_product_id?: string;
64
+ /** The value of location.pathname when latest_product_id was set */
65
+ latest_product_id_path?: string;
66
+ /** Latest session_id set on the client. Used for all events. */
67
+ latest_session_id?: string;
68
+ /** Latest user_id found in the DOM. Used for all events. */
69
+ latest_user_id?: string;
70
+ constructor(merchant: string, options?: Options);
71
+ /**
72
+ * Adds click and impression tracking to a recommendation card
73
+ */
74
+ trackRecommendationCard(card: HTMLElement): void;
75
+ tracked_elements: WeakSet<HTMLElement>;
76
+ /**
77
+ * Make sure the tracking is not set up twice on the same element.
78
+ */
79
+ checkIfTracked(element: HTMLElement): boolean;
80
+ /**
81
+ * Adds tracking to an Add to cart button.
82
+ */
83
+ trackAddToCartButton(button: HTMLElement): void;
84
+ /**
85
+ * Sends an Add to Cart Event to depict
86
+ */
87
+ sendAddToCartEvent(data: any): void;
88
+ /**
89
+ * Sends an impression event to Depict
90
+ */
91
+ sendRecommendationImpressionEvent({ recommendation_id }: {
92
+ recommendation_id: string;
93
+ }): void;
94
+ /**
95
+ * Sends a recommendation click event to Depict
96
+ */
97
+ sendRecommendationClickEvent({ recommendation_id }: {
98
+ recommendation_id: string;
99
+ }): void;
100
+ /**
101
+ * Sends a Purchase Event to Depict
102
+ */
103
+ sendPurchaseEvent(data: GA4Purchase): void;
104
+ /**
105
+ * Update the market used for tracking events
106
+ */
107
+ setMarket(market: string): void;
108
+ /**
109
+ * Update the product_id used for add-to-cart and pageview events on PDPs.
110
+ */
111
+ setProductId(product_id: string): void;
112
+ /**
113
+ * Set the user_id for logged in users.
114
+ *
115
+ * https://docs.depict.ai/docs/performance-client#informing-depict-of-logged-in-users
116
+ */
117
+ setUserId(user_id: string): void;
118
+ /**
119
+ * Set the current session_id.
120
+ *
121
+ * https://docs.depict.ai/docs/performance-client#browser-sessions
122
+ */
123
+ setSessionId(session_id: string): void;
124
+ /**
125
+ * Observe the dom for recommendations to track
126
+ */
127
+ observeRecommendations(): void;
128
+ /**
129
+ * Observe the dom for add to cart buttons. Used on product detail pages.
130
+ */
131
+ observePDPA2CButton(): void;
132
+ }
133
133
 
134
134
  //# sourceMappingURL=index.d.ts.map
package/dist/main.js CHANGED
@@ -1,4 +1,4 @@
1
- var e=require("@depict-ai/utilishared");function t(e,t,r,i){Object.defineProperty(e,t,{get:r,set:i,enumerable:!0,configurable:!0})}t(module.exports,"DPC",(()=>l)),t(module.exports,"version",(()=>r));
1
+ var e=require("@depict-ai/utilishared");function t(e,t,i,r){Object.defineProperty(e,t,{get:i,set:r,enumerable:!0,configurable:!0})}t(module.exports,"DPC",(()=>l)),t(module.exports,"version",(()=>i));
2
2
  /*! Depict PerformanceClient */
3
- const r="0.0.9",i=".depict-add-to-cart",n="[data-recommendation_id],[data-recommendation-id]";const s=(e,t)=>{if("string"!=typeof t)throw new Error(`${e} event requires a string as an argument`)},o=(e,t)=>{const r=window.depict.dpc;if("purchase"===e){if(!(e=>{if("object"!=typeof e||null===e)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(e.items))throw new Error("purchase event requires an items array of purchased products");if(!e.currency||"string"!=typeof e.currency)throw new Error("purchase event requires a currency string");if(!e.transaction_id||"string"!=typeof e.transaction_id)throw new Error("purchase event requires a transaction_id string");for(const t of e.items){if(!t.item_id||"string"!=typeof t.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(t.price))throw new Error("purchase item requires a unit price for the product purchased");if(!t.quantity||isNaN(t.quantity))throw new Error("purchase item requires a quantity of products purchased")}return!0})(t))return;r.sendPurchaseEvent(t)}else"setMarket"===e?(s(e,t),r.setMarket(t)):"setProductId"===e?(s(e,t),r.setProductId(t)):"setUserId"===e?(s(e,t),r.setUserId(t)):"setSessionId"===e&&(s(e,t),r.setSessionId(t))},d=e=>{const t=window.dpq||=function(){const e=function(){const t=arguments;e.send_event?e.send_event.apply(this,t):e.queue.push(t)};return e.queue=[],e}(),r=window.depict||={};r.dpq=t,r.eventHistory=t.queue,r.dpc=e,t.send_event||(t.send_event=(e,r)=>{t.queue.push([e,r]),o(e,r)},t.queue.forEach((([e,t])=>{o(e,t)})))};function a(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var c=0;function u(e){return"__private_"+c+++"_"+e}var h=u("parseOptions"),m=u("getContextData"),_=u("sendTrackingEvent"),p=u("getProductId");class l{constructor(t,r){if(Object.defineProperty(this,p,{value:y}),Object.defineProperty(this,_,{value:k}),Object.defineProperty(this,m,{value:f}),Object.defineProperty(this,h,{value:v}),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 l)return(0,e.dlog)("DPC already initialized. Returning existing instance."),window.depict.dpc;d(this),this.merchant=t,a(this,h)[h](r),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}trackRecommendationCard(t){const r=t.dataset.recommendation_id||t.dataset.recommendationId;if(!r)return void(0,e.derror)("No recommendation_id found on card. Can't add tracking.",t);if(this.checkIfTracked(t))return;t.addEventListener("click",(()=>this.sendRecommendationClickEvent({recommendation_id:r}))),(0,e.impression_track_element)({element:t,data:{recommendation_id:r,...a(this,m)[m]()}});const n=t.querySelector(i);n&&this.trackAddToCartButton(n)}checkIfTracked(t){return this.tracked_elements.has(t)?((0,e.dlog)("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackAddToCartButton(e){this.checkIfTracked(e)||e.addEventListener("click",(()=>{const t=e.closest(n),r=t?.dataset.recommendation_id||t?.dataset.recommendationId,i=a(this,p)[p]();this.sendAddToCartEvent({product_id:i,recommendation_id:r})}))}sendAddToCartEvent(e){a(this,_)[_]({type:"add_to_cart",...e})}sendRecommendationImpressionEvent({recommendation_id:e}){a(this,_)[_]({type:"element_visible",recommendation_id:e})}sendRecommendationClickEvent({recommendation_id:t}){(0,e.depict_click)({recommendation_id:t,...a(this,m)[m]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:r,user_id:i,session_id:n}=a(this,m)[m](),s=t.items.map((e=>({sku:e.item_id,price:e.price,quantity:e.quantity}))),o={events:[{event_type:"purchase",dsid:r,user_id:i,session_id:n,transaction_id:t.transaction_id,currency:t.currency,products:s}],tenant:this.merchant,market:this.market};(0,e.foolproof_send_beacon)("https://api.depict.ai/v2/create-events",o)}setMarket(e){this.market=e}setProductId(t){(0,e.dlog)("Setting product id",t),this.latest_product_id=t,this.latest_product_id_path=location.pathname}setUserId(t){(0,e.dlog)("Setting user id",t),this.latest_user_id=t}setSessionId(t){(0,e.dlog)("Setting session id",t),this.latest_session_id=t}observeRecommendations(){(0,e.dlog)("Starting to observe recommendations with selector",n),e.observer.onexists(n,(({element:t})=>{(0,e.dlog)("found recommendation",t),this.trackRecommendationCard(t)}))}observePDPA2CButton(){e.observer.onexists(i,(({element:t})=>{(0,e.dlog)("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function v(e){void 0!==e&&(void 0!==e.auto_observe&&(this.auto_observe=e.auto_observe),void 0!==e.market&&(this.market=e.market))}function f(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...(0,e.get_tracking_event_context_data)(),...t}}function k(t){(0,e.send_depict_unfiltered)([{...t,...a(this,m)[m]()}],!0)}function y(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}(0,e.add_to_version_info)("dpc",r);
3
+ const i="0.0.9",r=".depict-add-to-cart",n="[data-recommendation_id],[data-recommendation-id]";const s=(e,t)=>{if("number"==typeof t&&!isNaN(t))return t.toString();if("string"!=typeof t)throw new Error(`${e} event requires a string as an argument`);return t},o=(e,t)=>{const i=window.depict.dpc;if("purchase"!==e)"number"==typeof t&&(t=t.toString()),"setMarket"===e?i.setMarket(s(e,t)):"setProductId"===e?i.setProductId(s(e,t)):"setUserId"===e?i.setUserId(s(e,t)):"setSessionId"===e&&i.setSessionId(s(e,t));else{if(!(e=>{if("object"!=typeof e||null===e)throw new Error("purchase event requires an object as an argument");if(!Array.isArray(e.items))throw new Error("purchase event requires an items array of purchased products");if(!e.currency||"string"!=typeof e.currency)throw new Error("purchase event requires a currency string");if(!e.transaction_id||"string"!=typeof e.transaction_id&&"number"!=typeof e.transaction_id)throw new Error("purchase event requires a transaction_id string");e.transaction_id=e.transaction_id.toString();for(const t of e.items){if(""==t.item_id||isNaN(t.item_id)||"string"!=typeof t.item_id&&"number"!=typeof t.item_id)throw new Error("purchased item requires an item_id of the purchased product");if(isNaN(t.price))throw new Error("purchase item requires a unit price for the product purchased");if(!t.quantity||isNaN(t.quantity))throw new Error("purchase item requires a quantity of products purchased");t.item_id=t.item_id.toString()}return!0})(t))return;i.sendPurchaseEvent(t)}},d=e=>{const t=window.dpq||=function(){const e=function(){const t=arguments;e.send_event?e.send_event.apply(this,t):e.queue.push(t)};return e.queue=[],e}(),i=window.depict||={};i.dpq=t,i.eventHistory=t.queue,i.dpc=e,t.send_event||(t.send_event=(e,i)=>{t.queue.push([e,i]),o(e,i)},t.queue.forEach((([e,t])=>{o(e,t)})))};function a(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var c=0;function u(e){return"__private_"+c+++"_"+e}var h=u("parseOptions"),m=u("getContextData"),_=u("sendTrackingEvent"),p=u("getProductId");class l{constructor(t,i){if(Object.defineProperty(this,p,{value:y}),Object.defineProperty(this,_,{value:k}),Object.defineProperty(this,m,{value:v}),Object.defineProperty(this,h,{value:f}),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 l)return(0,e.dlog)("DPC already initialized. Returning existing instance."),window.depict.dpc;d(this),this.merchant=t,a(this,h)[h](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}trackRecommendationCard(t){const i=t.dataset.recommendation_id||t.dataset.recommendationId;if(!i)return void(0,e.derror)("No recommendation_id found on card. Can't add tracking.",t);if(this.checkIfTracked(t))return;t.addEventListener("click",(()=>this.sendRecommendationClickEvent({recommendation_id:i}))),(0,e.impression_track_element)({element:t,data:{recommendation_id:i,...a(this,m)[m]()}});const n=t.querySelector(r);n&&this.trackAddToCartButton(n)}checkIfTracked(t){return this.tracked_elements.has(t)?((0,e.dlog)("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackAddToCartButton(e){this.checkIfTracked(e)||e.addEventListener("click",(()=>{const t=e.closest(n),i=t?.dataset.recommendation_id||t?.dataset.recommendationId,r=a(this,p)[p]();this.sendAddToCartEvent({product_id:r,recommendation_id:i})}))}sendAddToCartEvent(e){a(this,_)[_]({type:"add_to_cart",...e})}sendRecommendationImpressionEvent({recommendation_id:e}){a(this,_)[_]({type:"element_visible",recommendation_id:e})}sendRecommendationClickEvent({recommendation_id:t}){(0,e.depict_click)({recommendation_id:t,...a(this,m)[m]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:i,user_id:r,session_id:n}=a(this,m)[m](),s=t.items.map((e=>({sku:e.item_id,price:e.price,quantity:e.quantity}))),o={events:[{event_type:"purchase",dsid:i,user_id:r,session_id:n,transaction_id:t.transaction_id,currency:t.currency,products:s}],tenant:this.merchant,market:this.market};(0,e.foolproof_send_beacon)("https://api.depict.ai/v2/create-events",o)}setMarket(e){this.market=e}setProductId(t){(0,e.dlog)("Setting product id",t),this.latest_product_id=t,this.latest_product_id_path=location.pathname}setUserId(t){(0,e.dlog)("Setting user id",t),this.latest_user_id=t}setSessionId(t){(0,e.dlog)("Setting session id",t),this.latest_session_id=t}observeRecommendations(){(0,e.dlog)("Starting to observe recommendations with selector",n),e.observer.onexists(n,(({element:t})=>{(0,e.dlog)("found recommendation",t),this.trackRecommendationCard(t)}))}observePDPA2CButton(){e.observer.onexists(r,(({element:t})=>{(0,e.dlog)("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function f(e){void 0!==e&&(void 0!==e.auto_observe&&(this.auto_observe=e.auto_observe),void 0!==e.market&&(this.market=e.market))}function v(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...(0,e.get_tracking_event_context_data)(),...t}}function k(t){(0,e.send_depict_unfiltered)([{...t,...a(this,m)[m]()}],!0)}function y(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}(0,e.add_to_version_info)("dpc",i);
4
4
  //# sourceMappingURL=main.js.map
package/dist/module.js CHANGED
@@ -1,3 +1,3 @@
1
- import{add_to_version_info as t,dlog as e,derror as i,impression_track_element as r,depict_click as s,foolproof_send_beacon as n,observer as o,get_tracking_event_context_data as a,send_depict_unfiltered as d}from"@depict-ai/utilishared";
2
- /*! Depict PerformanceClient */const c="0.0.9",u=".depict-add-to-cart",h="[data-recommendation_id],[data-recommendation-id]";const m=(t,e)=>{if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`)},_=(t,e)=>{const i=window.depict.dpc;if("purchase"===t){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)throw new Error("purchase event requires a transaction_id string");for(const e of t.items){if(!e.item_id||"string"!=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")}return!0})(e))return;i.sendPurchaseEvent(e)}else"setMarket"===t?(m(t,e),i.setMarket(e)):"setProductId"===t?(m(t,e),i.setProductId(e)):"setUserId"===t?(m(t,e),i.setUserId(e)):"setSessionId"===t&&(m(t,e),i.setSessionId(e))},p=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=(t,i)=>{e.queue.push([t,i]),_(t,i)},e.queue.forEach((([t,e])=>{_(t,e)})))};function v(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var f=0;function l(t){return"__private_"+f+++"_"+t}var k=l("parseOptions"),y=l("getContextData"),w=l("sendTrackingEvent"),g=l("getProductId");class b{constructor(t,i){if(Object.defineProperty(this,g,{value:I}),Object.defineProperty(this,w,{value:P}),Object.defineProperty(this,y,{value:E}),Object.defineProperty(this,k,{value:q}),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 b)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;p(this),this.merchant=t,v(this,k)[k](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}trackRecommendationCard(t){const e=t.dataset.recommendation_id||t.dataset.recommendationId;if(!e)return void i("No recommendation_id found on card. Can't add tracking.",t);if(this.checkIfTracked(t))return;t.addEventListener("click",(()=>this.sendRecommendationClickEvent({recommendation_id:e}))),r({element:t,data:{recommendation_id:e,...v(this,y)[y]()}});const s=t.querySelector(u);s&&this.trackAddToCartButton(s)}checkIfTracked(t){return this.tracked_elements.has(t)?(e("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackAddToCartButton(t){this.checkIfTracked(t)||t.addEventListener("click",(()=>{const e=t.closest(h),i=e?.dataset.recommendation_id||e?.dataset.recommendationId,r=v(this,g)[g]();this.sendAddToCartEvent({product_id:r,recommendation_id:i})}))}sendAddToCartEvent(t){v(this,w)[w]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent({recommendation_id:t}){v(this,w)[w]({type:"element_visible",recommendation_id:t})}sendRecommendationClickEvent({recommendation_id:t}){s({recommendation_id:t,...v(this,y)[y]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:i,session_id:r}=v(this,y)[y](),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}setUserId(t){e("Setting user id",t),this.latest_user_id=t}setSessionId(t){e("Setting session id",t),this.latest_session_id=t}observeRecommendations(){e("Starting to observe recommendations with selector",h),o.onexists(h,(({element:t})=>{e("found recommendation",t),this.trackRecommendationCard(t)}))}observePDPA2CButton(){o.onexists(u,(({element:t})=>{e("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function q(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function E(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...a(),...t}}function P(t){d([{...t,...v(this,y)[y]()}],!0)}function I(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{b as DPC,c as version};
1
+ import{add_to_version_info as t,dlog as e,derror as i,impression_track_element as r,depict_click as n,foolproof_send_beacon as s,observer as o,get_tracking_event_context_data as a,send_depict_unfiltered as d}from"@depict-ai/utilishared";
2
+ /*! Depict PerformanceClient */const c="0.0.9",u=".depict-add-to-cart",h="[data-recommendation_id],[data-recommendation-id]";const m=(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},_=(t,e)=>{const i=window.depict.dpc;if("purchase"!==t)"number"==typeof e&&(e=e.toString()),"setMarket"===t?i.setMarket(m(t,e)):"setProductId"===t?i.setProductId(m(t,e)):"setUserId"===t?i.setUserId(m(t,e)):"setSessionId"===t&&i.setSessionId(m(t,e));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})(e))return;i.sendPurchaseEvent(e)}},p=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=(t,i)=>{e.queue.push([t,i]),_(t,i)},e.queue.forEach((([t,e])=>{_(t,e)})))};function f(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var v=0;function l(t){return"__private_"+v+++"_"+t}var k=l("parseOptions"),y=l("getContextData"),w=l("sendTrackingEvent"),g=l("getProductId");class b{constructor(t,i){if(Object.defineProperty(this,g,{value:I}),Object.defineProperty(this,w,{value:P}),Object.defineProperty(this,y,{value:E}),Object.defineProperty(this,k,{value:q}),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 b)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;p(this),this.merchant=t,f(this,k)[k](i),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}trackRecommendationCard(t){const e=t.dataset.recommendation_id||t.dataset.recommendationId;if(!e)return void i("No recommendation_id found on card. Can't add tracking.",t);if(this.checkIfTracked(t))return;t.addEventListener("click",(()=>this.sendRecommendationClickEvent({recommendation_id:e}))),r({element:t,data:{recommendation_id:e,...f(this,y)[y]()}});const n=t.querySelector(u);n&&this.trackAddToCartButton(n)}checkIfTracked(t){return this.tracked_elements.has(t)?(e("Already tracked",t),!0):(this.tracked_elements.add(t),!1)}trackAddToCartButton(t){this.checkIfTracked(t)||t.addEventListener("click",(()=>{const e=t.closest(h),i=e?.dataset.recommendation_id||e?.dataset.recommendationId,r=f(this,g)[g]();this.sendAddToCartEvent({product_id:r,recommendation_id:i})}))}sendAddToCartEvent(t){f(this,w)[w]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent({recommendation_id:t}){f(this,w)[w]({type:"element_visible",recommendation_id:t})}sendRecommendationClickEvent({recommendation_id:t}){n({recommendation_id:t,...f(this,y)[y]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:i,session_id:r}=f(this,y)[y](),n=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:n}],tenant:this.merchant,market:this.market};s("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}setUserId(t){e("Setting user id",t),this.latest_user_id=t}setSessionId(t){e("Setting session id",t),this.latest_session_id=t}observeRecommendations(){e("Starting to observe recommendations with selector",h),o.onexists(h,(({element:t})=>{e("found recommendation",t),this.trackRecommendationCard(t)}))}observePDPA2CButton(){o.onexists(u,(({element:t})=>{e("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function q(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function E(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...a(),...t}}function P(t){d([{...t,...f(this,y)[y]()}],!0)}function I(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{b as DPC,c as version};
3
3
  //# sourceMappingURL=module.js.map
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@depict-ai/dpc",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "packageManager": "yarn@3.2.2",
5
5
  "source": "index_npm.ts",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/module.js",
8
8
  "types": "dist/index.d.ts",
9
+ "description": "Depict Performance Client",
9
10
  "license": "MIT",
10
11
  "publishConfig": {
11
12
  "access": "public"
@@ -36,8 +37,8 @@
36
37
  }
37
38
  },
38
39
  "dependencies": {
39
- "@depict-ai/types": "^0.0.8",
40
- "@depict-ai/utilishared": "^0.0.53"
40
+ "@depict-ai/types": "^0.0.24",
41
+ "@depict-ai/utilishared": "^0.0.76"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@babel/core": "latest",