@depict-ai/dpc 0.0.6 → 0.0.8
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 +22 -7
- package/dist/main.js +2 -2
- package/dist/module.js +2 -2
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export const version = "0.0.6";
|
|
1
|
+
export const version = "0.0.8";
|
|
3
2
|
/**
|
|
4
3
|
* The Depict Queue function used to register events to be sent to depict.
|
|
5
4
|
*/
|
|
@@ -25,6 +24,26 @@ declare global {
|
|
|
25
24
|
depict: DepictObject;
|
|
26
25
|
}
|
|
27
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
|
+
}
|
|
28
47
|
interface Options {
|
|
29
48
|
/**
|
|
30
49
|
* The initial market. Should be specified if the market is known at the point of intializing DPC.
|
|
@@ -79,11 +98,7 @@ export class DPC {
|
|
|
79
98
|
/**
|
|
80
99
|
* Sends a Purchase Event to Depict
|
|
81
100
|
*/
|
|
82
|
-
sendPurchaseEvent(data:
|
|
83
|
-
transaction_id: string;
|
|
84
|
-
currency: string;
|
|
85
|
-
products: PurchasedProduct[];
|
|
86
|
-
}): void;
|
|
101
|
+
sendPurchaseEvent(data: GA4Purchase): void;
|
|
87
102
|
/**
|
|
88
103
|
* Update the market used for tracking events
|
|
89
104
|
*/
|
package/dist/main.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var e=require("@depict-ai/utilishared");function t(e,t,r,
|
|
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",(()=>v)),t(module.exports,"version",(()=>r));
|
|
2
2
|
/*! Depict PerformanceClient */
|
|
3
|
-
const r="0.0.
|
|
3
|
+
const r="0.0.8",i=".depict-add-to-cart",n="[data-recommendation_id],[data-recommendation-id]";const o=(e,t)=>{if("string"!=typeof t)throw new Error(`${e} event requires a string as an argument`)},s=(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?(o(e,t),r.setMarket(t)):"setProductId"===e?(o(e,t),r.setProductId(t)):"setUserId"===e&&(o(e,t),r.setUserId(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]),s(e,r)},t.queue.forEach((([e,t])=>{s(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"),p=u("getContextData"),m=u("sendTrackingEvent"),_=u("getProductId");class v{constructor(t,r){if(Object.defineProperty(this,_,{value:y}),Object.defineProperty(this,m,{value:k}),Object.defineProperty(this,p,{value:f}),Object.defineProperty(this,h,{value:l}),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_user_id=void 0,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof v)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;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,p)[p]()}});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)?.dataset.recommendation_id,r=a(this,_)[_]();this.sendAddToCartEvent({product_id:r,recommendation_id:t})}))}sendAddToCartEvent(e){a(this,m)[m]({type:"add_to_cart",...e})}sendRecommendationImpressionEvent({recommendation_id:e}){a(this,m)[m]({type:"element_visible",recommendation_id:e})}sendRecommendationClickEvent({recommendation_id:t}){(0,e.depict_click)({recommendation_id:t,...a(this,p)[p]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:r,user_id:i,session_id:n}=a(this,p)[p](),o=t.items.map((e=>({sku:e.item_id,price:e.price,quantity:e.quantity}))),s={events:[{event_type:"purchase",dsid:r,user_id:i,session_id:n,transaction_id:t.transaction_id,currency:t.currency,products:o}],tenant:this.merchant,market:this.market};(0,e.foolproof_send_beacon)("https://api.depict.ai/v2/create-events",s)}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}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 l(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};return{...(0,e.get_tracking_event_context_data)(),...t}}function k(t){(0,e.send_depict_unfiltered)([{...t,...a(this,p)[p]()}],!0)}function y(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}(0,e.add_to_version_info)("dpc",r);
|
|
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 r,impression_track_element as
|
|
2
|
-
/*! Depict PerformanceClient */const c="0.0.
|
|
1
|
+
import{add_to_version_info as t,dlog as e,derror as r,impression_track_element as i,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.8",u=".depict-add-to-cart",h="[data-recommendation_id],[data-recommendation-id]";const p=(t,e)=>{if("string"!=typeof e)throw new Error(`${t} event requires a string as an argument`)},m=(t,e)=>{const r=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;r.sendPurchaseEvent(e)}else"setMarket"===t?(p(t,e),r.setMarket(e)):"setProductId"===t?(p(t,e),r.setProductId(e)):"setUserId"===t&&(p(t,e),r.setUserId(e))},_=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}(),r=window.depict||={};r.dpq=e,r.eventHistory=e.queue,r.dpc=t,e.send_event||(e.send_event=(t,r)=>{e.queue.push([t,r]),m(t,r)},e.queue.forEach((([t,e])=>{m(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"),b=l("getProductId");class g{constructor(t,r){if(Object.defineProperty(this,b,{value:C}),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_user_id=void 0,this.tracked_elements=new WeakSet,window.depict?.dpc instanceof g)return e("DPC already initialized. Returning existing instance."),window.depict.dpc;_(this),this.merchant=t,v(this,k)[k](r),this.auto_observe&&(this.observeRecommendations(),this.observePDPA2CButton())}trackRecommendationCard(t){const e=t.dataset.recommendation_id;if(!e)return void r("No recommendation_id found on card. Can't add tracking.",t);if(this.checkIfTracked(t))return;t.addEventListener("click",(()=>this.sendRecommendationClickEvent({recommendation_id:e}))),i({element:t,data:{recommendation_id:e,...v(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)?.dataset.recommendation_id,r=v(this,b)[b]();this.sendAddToCartEvent({product_id:r,recommendation_id:e})}))}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}){n({recommendation_id:t,...v(this,y)[y]()})}sendPurchaseEvent(t){if(!this.market)throw Error("Market not set");const{dsid:e,user_id:r,session_id:i}=v(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:r,session_id:i,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}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};return{...a(),...t}}function P(t){d([{...t,...v(this,y)[y]()}],!0)}function C(){if(this.latest_product_id_path===location.pathname)return this.latest_product_id}t("dpc",c);export{g 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": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"packageManager": "yarn@3.2.2",
|
|
5
5
|
"source": "index_npm.ts",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@depict-ai/types": "^0.0.
|
|
40
|
-
"@depict-ai/utilishared": "^0.0.
|
|
39
|
+
"@depict-ai/types": "^0.0.7",
|
|
40
|
+
"@depict-ai/utilishared": "^0.0.52"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@babel/core": "latest",
|