@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 +31 -13
- package/dist/main.js +2 -2
- package/dist/module.js +2 -2
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
export const version = "
|
|
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
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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(
|
|
98
|
-
|
|
99
|
-
|
|
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
|
|
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
|
|
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,
|
|
2
|
-
/*! Depict PerformanceClient */const c="0.0.
|
|
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": "
|
|
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": "^
|
|
40
|
-
"@depict-ai/utilishared": "^
|
|
40
|
+
"@depict-ai/types": "^1.0.1",
|
|
41
|
+
"@depict-ai/utilishared": "^1.0.1"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@babel/core": "latest",
|