@salla.sa/analytics 3.0.2 → 3.0.3
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/cjs/checkout.js +1 -1
- package/dist/esm/checkout.js +1 -1
- package/package.json +2 -2
- package/src/checkout/event-transformer.js +6 -0
- package/src/checkout/index.js +3 -0
- package/src/checkout/xsell-product-added.js +27 -0
- package/src/checkout/xsell-product-clicked.js +23 -0
- package/src/checkout/xsell-product-fields.js +32 -0
- package/src/checkout/xsell-product-list-viewed.js +36 -0
package/dist/cjs/checkout.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("./chunks/extra-context-B8aoGbHo.js"),e=require("@jitsu/js"),s={get(t,e=null){const s="undefined"!=typeof window?window.initialData:null;if(!s||"object"!=typeof s)return e;let i=s;for(const s of t.split(".")){if(null==i||"object"!=typeof i||!(s in i))return e;i=i[s]}return void 0!==i?i:e}};const i="s-session-id";let n=null;function r(t){return`${t}_${s.get("store.id")}`}var o={SESSION_STORAGE_KEY:i,SESSION_TTL_MINUTES:30,resolve:()=>(n||(n=function(t,e=null){try{const s=localStorage.getItem(r(t));if(!s)return e;const i=JSON.parse(s);return i&&"object"==typeof i?(new Date).getTime()>i.expiry?(localStorage.removeItem(r(t)),e):i.value:e}catch(t){return e}}(i)||t.generateSessionId()),function(t,e,s=30){try{const i=(new Date).getTime()+60*s*1e3;localStorage.setItem(r(t),JSON.stringify({value:e,expiry:i}))}catch(t){}}(i,n),n)};class a{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u;return{event:"Checkout Started",properties:{checkout_id:null==(t=window.initialData)||null==(t=t.cart)?void 0:t.id,order_id:(null==(e=this.transaction)||null==(e=e.order_id)?void 0:e.toString())||"",cart_id:(null==(s=this.transaction)||null==(s=s.cart_id)?void 0:s.toString())||"",affiliation:(null==(i=this.transaction)?void 0:i.affiliation)||"",value:(null==(n=this.transaction)?void 0:n.value)||0,revenue:(null==(r=this.transaction)?void 0:r.revenue)||0,shipping:(null==(o=this.transaction)?void 0:o.shipping)||0,tax:(null==(a=this.transaction)?void 0:a.tax)||0,discount:(null==(d=this.transaction)?void 0:d.discount)||0,coupon:(null==(l=this.transaction)?void 0:l.coupon)||"",currency:(null==(c=this.transaction)?void 0:c.currency)||"",products:((null==(u=this.transaction)?void 0:u.products)||[]).map(t=>{var e;return{product_id:(null==t||null==(e=t.product_id)?void 0:e.toString())||"",sku:(null==t?void 0:t.sku)||"",name:(null==t?void 0:t.name)||"",price:(null==t?void 0:t.price)||0,quantity:(null==t?void 0:t.quantity)||0,category:(null==t?void 0:t.category)||"",url:(null==t?void 0:t.url)||"",image_url:(null==t?void 0:t.image_url)||""}})}}}}class d{constructor(t){this.transaction=t}toSegment(){return{event:"Payment Info Entered",properties:{order_id:this.transaction.order_id,payment_method:this.transaction.payment_method,checkout_id:this.transaction.checkout_id}}}}class l{constructor(t){this.details=t||{}}toSegment(){var e;return{event:"Checkout Step Viewed",properties:t._extends({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:null!=(e=this.details.payment_method)?e:""},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}const c=/\b(?:\d[ -]?){13,19}\b/g,u=/[\w.+-]+@[\w-]+(\.[\w-]+)+/g,h=t=>t?String(t).replace(c,"[REDACTED]").replace(u,"[REDACTED]").slice(0,500):"",p=t=>t&&"object"==typeof t?Object.keys(t).map(e=>{const s=Array.isArray(t[e])?t[e][0]:t[e],i=s&&"object"==typeof s?s.message||JSON.stringify(s):s;return{field:e,message:h(i)}}):[];class _{constructor(t){this.details=t||{}}toSegment(){var e,s,i,n;const r=this.details.error||{},o=r.fields||{},a=Object.keys(o);return{event:"Checkout Step Completed",properties:t._extends({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:this.details.payment_method,success:null==(e=this.details.success)||e,error_code:null!=(s=r.code)?s:null,error_type:null!=(i=r.type)?i:null,error_message:h(r.message),http_status:null!=(n=r.status)?n:null,error_fields:a,field_errors:p(o)},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}class m{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Entered",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,value:this.transaction.value,currency:this.transaction.currency}}}}class y{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Applied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class g{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Denied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,reason:this.transaction.reason}}}}class v{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Removed",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class f{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u,_,m,y,g,v,f,k,S,C,x,b,w;const T=this.transaction.error||{},E=T.fields||{},P=Object.keys(E),j=null==(t=this.transaction.address)?void 0:t.lat,A=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(j)||Boolean(A);return{event:"Address Added",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(d=this.transaction.address)?void 0:d.local,street:null==(l=this.transaction.address)?void 0:l.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(_=this.transaction.address)||null==(_=_.region)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.region)?void 0:m.name},city:{id:null==(y=this.transaction.address)||null==(y=y.city)?void 0:y.id,name:null==(g=this.transaction.address)||null==(g=g.city)?void 0:g.name},country:{id:null==(v=this.transaction.address)||null==(v=v.country)?void 0:v.id,name:null==(f=this.transaction.address)||null==(f=f.country)?void 0:f.name},district:{id:null==(k=this.transaction.address)||null==(k=k.district)?void 0:k.id,name:null==(S=this.transaction.address)||null==(S=S.district)?void 0:S.name},lat:I&&null!=j?j:null,lng:I&&null!=A?A:null},success:null==(C=this.transaction.success)||C,error_code:null!=(x=T.code)?x:null,error_type:null!=(b=T.type)?b:null,error_message:h(T.message),http_status:null!=(w=T.status)?w:null,error_fields:P,field_errors:p(E)}}}}class k{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u,_,m,y,g,v,f,k,S,C,x,b,w;const T=this.transaction.error||{},E=T.fields||{},P=Object.keys(E),j=null==(t=this.transaction.address)?void 0:t.lat,A=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(j)||Boolean(A);return{event:"Address Updated",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(d=this.transaction.address)?void 0:d.local,street:null==(l=this.transaction.address)?void 0:l.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(_=this.transaction.address)||null==(_=_.region)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.region)?void 0:m.name},city:{id:null==(y=this.transaction.address)||null==(y=y.city)?void 0:y.id,name:null==(g=this.transaction.address)||null==(g=g.city)?void 0:g.name},country:{id:null==(v=this.transaction.address)||null==(v=v.country)?void 0:v.id,name:null==(f=this.transaction.address)||null==(f=f.country)?void 0:f.name},district:{id:null==(k=this.transaction.address)||null==(k=k.district)?void 0:k.id,name:null==(S=this.transaction.address)||null==(S=S.district)?void 0:S.name},lat:I&&null!=j?j:null,lng:I&&null!=A?A:null},success:null==(C=this.transaction.success)||C,error_code:null!=(x=T.code)?x:null,error_type:null!=(b=T.type)?b:null,error_message:h(T.message),http_status:null!=(w=T.status)?w:null,error_fields:P,field_errors:p(E)}}}}class S{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u;const _=this.transaction.error||{},m=_.fields||{},y=Object.keys(m);return{event:"Map Clicked",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(t=this.transaction.customer)?void 0:t.id,name:(null==(e=this.transaction.customer)?void 0:e.first_name)+" "+(null==(s=this.transaction.customer)?void 0:s.last_name),mobile:null==(i=this.transaction.customer)?void 0:i.mobile,email:null==(n=this.transaction.customer)?void 0:n.email,gender:null==(r=this.transaction.customer)?void 0:r.gender},address:{lat:null==(o=this.transaction.address)?void 0:o.lat,lng:null==(a=this.transaction.address)?void 0:a.lng},success:null==(d=this.transaction.success)||d,error_code:null!=(l=_.code)?l:null,error_type:null!=(c=_.type)?c:null,error_message:h(_.message),http_status:null!=(u=_.status)?u:null,error_fields:y,field_errors:p(m)}}}}class C{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Submitted",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,is_order_free:this.details.is_order_free,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0),shipping_method:this.details.shipping_method,has_partial_payment:this.details.has_partial_payment||!1}}}}class x{constructor(t){this.details=t||{}}toSegment(){const e=this.details.error_fields||{},i=e&&"object"==typeof e?Object.keys(e):[];return{event:"Payment Failed",properties:t._extends({checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,error_code:this.details.error_code,error_type:this.details.error_type,error_message:h(this.details.error_message),http_status:this.details.http_status,error_fields:i,field_errors:p(e)},b(this.details.apple_pay))}}}const b=t=>t&&"object"==typeof t?{apple_pay_cancel_stage:t.cancel_stage||null,apple_pay_elapsed_ms:Number.isFinite(t.elapsed_ms)?t.elapsed_ms:null,apple_pay_validation_result:t.validation_result||null,apple_pay_status_code:t.apple_status_code?String(t.apple_status_code):null,apple_pay_status_message:h(t.apple_status_message)||null,apple_pay_submit_state:t.submit_state||null,apple_pay_authorize_count:Number.isFinite(t.authorize_count)?t.authorize_count:null}:{};class w{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Succeeded",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,response_status:this.details.response_status,has_redirect:this.details.has_redirect||!1,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0)}}}}class T{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Pending",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0)}}}}class E{constructor(t){this.details=t||{}}toSegment(){return{event:"Gift Card Redeemed",properties:t.mapGiftCardRedeemedFields({source:"checkout",customer_id:s.get("customer.id"),store_id:s.get("store.id")},this.details)}}}var P=new class{constructor(){this.events={"Checkout Started":a,"Payment Info Entered":d,"Checkout Step Viewed":l,"Checkout Step Completed":_,"Coupon Entered":m,"Coupon Applied":y,"Coupon Denied":g,"Coupon Removed":v,"Address Added":f,"Address Updated":k,"Map Clicked":S,"Payment Submitted":C,"Payment Failed":x,"Payment Succeeded":w,"Payment Pending":T,"Gift Card Redeemed":E}}transform(t,e){const s=this.events[t];if(!s)return null;try{const t=new s(e).toSegment();return null!=t&&t.properties&&null!=e&&e.checkout_type&&(t.properties.checkout_type=e.checkout_type),t}catch(t){return console.error("Failed to transform event:",t),null}}};exports.SessionService=o,exports.default=class{constructor(){this.history=[],this.customTrackers=[],this.context={},this.events=[],this._sessionId=null,this.mappedEvents={"payment::method_updated":"Payment Info Entered","checkout::step_viewed":"Checkout Step Viewed","checkout::step_completed":"Checkout Step Completed","coupon::applied":"Coupon Applied","coupon::denied":"Coupon Denied","coupon::entered":"Coupon Entered","coupon::removed":"Coupon Removed","address::added":"Address Added","address::updated":"Address Updated","map::clicked":"Map Clicked","payment::submitted":"Payment Submitted","payment::failed":"Payment Failed","payment::succeeded":"Payment Succeeded","payment::pending":"Payment Pending","giftcard::redeemed":"Gift Card Redeemed"};const t=s.get("config.jitsu",{}),i=s.get("store.url",null),{host:n,key:r}=t||{};this.analytics=null,n&&r&&(this.analytics=e.jitsuAnalytics({host:this.getAnalyticsHost(i),writeKey:r,cookieCapture:this.captureCookies()}),this.events=s.get("config.jitsu.events",[]),s.get("config.jitsu.context.extra",{})&&this.setExtraContext(s.get("config.jitsu.context.extra",{})),this.initSessionId(),this.setSignedInCustomerTraits(),this.page())}setSignedInCustomerTraits(){const t=s.get("customer");t&&t.id&&this.setTraits(t)}setTraits(t){t&&this.setContext("traits",t)}initSessionId(){this._sessionId=o.resolve(),this.setContext("session_id",this._sessionId)}captureCookies(){const e=s.get("store.features",[])||[],i=e.includes("utm-client-ids")&&this.hasCookiesConsent(e);return t.captureSallaCookies("sa_",i)}hasCookiesConsent(t){if(!t.includes("website-cookies"))return!0;try{return"accepted"===this.readCookiesConsent()&&/(?:^|;\s*)advertising_cookies=true(?:;|$)/.test(document.cookie)}catch(t){return!1}}readCookiesConsent(){var t;return null!=(t=globalThis.Salla)&&t.storage?globalThis.Salla.storage.get("cookies-consent"):JSON.parse(localStorage.getItem("cookies-consent"))}getAnalyticsHost(e){var s;return t.resolveAnalyticsHost(e,{documentOrigin:"undefined"!=typeof window?null==(s=window.location)?void 0:s.origin:null,log:console.log})}trackSallaEvent(t){var e;if("customer::identified"===t.type)return this.setTraits(t.detail);const s=null!=(e=this.mappedEvents[t.type])?e:t.type;if(this.analytics&&this.events.includes(s))try{if(!s)return;const e=P.transform(s,t.detail);e&&this.track(e.event,e.properties)}catch(t){console.log(t)}}track(t,e={}){return this.analytics?(e=this.finalPayload(e),this.analytics.track(t,e).then(s=>(this.pushEventToCustomTrackers("track",t,e),s)).then(()=>{this.history.push({action:"track",eventName:t,payload:e})}).catch(t=>Promise.reject(t))):Promise.resolve()}page(t={}){if(!this.analytics)return Promise.resolve();const e=this.finalPayload(t);return this.analytics.page(e).then(t=>(this.pushEventToCustomTrackers("page",e),t)).then(()=>{this.history.push({action:"page",payload:e})}).catch(t=>Promise.reject(t))}finalPayload(e={}){var i;let n={session_id:o.resolve(),cart_id:s.get("cart.id")||e.cart_id||e.checkout_id||null,requires_shipping:Boolean(s.get("cart.requires_shipping",!1))};return this.setContext("version",s.get("config.jitsu.version",1)),this.setContext("store_id",s.get("config.store.id")),n.session_id&&this.setContext("session_id",n.session_id),e.event_id=e.event_id||t.generateSessionId(),t._extends({},n,e,{session_id:e.session_id||n.session_id,cart_id:e.cart_id||n.cart_id,requires_shipping:null!=(i=e.requires_shipping)?i:n.requires_shipping})}registerTracker(e){t.isValidTracker(e,this.customTrackers,console.log)&&(this.customTrackers.push(e),t.replayHistory(e,this.history))}pushEventToCustomTrackers(e,s,i={}){return t.pushEventToTrackers(this.customTrackers,e,s,i,console.log)}setContext(t,e){var s;null==(s=this.analytics)||s.setContextProperty(t,e),this.context[t]=e}setExtraContext(e){t.applyExtraContext((t,e)=>this.setContext(t,e),e,console.log)}},exports.initialData=s,exports.sanitizeErrorMessage=h,exports.sanitizeFieldErrors=p;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("./chunks/extra-context-B8aoGbHo.js"),e=require("@jitsu/js"),s={get(t,e=null){const s="undefined"!=typeof window?window.initialData:null;if(!s||"object"!=typeof s)return e;let i=s;for(const s of t.split(".")){if(null==i||"object"!=typeof i||!(s in i))return e;i=i[s]}return void 0!==i?i:e}};const i="s-session-id";let n=null;function r(t){return`${t}_${s.get("store.id")}`}var o={SESSION_STORAGE_KEY:i,SESSION_TTL_MINUTES:30,resolve:()=>(n||(n=function(t,e=null){try{const s=localStorage.getItem(r(t));if(!s)return e;const i=JSON.parse(s);return i&&"object"==typeof i?(new Date).getTime()>i.expiry?(localStorage.removeItem(r(t)),e):i.value:e}catch(t){return e}}(i)||t.generateSessionId()),function(t,e,s=30){try{const i=(new Date).getTime()+60*s*1e3;localStorage.setItem(r(t),JSON.stringify({value:e,expiry:i}))}catch(t){}}(i,n),n)};class a{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u;return{event:"Checkout Started",properties:{checkout_id:null==(t=window.initialData)||null==(t=t.cart)?void 0:t.id,order_id:(null==(e=this.transaction)||null==(e=e.order_id)?void 0:e.toString())||"",cart_id:(null==(s=this.transaction)||null==(s=s.cart_id)?void 0:s.toString())||"",affiliation:(null==(i=this.transaction)?void 0:i.affiliation)||"",value:(null==(n=this.transaction)?void 0:n.value)||0,revenue:(null==(r=this.transaction)?void 0:r.revenue)||0,shipping:(null==(o=this.transaction)?void 0:o.shipping)||0,tax:(null==(a=this.transaction)?void 0:a.tax)||0,discount:(null==(l=this.transaction)?void 0:l.discount)||0,coupon:(null==(d=this.transaction)?void 0:d.coupon)||"",currency:(null==(c=this.transaction)?void 0:c.currency)||"",products:((null==(u=this.transaction)?void 0:u.products)||[]).map(t=>{var e;return{product_id:(null==t||null==(e=t.product_id)?void 0:e.toString())||"",sku:(null==t?void 0:t.sku)||"",name:(null==t?void 0:t.name)||"",price:(null==t?void 0:t.price)||0,quantity:(null==t?void 0:t.quantity)||0,category:(null==t?void 0:t.category)||"",url:(null==t?void 0:t.url)||"",image_url:(null==t?void 0:t.image_url)||""}})}}}}class l{constructor(t){this.transaction=t}toSegment(){return{event:"Payment Info Entered",properties:{order_id:this.transaction.order_id,payment_method:this.transaction.payment_method,checkout_id:this.transaction.checkout_id}}}}class d{constructor(t){this.details=t||{}}toSegment(){var e;return{event:"Checkout Step Viewed",properties:t._extends({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:null!=(e=this.details.payment_method)?e:""},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}const c=/\b(?:\d[ -]?){13,19}\b/g,u=/[\w.+-]+@[\w-]+(\.[\w-]+)+/g,h=t=>t?String(t).replace(c,"[REDACTED]").replace(u,"[REDACTED]").slice(0,500):"",p=t=>t&&"object"==typeof t?Object.keys(t).map(e=>{const s=Array.isArray(t[e])?t[e][0]:t[e],i=s&&"object"==typeof s?s.message||JSON.stringify(s):s;return{field:e,message:h(i)}}):[];class _{constructor(t){this.details=t||{}}toSegment(){var e,s,i,n;const r=this.details.error||{},o=r.fields||{},a=Object.keys(o);return{event:"Checkout Step Completed",properties:t._extends({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:this.details.payment_method,success:null==(e=this.details.success)||e,error_code:null!=(s=r.code)?s:null,error_type:null!=(i=r.type)?i:null,error_message:h(r.message),http_status:null!=(n=r.status)?n:null,error_fields:a,field_errors:p(o)},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}class m{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Entered",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,value:this.transaction.value,currency:this.transaction.currency}}}}class g{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Applied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class v{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Denied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,reason:this.transaction.reason}}}}class y{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Removed",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class k{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u,_,m,g,v,y,k,f,S,C,x,P,w;const b=this.transaction.error||{},T=b.fields||{},E=Object.keys(T),A=null==(t=this.transaction.address)?void 0:t.lat,j=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(A)||Boolean(j);return{event:"Address Added",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(l=this.transaction.address)?void 0:l.local,street:null==(d=this.transaction.address)?void 0:d.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(_=this.transaction.address)||null==(_=_.region)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.region)?void 0:m.name},city:{id:null==(g=this.transaction.address)||null==(g=g.city)?void 0:g.id,name:null==(v=this.transaction.address)||null==(v=v.city)?void 0:v.name},country:{id:null==(y=this.transaction.address)||null==(y=y.country)?void 0:y.id,name:null==(k=this.transaction.address)||null==(k=k.country)?void 0:k.name},district:{id:null==(f=this.transaction.address)||null==(f=f.district)?void 0:f.id,name:null==(S=this.transaction.address)||null==(S=S.district)?void 0:S.name},lat:I&&null!=A?A:null,lng:I&&null!=j?j:null},success:null==(C=this.transaction.success)||C,error_code:null!=(x=b.code)?x:null,error_type:null!=(P=b.type)?P:null,error_message:h(b.message),http_status:null!=(w=b.status)?w:null,error_fields:E,field_errors:p(T)}}}}class f{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u,_,m,g,v,y,k,f,S,C,x,P,w;const b=this.transaction.error||{},T=b.fields||{},E=Object.keys(T),A=null==(t=this.transaction.address)?void 0:t.lat,j=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(A)||Boolean(j);return{event:"Address Updated",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(l=this.transaction.address)?void 0:l.local,street:null==(d=this.transaction.address)?void 0:d.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(_=this.transaction.address)||null==(_=_.region)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.region)?void 0:m.name},city:{id:null==(g=this.transaction.address)||null==(g=g.city)?void 0:g.id,name:null==(v=this.transaction.address)||null==(v=v.city)?void 0:v.name},country:{id:null==(y=this.transaction.address)||null==(y=y.country)?void 0:y.id,name:null==(k=this.transaction.address)||null==(k=k.country)?void 0:k.name},district:{id:null==(f=this.transaction.address)||null==(f=f.district)?void 0:f.id,name:null==(S=this.transaction.address)||null==(S=S.district)?void 0:S.name},lat:I&&null!=A?A:null,lng:I&&null!=j?j:null},success:null==(C=this.transaction.success)||C,error_code:null!=(x=b.code)?x:null,error_type:null!=(P=b.type)?P:null,error_message:h(b.message),http_status:null!=(w=b.status)?w:null,error_fields:E,field_errors:p(T)}}}}class S{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u;const _=this.transaction.error||{},m=_.fields||{},g=Object.keys(m);return{event:"Map Clicked",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(t=this.transaction.customer)?void 0:t.id,name:(null==(e=this.transaction.customer)?void 0:e.first_name)+" "+(null==(s=this.transaction.customer)?void 0:s.last_name),mobile:null==(i=this.transaction.customer)?void 0:i.mobile,email:null==(n=this.transaction.customer)?void 0:n.email,gender:null==(r=this.transaction.customer)?void 0:r.gender},address:{lat:null==(o=this.transaction.address)?void 0:o.lat,lng:null==(a=this.transaction.address)?void 0:a.lng},success:null==(l=this.transaction.success)||l,error_code:null!=(d=_.code)?d:null,error_type:null!=(c=_.type)?c:null,error_message:h(_.message),http_status:null!=(u=_.status)?u:null,error_fields:g,field_errors:p(m)}}}}class C{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Submitted",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,is_order_free:this.details.is_order_free,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0),shipping_method:this.details.shipping_method,has_partial_payment:this.details.has_partial_payment||!1}}}}class x{constructor(t){this.details=t||{}}toSegment(){const e=this.details.error_fields||{},i=e&&"object"==typeof e?Object.keys(e):[];return{event:"Payment Failed",properties:t._extends({checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,error_code:this.details.error_code,error_type:this.details.error_type,error_message:h(this.details.error_message),http_status:this.details.http_status,error_fields:i,field_errors:p(e)},P(this.details.apple_pay))}}}const P=t=>t&&"object"==typeof t?{apple_pay_cancel_stage:t.cancel_stage||null,apple_pay_elapsed_ms:Number.isFinite(t.elapsed_ms)?t.elapsed_ms:null,apple_pay_validation_result:t.validation_result||null,apple_pay_status_code:t.apple_status_code?String(t.apple_status_code):null,apple_pay_status_message:h(t.apple_status_message)||null,apple_pay_submit_state:t.submit_state||null,apple_pay_authorize_count:Number.isFinite(t.authorize_count)?t.authorize_count:null}:{};class w{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Succeeded",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,response_status:this.details.response_status,has_redirect:this.details.has_redirect||!1,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0)}}}}class b{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Pending",properties:{checkout_id:this.details.checkout_id||s.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||s.get("cart.total_in_customer_currency",0)}}}}class T{constructor(t){this.details=t||{}}toSegment(){return{event:"Gift Card Redeemed",properties:t.mapGiftCardRedeemedFields({source:"checkout",customer_id:s.get("customer.id"),store_id:s.get("store.id")},this.details)}}}function E(t,e){var i,n;return{product_id:null==(i=t.id)?void 0:i.toString(),name:t.name,price:t.price,position:e,url:t.url,image_url:null==(n=t.image)?void 0:n.url,currency:t.currency||s.get("cart.currency")||null}}function A(t){var e;return{source:null==t?void 0:t.source,recommendation_source:null==t?void 0:t.recommendation_source,request_id:null!=(e=null==t?void 0:t.request_id)?e:null}}class j{constructor(t){this.details=t}toSegment(){var e;const s=Array.isArray(null==(e=this.details)?void 0:e.products)?this.details.products.filter(Boolean):[];return s.length?{event:"Product List Viewed",properties:t._extends({list_id:"checkout_xsell",category:null,sub_category:null},A(this.details),{products:s.map((e,s)=>t._extends({},E(e,s+1),{variant:"product"}))})}:null}}class I{constructor(t){this.details=t}toSegment(){var e,s;const i=null==(e=this.details)?void 0:e.product;return i?{event:"Product Clicked",properties:t._extends({},E(i,null!=(s=this.details.position)?s:null),A(this.details))}:null}}class O{constructor(t){this.details=t}toSegment(){var e,i,n;const r=null==(e=this.details)?void 0:e.product;return r?{event:"Product Added",properties:t._extends({},E(r,null!=(i=this.details.position)?i:null),{quantity:null!=(n=this.details.quantity)?n:1,cart_id:s.get("cart.id")||null},A(this.details))}:null}}var q=new class{constructor(){this.events={"Checkout Started":a,"Payment Info Entered":l,"Checkout Step Viewed":d,"Checkout Step Completed":_,"Coupon Entered":m,"Coupon Applied":g,"Coupon Denied":v,"Coupon Removed":y,"Address Added":k,"Address Updated":f,"Map Clicked":S,"Payment Submitted":C,"Payment Failed":x,"Payment Succeeded":w,"Payment Pending":b,"Gift Card Redeemed":T,"Product List Viewed":j,"Product Clicked":I,"Product Added":O}}transform(t,e){const s=this.events[t];if(!s)return null;try{const t=new s(e).toSegment();return null!=t&&t.properties&&null!=e&&e.checkout_type&&(t.properties.checkout_type=e.checkout_type),t}catch(t){return console.error("Failed to transform event:",t),null}}};exports.SessionService=o,exports.default=class{constructor(){this.history=[],this.customTrackers=[],this.context={},this.events=[],this._sessionId=null,this.mappedEvents={"payment::method_updated":"Payment Info Entered","checkout::step_viewed":"Checkout Step Viewed","checkout::step_completed":"Checkout Step Completed","coupon::applied":"Coupon Applied","coupon::denied":"Coupon Denied","coupon::entered":"Coupon Entered","coupon::removed":"Coupon Removed","address::added":"Address Added","address::updated":"Address Updated","map::clicked":"Map Clicked","payment::submitted":"Payment Submitted","payment::failed":"Payment Failed","payment::succeeded":"Payment Succeeded","payment::pending":"Payment Pending","giftcard::redeemed":"Gift Card Redeemed","checkout-xsell::list.viewed":"Product List Viewed","checkout-xsell::product.clicked":"Product Clicked","checkout-xsell::product.added":"Product Added"};const t=s.get("config.jitsu",{}),i=s.get("store.url",null),{host:n,key:r}=t||{};this.analytics=null,n&&r&&(this.analytics=e.jitsuAnalytics({host:this.getAnalyticsHost(i),writeKey:r,cookieCapture:this.captureCookies()}),this.events=s.get("config.jitsu.events",[]),s.get("config.jitsu.context.extra",{})&&this.setExtraContext(s.get("config.jitsu.context.extra",{})),this.initSessionId(),this.setSignedInCustomerTraits(),this.page())}setSignedInCustomerTraits(){const t=s.get("customer");t&&t.id&&this.setTraits(t)}setTraits(t){t&&this.setContext("traits",t)}initSessionId(){this._sessionId=o.resolve(),this.setContext("session_id",this._sessionId)}captureCookies(){const e=s.get("store.features",[])||[],i=e.includes("utm-client-ids")&&this.hasCookiesConsent(e);return t.captureSallaCookies("sa_",i)}hasCookiesConsent(t){if(!t.includes("website-cookies"))return!0;try{return"accepted"===this.readCookiesConsent()&&/(?:^|;\s*)advertising_cookies=true(?:;|$)/.test(document.cookie)}catch(t){return!1}}readCookiesConsent(){var t;return null!=(t=globalThis.Salla)&&t.storage?globalThis.Salla.storage.get("cookies-consent"):JSON.parse(localStorage.getItem("cookies-consent"))}getAnalyticsHost(e){var s;return t.resolveAnalyticsHost(e,{documentOrigin:"undefined"!=typeof window?null==(s=window.location)?void 0:s.origin:null,log:console.log})}trackSallaEvent(t){var e;if("customer::identified"===t.type)return this.setTraits(t.detail);const s=null!=(e=this.mappedEvents[t.type])?e:t.type;if(this.analytics&&this.events.includes(s))try{if(!s)return;const e=q.transform(s,t.detail);e&&this.track(e.event,e.properties)}catch(t){console.log(t)}}track(t,e={}){return this.analytics?(e=this.finalPayload(e),this.analytics.track(t,e).then(s=>(this.pushEventToCustomTrackers("track",t,e),s)).then(()=>{this.history.push({action:"track",eventName:t,payload:e})}).catch(t=>Promise.reject(t))):Promise.resolve()}page(t={}){if(!this.analytics)return Promise.resolve();const e=this.finalPayload(t);return this.analytics.page(e).then(t=>(this.pushEventToCustomTrackers("page",e),t)).then(()=>{this.history.push({action:"page",payload:e})}).catch(t=>Promise.reject(t))}finalPayload(e={}){var i;let n={session_id:o.resolve(),cart_id:s.get("cart.id")||e.cart_id||e.checkout_id||null,requires_shipping:Boolean(s.get("cart.requires_shipping",!1))};return this.setContext("version",s.get("config.jitsu.version",1)),this.setContext("store_id",s.get("config.store.id")),n.session_id&&this.setContext("session_id",n.session_id),e.event_id=e.event_id||t.generateSessionId(),t._extends({},n,e,{session_id:e.session_id||n.session_id,cart_id:e.cart_id||n.cart_id,requires_shipping:null!=(i=e.requires_shipping)?i:n.requires_shipping})}registerTracker(e){t.isValidTracker(e,this.customTrackers,console.log)&&(this.customTrackers.push(e),t.replayHistory(e,this.history))}pushEventToCustomTrackers(e,s,i={}){return t.pushEventToTrackers(this.customTrackers,e,s,i,console.log)}setContext(t,e){var s;null==(s=this.analytics)||s.setContextProperty(t,e),this.context[t]=e}setExtraContext(e){t.applyExtraContext((t,e)=>this.setContext(t,e),e,console.log)}},exports.initialData=s,exports.sanitizeErrorMessage=h,exports.sanitizeFieldErrors=p;
|
|
2
2
|
//# sourceMappingURL=checkout.js.map
|
package/dist/esm/checkout.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{g as t,_ as e,m as s,c as i,b as n,i as r,r as o,p as a,a as d}from"./chunks/extra-context-D4VyOqyT.js";import{jitsuAnalytics as l}from"@jitsu/js";var c={get(t,e=null){const s="undefined"!=typeof window?window.initialData:null;if(!s||"object"!=typeof s)return e;let i=s;for(const s of t.split(".")){if(null==i||"object"!=typeof i||!(s in i))return e;i=i[s]}return void 0!==i?i:e}};const u="s-session-id";let h=null;function p(t){return`${t}_${c.get("store.id")}`}var _={SESSION_STORAGE_KEY:u,SESSION_TTL_MINUTES:30,resolve:()=>(h||(h=function(t,e=null){try{const s=localStorage.getItem(p(t));if(!s)return e;const i=JSON.parse(s);return i&&"object"==typeof i?(new Date).getTime()>i.expiry?(localStorage.removeItem(p(t)),e):i.value:e}catch(t){return e}}(u)||t()),function(t,e,s=30){try{const i=(new Date).getTime()+60*s*1e3;localStorage.setItem(p(t),JSON.stringify({value:e,expiry:i}))}catch(t){}}(u,h),h)};class m{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u;return{event:"Checkout Started",properties:{checkout_id:null==(t=window.initialData)||null==(t=t.cart)?void 0:t.id,order_id:(null==(e=this.transaction)||null==(e=e.order_id)?void 0:e.toString())||"",cart_id:(null==(s=this.transaction)||null==(s=s.cart_id)?void 0:s.toString())||"",affiliation:(null==(i=this.transaction)?void 0:i.affiliation)||"",value:(null==(n=this.transaction)?void 0:n.value)||0,revenue:(null==(r=this.transaction)?void 0:r.revenue)||0,shipping:(null==(o=this.transaction)?void 0:o.shipping)||0,tax:(null==(a=this.transaction)?void 0:a.tax)||0,discount:(null==(d=this.transaction)?void 0:d.discount)||0,coupon:(null==(l=this.transaction)?void 0:l.coupon)||"",currency:(null==(c=this.transaction)?void 0:c.currency)||"",products:((null==(u=this.transaction)?void 0:u.products)||[]).map(t=>{var e;return{product_id:(null==t||null==(e=t.product_id)?void 0:e.toString())||"",sku:(null==t?void 0:t.sku)||"",name:(null==t?void 0:t.name)||"",price:(null==t?void 0:t.price)||0,quantity:(null==t?void 0:t.quantity)||0,category:(null==t?void 0:t.category)||"",url:(null==t?void 0:t.url)||"",image_url:(null==t?void 0:t.image_url)||""}})}}}}class g{constructor(t){this.transaction=t}toSegment(){return{event:"Payment Info Entered",properties:{order_id:this.transaction.order_id,payment_method:this.transaction.payment_method,checkout_id:this.transaction.checkout_id}}}}class y{constructor(t){this.details=t||{}}toSegment(){var t;return{event:"Checkout Step Viewed",properties:e({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:null!=(t=this.details.payment_method)?t:""},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}const v=/\b(?:\d[ -]?){13,19}\b/g,f=/[\w.+-]+@[\w-]+(\.[\w-]+)+/g,k=t=>t?String(t).replace(v,"[REDACTED]").replace(f,"[REDACTED]").slice(0,500):"",S=t=>t&&"object"==typeof t?Object.keys(t).map(e=>{const s=Array.isArray(t[e])?t[e][0]:t[e],i=s&&"object"==typeof s?s.message||JSON.stringify(s):s;return{field:e,message:k(i)}}):[];class C{constructor(t){this.details=t||{}}toSegment(){var t,s,i,n;const r=this.details.error||{},o=r.fields||{},a=Object.keys(o);return{event:"Checkout Step Completed",properties:e({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:this.details.payment_method,success:null==(t=this.details.success)||t,error_code:null!=(s=r.code)?s:null,error_type:null!=(i=r.type)?i:null,error_message:k(r.message),http_status:null!=(n=r.status)?n:null,error_fields:a,field_errors:S(o)},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}class b{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Entered",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,value:this.transaction.value,currency:this.transaction.currency}}}}class w{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Applied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class P{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Denied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,reason:this.transaction.reason}}}}class T{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Removed",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class x{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u,h,p,_,m,g,y,v,f,C,b,w,P;const T=this.transaction.error||{},x=T.fields||{},j=Object.keys(x),E=null==(t=this.transaction.address)?void 0:t.lat,A=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(E)||Boolean(A);return{event:"Address Added",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(d=this.transaction.address)?void 0:d.local,street:null==(l=this.transaction.address)?void 0:l.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(h=this.transaction.address)||null==(h=h.region)?void 0:h.id,name:null==(p=this.transaction.address)||null==(p=p.region)?void 0:p.name},city:{id:null==(_=this.transaction.address)||null==(_=_.city)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.city)?void 0:m.name},country:{id:null==(g=this.transaction.address)||null==(g=g.country)?void 0:g.id,name:null==(y=this.transaction.address)||null==(y=y.country)?void 0:y.name},district:{id:null==(v=this.transaction.address)||null==(v=v.district)?void 0:v.id,name:null==(f=this.transaction.address)||null==(f=f.district)?void 0:f.name},lat:I&&null!=E?E:null,lng:I&&null!=A?A:null},success:null==(C=this.transaction.success)||C,error_code:null!=(b=T.code)?b:null,error_type:null!=(w=T.type)?w:null,error_message:k(T.message),http_status:null!=(P=T.status)?P:null,error_fields:j,field_errors:S(x)}}}}class j{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u,h,p,_,m,g,y,v,f,C,b,w,P;const T=this.transaction.error||{},x=T.fields||{},j=Object.keys(x),E=null==(t=this.transaction.address)?void 0:t.lat,A=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(E)||Boolean(A);return{event:"Address Updated",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(d=this.transaction.address)?void 0:d.local,street:null==(l=this.transaction.address)?void 0:l.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(h=this.transaction.address)||null==(h=h.region)?void 0:h.id,name:null==(p=this.transaction.address)||null==(p=p.region)?void 0:p.name},city:{id:null==(_=this.transaction.address)||null==(_=_.city)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.city)?void 0:m.name},country:{id:null==(g=this.transaction.address)||null==(g=g.country)?void 0:g.id,name:null==(y=this.transaction.address)||null==(y=y.country)?void 0:y.name},district:{id:null==(v=this.transaction.address)||null==(v=v.district)?void 0:v.id,name:null==(f=this.transaction.address)||null==(f=f.district)?void 0:f.name},lat:I&&null!=E?E:null,lng:I&&null!=A?A:null},success:null==(C=this.transaction.success)||C,error_code:null!=(b=T.code)?b:null,error_type:null!=(w=T.type)?w:null,error_message:k(T.message),http_status:null!=(P=T.status)?P:null,error_fields:j,field_errors:S(x)}}}}class E{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,d,l,c,u;const h=this.transaction.error||{},p=h.fields||{},_=Object.keys(p);return{event:"Map Clicked",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(t=this.transaction.customer)?void 0:t.id,name:(null==(e=this.transaction.customer)?void 0:e.first_name)+" "+(null==(s=this.transaction.customer)?void 0:s.last_name),mobile:null==(i=this.transaction.customer)?void 0:i.mobile,email:null==(n=this.transaction.customer)?void 0:n.email,gender:null==(r=this.transaction.customer)?void 0:r.gender},address:{lat:null==(o=this.transaction.address)?void 0:o.lat,lng:null==(a=this.transaction.address)?void 0:a.lng},success:null==(d=this.transaction.success)||d,error_code:null!=(l=h.code)?l:null,error_type:null!=(c=h.type)?c:null,error_message:k(h.message),http_status:null!=(u=h.status)?u:null,error_fields:_,field_errors:S(p)}}}}class A{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Submitted",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,is_order_free:this.details.is_order_free,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0),shipping_method:this.details.shipping_method,has_partial_payment:this.details.has_partial_payment||!1}}}}class I{constructor(t){this.details=t||{}}toSegment(){const t=this.details.error_fields||{},s=t&&"object"==typeof t?Object.keys(t):[];return{event:"Payment Failed",properties:e({checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,error_code:this.details.error_code,error_type:this.details.error_type,error_message:k(this.details.error_message),http_status:this.details.http_status,error_fields:s,field_errors:S(t)},O(this.details.apple_pay))}}}const O=t=>t&&"object"==typeof t?{apple_pay_cancel_stage:t.cancel_stage||null,apple_pay_elapsed_ms:Number.isFinite(t.elapsed_ms)?t.elapsed_ms:null,apple_pay_validation_result:t.validation_result||null,apple_pay_status_code:t.apple_status_code?String(t.apple_status_code):null,apple_pay_status_message:k(t.apple_status_message)||null,apple_pay_submit_state:t.submit_state||null,apple_pay_authorize_count:Number.isFinite(t.authorize_count)?t.authorize_count:null}:{};class D{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Succeeded",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,response_status:this.details.response_status,has_redirect:this.details.has_redirect||!1,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0)}}}}class N{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Pending",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0)}}}}class R{constructor(t){this.details=t||{}}toSegment(){return{event:"Gift Card Redeemed",properties:s({source:"checkout",customer_id:c.get("customer.id"),store_id:c.get("store.id")},this.details)}}}var q=new class{constructor(){this.events={"Checkout Started":m,"Payment Info Entered":g,"Checkout Step Viewed":y,"Checkout Step Completed":C,"Coupon Entered":b,"Coupon Applied":w,"Coupon Denied":P,"Coupon Removed":T,"Address Added":x,"Address Updated":j,"Map Clicked":E,"Payment Submitted":A,"Payment Failed":I,"Payment Succeeded":D,"Payment Pending":N,"Gift Card Redeemed":R}}transform(t,e){const s=this.events[t];if(!s)return null;try{const t=new s(e).toSegment();return null!=t&&t.properties&&null!=e&&e.checkout_type&&(t.properties.checkout_type=e.checkout_type),t}catch(t){return console.error("Failed to transform event:",t),null}}};class F{constructor(){this.history=[],this.customTrackers=[],this.context={},this.events=[],this._sessionId=null,this.mappedEvents={"payment::method_updated":"Payment Info Entered","checkout::step_viewed":"Checkout Step Viewed","checkout::step_completed":"Checkout Step Completed","coupon::applied":"Coupon Applied","coupon::denied":"Coupon Denied","coupon::entered":"Coupon Entered","coupon::removed":"Coupon Removed","address::added":"Address Added","address::updated":"Address Updated","map::clicked":"Map Clicked","payment::submitted":"Payment Submitted","payment::failed":"Payment Failed","payment::succeeded":"Payment Succeeded","payment::pending":"Payment Pending","giftcard::redeemed":"Gift Card Redeemed"};const t=c.get("config.jitsu",{}),e=c.get("store.url",null),{host:s,key:i}=t||{};this.analytics=null,s&&i&&(this.analytics=l({host:this.getAnalyticsHost(e),writeKey:i,cookieCapture:this.captureCookies()}),this.events=c.get("config.jitsu.events",[]),c.get("config.jitsu.context.extra",{})&&this.setExtraContext(c.get("config.jitsu.context.extra",{})),this.initSessionId(),this.setSignedInCustomerTraits(),this.page())}setSignedInCustomerTraits(){const t=c.get("customer");t&&t.id&&this.setTraits(t)}setTraits(t){t&&this.setContext("traits",t)}initSessionId(){this._sessionId=_.resolve(),this.setContext("session_id",this._sessionId)}captureCookies(){const t=c.get("store.features",[])||[],e=t.includes("utm-client-ids")&&this.hasCookiesConsent(t);return i("sa_",e)}hasCookiesConsent(t){if(!t.includes("website-cookies"))return!0;try{return"accepted"===this.readCookiesConsent()&&/(?:^|;\s*)advertising_cookies=true(?:;|$)/.test(document.cookie)}catch(t){return!1}}readCookiesConsent(){var t;return null!=(t=globalThis.Salla)&&t.storage?globalThis.Salla.storage.get("cookies-consent"):JSON.parse(localStorage.getItem("cookies-consent"))}getAnalyticsHost(t){var e;return n(t,{documentOrigin:"undefined"!=typeof window?null==(e=window.location)?void 0:e.origin:null,log:console.log})}trackSallaEvent(t){var e;if("customer::identified"===t.type)return this.setTraits(t.detail);const s=null!=(e=this.mappedEvents[t.type])?e:t.type;if(this.analytics&&this.events.includes(s))try{if(!s)return;const e=q.transform(s,t.detail);e&&this.track(e.event,e.properties)}catch(t){console.log(t)}}track(t,e={}){return this.analytics?(e=this.finalPayload(e),this.analytics.track(t,e).then(s=>(this.pushEventToCustomTrackers("track",t,e),s)).then(()=>{this.history.push({action:"track",eventName:t,payload:e})}).catch(t=>Promise.reject(t))):Promise.resolve()}page(t={}){if(!this.analytics)return Promise.resolve();const e=this.finalPayload(t);return this.analytics.page(e).then(t=>(this.pushEventToCustomTrackers("page",e),t)).then(()=>{this.history.push({action:"page",payload:e})}).catch(t=>Promise.reject(t))}finalPayload(s={}){var i;let n={session_id:_.resolve(),cart_id:c.get("cart.id")||s.cart_id||s.checkout_id||null,requires_shipping:Boolean(c.get("cart.requires_shipping",!1))};return this.setContext("version",c.get("config.jitsu.version",1)),this.setContext("store_id",c.get("config.store.id")),n.session_id&&this.setContext("session_id",n.session_id),s.event_id=s.event_id||t(),e({},n,s,{session_id:s.session_id||n.session_id,cart_id:s.cart_id||n.cart_id,requires_shipping:null!=(i=s.requires_shipping)?i:n.requires_shipping})}registerTracker(t){r(t,this.customTrackers,console.log)&&(this.customTrackers.push(t),o(t,this.history))}pushEventToCustomTrackers(t,e,s={}){return a(this.customTrackers,t,e,s,console.log)}setContext(t,e){var s;null==(s=this.analytics)||s.setContextProperty(t,e),this.context[t]=e}setExtraContext(t){d((t,e)=>this.setContext(t,e),t,console.log)}}export{_ as SessionService,F as default,c as initialData,k as sanitizeErrorMessage,S as sanitizeFieldErrors};
|
|
1
|
+
import{g as t,_ as e,m as s,c as i,b as n,i as r,r as o,p as a,a as l}from"./chunks/extra-context-D4VyOqyT.js";import{jitsuAnalytics as d}from"@jitsu/js";var c={get(t,e=null){const s="undefined"!=typeof window?window.initialData:null;if(!s||"object"!=typeof s)return e;let i=s;for(const s of t.split(".")){if(null==i||"object"!=typeof i||!(s in i))return e;i=i[s]}return void 0!==i?i:e}};const u="s-session-id";let h=null;function p(t){return`${t}_${c.get("store.id")}`}var _={SESSION_STORAGE_KEY:u,SESSION_TTL_MINUTES:30,resolve:()=>(h||(h=function(t,e=null){try{const s=localStorage.getItem(p(t));if(!s)return e;const i=JSON.parse(s);return i&&"object"==typeof i?(new Date).getTime()>i.expiry?(localStorage.removeItem(p(t)),e):i.value:e}catch(t){return e}}(u)||t()),function(t,e,s=30){try{const i=(new Date).getTime()+60*s*1e3;localStorage.setItem(p(t),JSON.stringify({value:e,expiry:i}))}catch(t){}}(u,h),h)};class m{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u;return{event:"Checkout Started",properties:{checkout_id:null==(t=window.initialData)||null==(t=t.cart)?void 0:t.id,order_id:(null==(e=this.transaction)||null==(e=e.order_id)?void 0:e.toString())||"",cart_id:(null==(s=this.transaction)||null==(s=s.cart_id)?void 0:s.toString())||"",affiliation:(null==(i=this.transaction)?void 0:i.affiliation)||"",value:(null==(n=this.transaction)?void 0:n.value)||0,revenue:(null==(r=this.transaction)?void 0:r.revenue)||0,shipping:(null==(o=this.transaction)?void 0:o.shipping)||0,tax:(null==(a=this.transaction)?void 0:a.tax)||0,discount:(null==(l=this.transaction)?void 0:l.discount)||0,coupon:(null==(d=this.transaction)?void 0:d.coupon)||"",currency:(null==(c=this.transaction)?void 0:c.currency)||"",products:((null==(u=this.transaction)?void 0:u.products)||[]).map(t=>{var e;return{product_id:(null==t||null==(e=t.product_id)?void 0:e.toString())||"",sku:(null==t?void 0:t.sku)||"",name:(null==t?void 0:t.name)||"",price:(null==t?void 0:t.price)||0,quantity:(null==t?void 0:t.quantity)||0,category:(null==t?void 0:t.category)||"",url:(null==t?void 0:t.url)||"",image_url:(null==t?void 0:t.image_url)||""}})}}}}class g{constructor(t){this.transaction=t}toSegment(){return{event:"Payment Info Entered",properties:{order_id:this.transaction.order_id,payment_method:this.transaction.payment_method,checkout_id:this.transaction.checkout_id}}}}class v{constructor(t){this.details=t||{}}toSegment(){var t;return{event:"Checkout Step Viewed",properties:e({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:null!=(t=this.details.payment_method)?t:""},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}const y=/\b(?:\d[ -]?){13,19}\b/g,k=/[\w.+-]+@[\w-]+(\.[\w-]+)+/g,f=t=>t?String(t).replace(y,"[REDACTED]").replace(k,"[REDACTED]").slice(0,500):"",S=t=>t&&"object"==typeof t?Object.keys(t).map(e=>{const s=Array.isArray(t[e])?t[e][0]:t[e],i=s&&"object"==typeof s?s.message||JSON.stringify(s):s;return{field:e,message:f(i)}}):[];class C{constructor(t){this.details=t||{}}toSegment(){var t,s,i,n;const r=this.details.error||{},o=r.fields||{},a=Object.keys(o);return{event:"Checkout Step Completed",properties:e({checkout_id:this.details.checkout_id,step:this.details.step,shipping_method:this.details.shipping_method,payment_method:this.details.payment_method,success:null==(t=this.details.success)||t,error_code:null!=(s=r.code)?s:null,error_type:null!=(i=r.type)?i:null,error_message:f(r.message),http_status:null!=(n=r.status)?n:null,error_fields:a,field_errors:S(o)},this.details.payment_methods?{payment_methods:this.details.payment_methods}:{})}}}class w{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Entered",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,value:this.transaction.value,currency:this.transaction.currency}}}}class P{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Applied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class b{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Denied",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,reason:this.transaction.reason}}}}class x{constructor(t){this.transaction=t}toSegment(){return{event:"Coupon Removed",properties:{cart_id:this.transaction.cart_id,order_id:this.transaction.order_id,coupon_id:this.transaction.coupon_id,coupon_name:this.transaction.coupon_name,discount:this.transaction.discount,currency:this.transaction.currency}}}}class T{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u,h,p,_,m,g,v,y,k,C,w,P,b;const x=this.transaction.error||{},T=x.fields||{},A=Object.keys(T),j=null==(t=this.transaction.address)?void 0:t.lat,E=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(j)||Boolean(E);return{event:"Address Added",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(l=this.transaction.address)?void 0:l.local,street:null==(d=this.transaction.address)?void 0:d.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(h=this.transaction.address)||null==(h=h.region)?void 0:h.id,name:null==(p=this.transaction.address)||null==(p=p.region)?void 0:p.name},city:{id:null==(_=this.transaction.address)||null==(_=_.city)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.city)?void 0:m.name},country:{id:null==(g=this.transaction.address)||null==(g=g.country)?void 0:g.id,name:null==(v=this.transaction.address)||null==(v=v.country)?void 0:v.name},district:{id:null==(y=this.transaction.address)||null==(y=y.district)?void 0:y.id,name:null==(k=this.transaction.address)||null==(k=k.district)?void 0:k.name},lat:I&&null!=j?j:null,lng:I&&null!=E?E:null},success:null==(C=this.transaction.success)||C,error_code:null!=(w=x.code)?w:null,error_type:null!=(P=x.type)?P:null,error_message:f(x.message),http_status:null!=(b=x.status)?b:null,error_fields:A,field_errors:S(T)}}}}class A{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u,h,p,_,m,g,v,y,k,C,w,P,b;const x=this.transaction.error||{},T=x.fields||{},A=Object.keys(T),j=null==(t=this.transaction.address)?void 0:t.lat,E=null==(e=this.transaction.address)?void 0:e.lng,I=Boolean(j)||Boolean(E);return{event:"Address Updated",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(s=this.transaction.customer)?void 0:s.id,name:(null==(i=this.transaction.customer)?void 0:i.first_name)+" "+(null==(n=this.transaction.customer)?void 0:n.last_name),mobile:null==(r=this.transaction.customer)?void 0:r.mobile,email:null==(o=this.transaction.customer)?void 0:o.email,gender:null==(a=this.transaction.customer)?void 0:a.gender},address:{local:null==(l=this.transaction.address)?void 0:l.local,street:null==(d=this.transaction.address)?void 0:d.street,postal_code:null==(c=this.transaction.address)?void 0:c.postal_code,short_address:null==(u=this.transaction.address)?void 0:u.short_address,region:{id:null==(h=this.transaction.address)||null==(h=h.region)?void 0:h.id,name:null==(p=this.transaction.address)||null==(p=p.region)?void 0:p.name},city:{id:null==(_=this.transaction.address)||null==(_=_.city)?void 0:_.id,name:null==(m=this.transaction.address)||null==(m=m.city)?void 0:m.name},country:{id:null==(g=this.transaction.address)||null==(g=g.country)?void 0:g.id,name:null==(v=this.transaction.address)||null==(v=v.country)?void 0:v.name},district:{id:null==(y=this.transaction.address)||null==(y=y.district)?void 0:y.id,name:null==(k=this.transaction.address)||null==(k=k.district)?void 0:k.name},lat:I&&null!=j?j:null,lng:I&&null!=E?E:null},success:null==(C=this.transaction.success)||C,error_code:null!=(w=x.code)?w:null,error_type:null!=(P=x.type)?P:null,error_message:f(x.message),http_status:null!=(b=x.status)?b:null,error_fields:A,field_errors:S(T)}}}}class j{constructor(t){this.transaction=t}toSegment(){var t,e,s,i,n,r,o,a,l,d,c,u;const h=this.transaction.error||{},p=h.fields||{},_=Object.keys(p);return{event:"Map Clicked",properties:{checkout_id:this.transaction.cart_id,order_id:this.transaction.order_id,cart_id:this.transaction.cart_id,customer:{id:null==(t=this.transaction.customer)?void 0:t.id,name:(null==(e=this.transaction.customer)?void 0:e.first_name)+" "+(null==(s=this.transaction.customer)?void 0:s.last_name),mobile:null==(i=this.transaction.customer)?void 0:i.mobile,email:null==(n=this.transaction.customer)?void 0:n.email,gender:null==(r=this.transaction.customer)?void 0:r.gender},address:{lat:null==(o=this.transaction.address)?void 0:o.lat,lng:null==(a=this.transaction.address)?void 0:a.lng},success:null==(l=this.transaction.success)||l,error_code:null!=(d=h.code)?d:null,error_type:null!=(c=h.type)?c:null,error_message:f(h.message),http_status:null!=(u=h.status)?u:null,error_fields:_,field_errors:S(p)}}}}class E{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Submitted",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,is_order_free:this.details.is_order_free,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0),shipping_method:this.details.shipping_method,has_partial_payment:this.details.has_partial_payment||!1}}}}class I{constructor(t){this.details=t||{}}toSegment(){const t=this.details.error_fields||{},s=t&&"object"==typeof t?Object.keys(t):[];return{event:"Payment Failed",properties:e({checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,error_code:this.details.error_code,error_type:this.details.error_type,error_message:f(this.details.error_message),http_status:this.details.http_status,error_fields:s,field_errors:S(t)},O(this.details.apple_pay))}}}const O=t=>t&&"object"==typeof t?{apple_pay_cancel_stage:t.cancel_stage||null,apple_pay_elapsed_ms:Number.isFinite(t.elapsed_ms)?t.elapsed_ms:null,apple_pay_validation_result:t.validation_result||null,apple_pay_status_code:t.apple_status_code?String(t.apple_status_code):null,apple_pay_status_message:f(t.apple_status_message)||null,apple_pay_submit_state:t.submit_state||null,apple_pay_authorize_count:Number.isFinite(t.authorize_count)?t.authorize_count:null}:{};class q{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Succeeded",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,response_status:this.details.response_status,has_redirect:this.details.has_redirect||!1,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0)}}}}class D{constructor(t){this.details=t||{}}toSegment(){return{event:"Payment Pending",properties:{checkout_id:this.details.checkout_id||c.get("cart.id"),payment_method:this.details.payment_method,gateway:this.details.gateway,redirect_url:this.details.redirect_url||null,order_id:this.details.order_id||null,cart_total:this.details.cart_total||c.get("cart.total_in_customer_currency",0)}}}}class N{constructor(t){this.details=t||{}}toSegment(){return{event:"Gift Card Redeemed",properties:s({source:"checkout",customer_id:c.get("customer.id"),store_id:c.get("store.id")},this.details)}}}function R(t,e){var s,i;return{product_id:null==(s=t.id)?void 0:s.toString(),name:t.name,price:t.price,position:e,url:t.url,image_url:null==(i=t.image)?void 0:i.url,currency:t.currency||c.get("cart.currency")||null}}function B(t){var e;return{source:null==t?void 0:t.source,recommendation_source:null==t?void 0:t.recommendation_source,request_id:null!=(e=null==t?void 0:t.request_id)?e:null}}class F{constructor(t){this.details=t}toSegment(){var t;const s=Array.isArray(null==(t=this.details)?void 0:t.products)?this.details.products.filter(Boolean):[];return s.length?{event:"Product List Viewed",properties:e({list_id:"checkout_xsell",category:null,sub_category:null},B(this.details),{products:s.map((t,s)=>e({},R(t,s+1),{variant:"product"}))})}:null}}class V{constructor(t){this.details=t}toSegment(){var t,s;const i=null==(t=this.details)?void 0:t.product;return i?{event:"Product Clicked",properties:e({},R(i,null!=(s=this.details.position)?s:null),B(this.details))}:null}}class G{constructor(t){this.details=t}toSegment(){var t,s,i;const n=null==(t=this.details)?void 0:t.product;return n?{event:"Product Added",properties:e({},R(n,null!=(s=this.details.position)?s:null),{quantity:null!=(i=this.details.quantity)?i:1,cart_id:c.get("cart.id")||null},B(this.details))}:null}}var J=new class{constructor(){this.events={"Checkout Started":m,"Payment Info Entered":g,"Checkout Step Viewed":v,"Checkout Step Completed":C,"Coupon Entered":w,"Coupon Applied":P,"Coupon Denied":b,"Coupon Removed":x,"Address Added":T,"Address Updated":A,"Map Clicked":j,"Payment Submitted":E,"Payment Failed":I,"Payment Succeeded":q,"Payment Pending":D,"Gift Card Redeemed":N,"Product List Viewed":F,"Product Clicked":V,"Product Added":G}}transform(t,e){const s=this.events[t];if(!s)return null;try{const t=new s(e).toSegment();return null!=t&&t.properties&&null!=e&&e.checkout_type&&(t.properties.checkout_type=e.checkout_type),t}catch(t){return console.error("Failed to transform event:",t),null}}};class L{constructor(){this.history=[],this.customTrackers=[],this.context={},this.events=[],this._sessionId=null,this.mappedEvents={"payment::method_updated":"Payment Info Entered","checkout::step_viewed":"Checkout Step Viewed","checkout::step_completed":"Checkout Step Completed","coupon::applied":"Coupon Applied","coupon::denied":"Coupon Denied","coupon::entered":"Coupon Entered","coupon::removed":"Coupon Removed","address::added":"Address Added","address::updated":"Address Updated","map::clicked":"Map Clicked","payment::submitted":"Payment Submitted","payment::failed":"Payment Failed","payment::succeeded":"Payment Succeeded","payment::pending":"Payment Pending","giftcard::redeemed":"Gift Card Redeemed","checkout-xsell::list.viewed":"Product List Viewed","checkout-xsell::product.clicked":"Product Clicked","checkout-xsell::product.added":"Product Added"};const t=c.get("config.jitsu",{}),e=c.get("store.url",null),{host:s,key:i}=t||{};this.analytics=null,s&&i&&(this.analytics=d({host:this.getAnalyticsHost(e),writeKey:i,cookieCapture:this.captureCookies()}),this.events=c.get("config.jitsu.events",[]),c.get("config.jitsu.context.extra",{})&&this.setExtraContext(c.get("config.jitsu.context.extra",{})),this.initSessionId(),this.setSignedInCustomerTraits(),this.page())}setSignedInCustomerTraits(){const t=c.get("customer");t&&t.id&&this.setTraits(t)}setTraits(t){t&&this.setContext("traits",t)}initSessionId(){this._sessionId=_.resolve(),this.setContext("session_id",this._sessionId)}captureCookies(){const t=c.get("store.features",[])||[],e=t.includes("utm-client-ids")&&this.hasCookiesConsent(t);return i("sa_",e)}hasCookiesConsent(t){if(!t.includes("website-cookies"))return!0;try{return"accepted"===this.readCookiesConsent()&&/(?:^|;\s*)advertising_cookies=true(?:;|$)/.test(document.cookie)}catch(t){return!1}}readCookiesConsent(){var t;return null!=(t=globalThis.Salla)&&t.storage?globalThis.Salla.storage.get("cookies-consent"):JSON.parse(localStorage.getItem("cookies-consent"))}getAnalyticsHost(t){var e;return n(t,{documentOrigin:"undefined"!=typeof window?null==(e=window.location)?void 0:e.origin:null,log:console.log})}trackSallaEvent(t){var e;if("customer::identified"===t.type)return this.setTraits(t.detail);const s=null!=(e=this.mappedEvents[t.type])?e:t.type;if(this.analytics&&this.events.includes(s))try{if(!s)return;const e=J.transform(s,t.detail);e&&this.track(e.event,e.properties)}catch(t){console.log(t)}}track(t,e={}){return this.analytics?(e=this.finalPayload(e),this.analytics.track(t,e).then(s=>(this.pushEventToCustomTrackers("track",t,e),s)).then(()=>{this.history.push({action:"track",eventName:t,payload:e})}).catch(t=>Promise.reject(t))):Promise.resolve()}page(t={}){if(!this.analytics)return Promise.resolve();const e=this.finalPayload(t);return this.analytics.page(e).then(t=>(this.pushEventToCustomTrackers("page",e),t)).then(()=>{this.history.push({action:"page",payload:e})}).catch(t=>Promise.reject(t))}finalPayload(s={}){var i;let n={session_id:_.resolve(),cart_id:c.get("cart.id")||s.cart_id||s.checkout_id||null,requires_shipping:Boolean(c.get("cart.requires_shipping",!1))};return this.setContext("version",c.get("config.jitsu.version",1)),this.setContext("store_id",c.get("config.store.id")),n.session_id&&this.setContext("session_id",n.session_id),s.event_id=s.event_id||t(),e({},n,s,{session_id:s.session_id||n.session_id,cart_id:s.cart_id||n.cart_id,requires_shipping:null!=(i=s.requires_shipping)?i:n.requires_shipping})}registerTracker(t){r(t,this.customTrackers,console.log)&&(this.customTrackers.push(t),o(t,this.history))}pushEventToCustomTrackers(t,e,s={}){return a(this.customTrackers,t,e,s,console.log)}setContext(t,e){var s;null==(s=this.analytics)||s.setContextProperty(t,e),this.context[t]=e}setExtraContext(t){l((t,e)=>this.setContext(t,e),t,console.log)}}export{_ as SessionService,L as default,c as initialData,f as sanitizeErrorMessage,S as sanitizeFieldErrors};
|
|
2
2
|
//# sourceMappingURL=checkout.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salla.sa/analytics",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.3",
|
|
4
4
|
"description": "Salla unified analytics: Jitsu engine, event transformers and trackers for storefront (twilight) and checkout",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"rollup-plugin-exclude-dependencies-from-bundle": "^1.1.24",
|
|
64
64
|
"rollup-plugin-version-injector": "^1.3.3"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "f81ee70ec52634ccf66ac12038c06b4f528087ac"
|
|
67
67
|
}
|
|
@@ -14,6 +14,9 @@ import PaymentFailed from './payment-failed';
|
|
|
14
14
|
import PaymentSucceeded from './payment-succeeded';
|
|
15
15
|
import PaymentPending from './payment-pending';
|
|
16
16
|
import GiftCardRedeemed from './gift-card-redeemed';
|
|
17
|
+
import XsellProductListViewed from './xsell-product-list-viewed';
|
|
18
|
+
import XsellProductClicked from './xsell-product-clicked';
|
|
19
|
+
import XsellProductAdded from './xsell-product-added';
|
|
17
20
|
|
|
18
21
|
class EventTransformer {
|
|
19
22
|
constructor() {
|
|
@@ -34,6 +37,9 @@ class EventTransformer {
|
|
|
34
37
|
'Payment Succeeded': PaymentSucceeded,
|
|
35
38
|
'Payment Pending': PaymentPending,
|
|
36
39
|
'Gift Card Redeemed': GiftCardRedeemed,
|
|
40
|
+
'Product List Viewed': XsellProductListViewed,
|
|
41
|
+
'Product Clicked': XsellProductClicked,
|
|
42
|
+
'Product Added': XsellProductAdded,
|
|
37
43
|
};
|
|
38
44
|
}
|
|
39
45
|
|
package/src/checkout/index.js
CHANGED
|
@@ -31,6 +31,9 @@ export default class Analytics {
|
|
|
31
31
|
'payment::succeeded': 'Payment Succeeded',
|
|
32
32
|
'payment::pending': 'Payment Pending',
|
|
33
33
|
'giftcard::redeemed': 'Gift Card Redeemed',
|
|
34
|
+
'checkout-xsell::list.viewed': 'Product List Viewed',
|
|
35
|
+
'checkout-xsell::product.clicked': 'Product Clicked',
|
|
36
|
+
'checkout-xsell::product.added': 'Product Added',
|
|
34
37
|
};
|
|
35
38
|
const config = initialData.get('config.jitsu', {});
|
|
36
39
|
const storeUrl = initialData.get('store.url', null);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import initialData from './initial-data.js';
|
|
2
|
+
import { mapXsellProductFields, mapXsellAttribution } from './xsell-product-fields.js';
|
|
3
|
+
|
|
4
|
+
class XsellProductAdded {
|
|
5
|
+
constructor(details) {
|
|
6
|
+
this.details = details;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
toSegment() {
|
|
10
|
+
const product = this.details?.product;
|
|
11
|
+
if (!product) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
event: 'Product Added',
|
|
17
|
+
properties: {
|
|
18
|
+
...mapXsellProductFields(product, this.details.position ?? null),
|
|
19
|
+
quantity: this.details.quantity ?? 1,
|
|
20
|
+
// Same source and shape finalPayload() injects, so the two never disagree.
|
|
21
|
+
cart_id: initialData.get('cart.id') || null,
|
|
22
|
+
...mapXsellAttribution(this.details),
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export default XsellProductAdded;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { mapXsellProductFields, mapXsellAttribution } from './xsell-product-fields.js';
|
|
2
|
+
|
|
3
|
+
class XsellProductClicked {
|
|
4
|
+
constructor(details) {
|
|
5
|
+
this.details = details;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
toSegment() {
|
|
9
|
+
const product = this.details?.product;
|
|
10
|
+
if (!product) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
event: 'Product Clicked',
|
|
16
|
+
properties: {
|
|
17
|
+
...mapXsellProductFields(product, this.details.position ?? null),
|
|
18
|
+
...mapXsellAttribution(this.details),
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export default XsellProductClicked;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import initialData from './initial-data.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checkout-safe mirror of storefront/product-fields.js for the checkout upsell &
|
|
5
|
+
* cross-sell section.
|
|
6
|
+
*
|
|
7
|
+
* The storefront helper reads `Salla.config.currency()`, which does not exist on the
|
|
8
|
+
* checkout page, so the currency comes from the product itself and falls back to the
|
|
9
|
+
* server-rendered cart currency.
|
|
10
|
+
*/
|
|
11
|
+
export function mapXsellProductFields(product, position) {
|
|
12
|
+
return {
|
|
13
|
+
product_id: product.id?.toString(),
|
|
14
|
+
name: product.name,
|
|
15
|
+
price: product.price,
|
|
16
|
+
position,
|
|
17
|
+
url: product.url,
|
|
18
|
+
image_url: product.image?.url,
|
|
19
|
+
currency: product.currency || initialData.get('cart.currency') || null,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The attribution fields every checkout cross-sell emit carries.
|
|
25
|
+
*/
|
|
26
|
+
export function mapXsellAttribution(details) {
|
|
27
|
+
return {
|
|
28
|
+
source: details?.source,
|
|
29
|
+
recommendation_source: details?.recommendation_source,
|
|
30
|
+
request_id: details?.request_id ?? null,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { mapXsellProductFields, mapXsellAttribution } from './xsell-product-fields.js';
|
|
2
|
+
|
|
3
|
+
const LIST_ID = 'checkout_xsell';
|
|
4
|
+
|
|
5
|
+
class XsellProductListViewed {
|
|
6
|
+
constructor(details) {
|
|
7
|
+
this.details = details;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
toSegment() {
|
|
11
|
+
const products = Array.isArray(this.details?.products) ? this.details.products.filter(Boolean) : [];
|
|
12
|
+
// A list whose entries are all invalid is not an impression: sending it would count exposure
|
|
13
|
+
// with no products behind it.
|
|
14
|
+
if (!products.length) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
event: 'Product List Viewed',
|
|
20
|
+
properties: {
|
|
21
|
+
list_id: LIST_ID,
|
|
22
|
+
// A recommendation section has no category of its own.
|
|
23
|
+
category: null,
|
|
24
|
+
sub_category: null,
|
|
25
|
+
...mapXsellAttribution(this.details),
|
|
26
|
+
// `position` is the 1-based rank within this section.
|
|
27
|
+
products: products
|
|
28
|
+
.map((product, index) => ({
|
|
29
|
+
...mapXsellProductFields(product, index + 1),
|
|
30
|
+
variant: 'product',
|
|
31
|
+
})),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export default XsellProductListViewed;
|