@depict-ai/dpc 0.0.9 → 1.0.1

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/dist/index.d.ts CHANGED
@@ -1,4 +1,10 @@
1
- export const version = "0.0.9";
1
+ export const version = "1.0.1";
2
+ enum ProductCardType {
3
+ Recommendation = 0,
4
+ SnakeRecommendation = 1,
5
+ SearchResult = 2,
6
+ ProductListingResult = 3
7
+ }
2
8
  /**
3
9
  * The Depict Queue function used to register events to be sent to depict.
4
10
  */
@@ -68,35 +74,47 @@ export class DPC {
68
74
  /** Latest user_id found in the DOM. Used for all events. */
69
75
  latest_user_id?: string;
70
76
  constructor(merchant: string, options?: Options);
71
- /**
72
- * Adds click and impression tracking to a recommendation card
73
- */
74
- trackRecommendationCard(card: HTMLElement): void;
75
77
  tracked_elements: WeakSet<HTMLElement>;
76
78
  /**
77
79
  * Make sure the tracking is not set up twice on the same element.
78
80
  */
79
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;
80
86
  /**
81
87
  * Adds tracking to an Add to cart button.
82
88
  */
83
- trackAddToCartButton(button: HTMLElement): void;
89
+ trackAddToCartButton(button: HTMLElement, productCard?: HTMLElement): void;
84
90
  /**
85
91
  * Sends an Add to Cart Event to depict
86
92
  */
87
93
  sendAddToCartEvent(data: any): void;
88
94
  /**
89
- * Sends an impression event to Depict
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
90
100
  */
91
- sendRecommendationImpressionEvent({ recommendation_id }: {
92
- recommendation_id: string;
93
- }): void;
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;
94
106
  /**
95
107
  * Sends a recommendation click event to Depict
96
108
  */
97
- sendRecommendationClickEvent({ recommendation_id }: {
98
- recommendation_id: string;
99
- }): void;
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;
100
118
  /**
101
119
  * Sends a Purchase Event to Depict
102
120
  */
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 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",(()=>k)),e(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="1.0.1";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,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(a(e,i)):"setSessionId"===e&&t.setSessionId(a(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)}},u=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]),c(t,i,r)},e.queue.forEach((([e,i])=>{c(t,e,i)})))};function h(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var _=0;function l(t){return"__private_"+_+++"_"+t}var m=l("parseOptions"),p=l("getContextData"),v=l("sendTrackingEvent"),f=l("getProductId");class k{constructor(e,i){if(Object.defineProperty(this,f,{value:w}),Object.defineProperty(this,v,{value:b}),Object.defineProperty(this,p,{value:y}),Object.defineProperty(this,m,{value:g}),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 k)return(0,t.dlog)("DPC already initialized. Returning existing instance."),window.depict.dpc;u(this),this.merchant=e,h(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,...h(this,p)[p]()}});break;case r.SearchResult:e.addEventListener("click",(()=>this.sendSearchResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{search_result_id:i,...h(this,p)[p]()}});break;case r.ProductListingResult:e.addEventListener("click",(()=>this.sendProductListingResultClickEvent(i))),(0,t.impression_track_element)({element:e,data:{product_listing_result_id:i,...h(this,p)[p]()}})}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=h(this,f)[f]();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){h(this,v)[v]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){h(this,v)[v]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){h(this,v)[v]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){h(this,v)[v]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(e){(0,t.depict_click)({recommendation_id:e,...h(this,p)[p]()})}sendSearchResultClickEvent(e){(0,t.depict_click)({search_result_id:e,...h(this,p)[p]()})}sendProductListingResultClickEvent(e){(0,t.depict_click)({product_listing_result_id:e,...h(this,p)[p]()})}sendPurchaseEvent(e){if(!this.market)throw Error("Market not set");const{dsid:i,user_id:r,session_id:n}=h(this,p)[p](),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}setUserId(e){(0,t.dlog)("Setting user id",e),this.latest_user_id=e}setSessionId(e){(0,t.dlog)("Setting session id",e),this.latest_session_id=e}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 g(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function y(){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 b(e){(0,t.send_depict_unfiltered)([{...e,...h(this,p)[p]()}],!0)}function w(){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
- 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,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.1";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"},m=".depict-add-to-cart",p={"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,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(l(e,i)):"setSessionId"===e&&t.setSessionId(l(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)}},f=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]),v(t,i,r)},e.queue.forEach((([e,i])=>{v(t,e,i)})))};function k(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var g=0;function y(t){return"__private_"+g+++"_"+t}var b=y("parseOptions"),w=y("getContextData"),R=y("sendTrackingEvent"),E=y("getProductId");class P{constructor(t,i){if(Object.defineProperty(this,E,{value:I}),Object.defineProperty(this,R,{value:q}),Object.defineProperty(this,w,{value:C}),Object.defineProperty(this,b,{value:S}),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 P)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;f(this),this.merchant=t,k(this,b)[b](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,...k(this,w)[w]()}});break;case u.SearchResult:t.addEventListener("click",(()=>this.sendSearchResultClickEvent(e))),i({element:t,data:{search_result_id:e,...k(this,w)[w]()}});break;case u.ProductListingResult:t.addEventListener("click",(()=>this.sendProductListingResultClickEvent(e))),i({element:t,data:{product_listing_result_id:e,...k(this,w)[w]()}})}const s=t.querySelectorAll(m);for(const e of s)this.trackAddToCartButton(e,t)}trackAddToCartButton(t,e){this.checkIfTracked(t)||t.addEventListener("click",(()=>{if(!e){const e=k(this,E)[E]();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(p)){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){k(this,R)[R]({type:"add_to_cart",...t})}sendRecommendationImpressionEvent(t){k(this,R)[R]({type:"element_visible",recommendation_id:t})}sendSearchResultImpressionEvent(t){k(this,R)[R]({type:"element_visible",search_result_id:t})}sendProductListingResultImpressionEvent(t){k(this,R)[R]({type:"element_visible",product_listing_result_id:t})}sendRecommendationClickEvent(t){s({recommendation_id:t,...k(this,w)[w]()})}sendSearchResultClickEvent(t){s({search_result_id:t,...k(this,w)[w]()})}sendProductListingResultClickEvent(t){s({product_listing_result_id:t,...k(this,w)[w]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:i,session_id:r}=k(this,w)[w](),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(){for(const[t,i]of Object.entries(p)){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(m,(({element:t})=>{e("Found pdp add-to-cart button",t),this.trackAddToCartButton(t)}))}}function S(t){void 0!==t&&(void 0!==t.auto_observe&&(this.auto_observe=t.auto_observe),void 0!==t.market&&(this.market=t.market))}function C(){const t={market:this.market,tenant:this.merchant,user_id:this.latest_user_id,session_id:this.latest_session_id};return{...d(),...t}}function q(t){a([{...t,...k(this,w)[w]()}],!0)}function I(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{P 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": "1.0.1",
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": "^1.0.1",
41
+ "@depict-ai/utilishared": "^1.0.1"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@babel/core": "latest",