(function(n,t,i){var r=i();typeof define=="function"&&define.amd?define(n,[],i):typeof exports=="object"?module.exports=r:t[n]=r;Window[n]=r})("sAttribution",this,function(){function p(n,t){var i="; expires="+ni(new Date),r="; domain=."+k;document.cookie=n+"="+(t||"")+i+r+"; path=/"}function kt(n){for(var t,r=n+"=",u=document.cookie.split(";"),i=0;i0){const u=3e3*r;return await new Promise(n=>setTimeout(n,u)),ut(n,t,i-1,r+1)}const f=JSON.parse(t.body);await ti(JSON.stringify({data:JSON.stringify({data:f,error:u}),token:a,message:"Error emitting data"}));throw new Error(u);}return fetch(n,t).then(async n=>{if(!n.ok)return await u()}).catch(async n=>await u(n))}async function ti(n){await fetch(at,{method:"POST",keepalive:!0,body:n,headers:{"Content-Type":"application/json"}})}function ii(){this.updateStorage({...this.storage,leAt:(new Date).valueOf()})}function ri(){this.updateStorage({...this.storage,lcAt:(new Date).valueOf()})}function ui(){this.updateStorage({...this.storage,leAt:undefined})}function fi(n=6){return window.crypto?crypto.randomUUID():Math.random().toString(20).substr(2,n)}function ei(){this.updateStorage({...this.storage,externalId:fi(40)+this.storage.rId})}function oi(){const i=w(t)??{};if(Object.keys(i)?.length>0)return p(t,JSON.stringify(i)),i;const f=kt(t)||"{}",r=JSON.parse(f)||{};if(Object.keys(r)?.length>0)return b(t,r),r;const n={},u=Object.keys(rt);for(let t=0;t0?(b(t,n),p(t,JSON.stringify(n)),n):{}}function si(n){u(t,n);p(t,JSON.stringify(n));this.storage=n}function hi(){if(!this.storage.leAt)return!0;const n=(new Date).setDate((new Date).getDate()-3);return this.storage.leAt({...t,[i]:n.get(i)}),{}),f=Array.from(n.keys()).filter(function(n){return n.startsWith("utm")}).reduce((t,i)=>({...t,[i]:n.get(i)}),{}),e=navigator.userAgent,o={referralLinkId:this.storage.rId,landingPageUrl:i,referrerUrl:r,queryParams:u,utmParams:f,userAgent:e,linkSource:this.storage.linkS,linkVariant:this.storage.linkV,characterSet:document.characterSet,title:document.title,location:document.location,cookieEnabled:navigator.cookieEnabled,externalId:this.storage.externalId},t={accessToken:a,type:"click",xInfo:o,pixelType:it};return await y(ot,t,{method:"POST",keepalive:!0,body:JSON.stringify(t)})},pt=async function(){let n=this.storage.lcAt;n||(this.storage.expireDate?(n=new Date(this.storage.expireDate).setDate(new Date(this.storage.expireDate).getDate()-tt).valueOf(),delete this.storage.expireDate,this.updateStorage(this.storage)):n=(new Date).valueOf());const t={accessToken:a,type:"order",pixelType:it,oInfo:{referralLinkId:this.storage.rId,externalOrderId:this.data.orderId,externalOrderNumber:this.data.orderNumber,externalOrderSource:this.data.externalOrderSource,orderDate:this.data.orderDate,isNewCustomer:this.data.isNewCustomer,totalPrice:this.data.totalPrice,subtotalPrice:this.data.subtotalPrice,currency:this.data.currency,discountCodes:this.data.discountCodes,lcAt:n,leAt:this.storage.leAt,customer:{externalCustomerId:this.data.customerId,firstName:this.data.firstName,lastName:this.data.lastName,emailAddress:this.data.email},linkSource:this.storage.linkS,linkVariant:this.storage.linkV,externalId:this.storage.externalId}};return await y(st,t,{method:"POST",keepalive:!0,body:JSON.stringify(t)})},wt=function(n){if(n&&(window.Shopify||d))if(sessionStorage.getItem(i)){var t=sessionStorage.getItem(i);t&&fetch(window.location.origin+"/discount/"+t)}else if(!sessionStorage.getItem(r)){sessionStorage.setItem(r,!0);const t=new URLSearchParams({external_source:lt,referral_ids:n});this.storage.linkS&&t.set("l_s",this.storage.linkS);this.storage.linkV&&t.set("l_v",this.storage.linkV);this.storage.externalId&&t.set("external_id",this.storage.externalId);this.storage.lcAt&&t.set("lc_at",this.storage.lcAt.toString());fetch(ht+"&"+t,{method:"GET"}).then(function(n){sessionStorage.removeItem(r);n.json().then(n=>{n.code&&(sessionStorage.setItem(i,n.code),d&&(localStorage.setItem("cart-discount-applied",n.code),localStorage.setItem("discount_code",n.code)),fetch(window.location.origin+"/discount/"+n.code))})}).catch(function(){sessionStorage.removeItem(r)})}},bt=function(){sessionStorage.removeItem(i)};for(n.prototype.set=vt,n.prototype.emit=y,n.prototype.emitOrder=pt,n.prototype.emitClick=yt,n.prototype.updateStorage=si,n.prototype.updateLastEmittedAt=ii,n.prototype.updateLastClickedAt=ri,n.prototype.canEmit=hi,n.prototype.clearLastEmittedAt=ui,n.prototype.applyDiscountCode=wt,n.prototype.addExternalId=ei,n=new n,u=b.bind(n),n?.storage?.emitPending===!0&&(n.emitClick(),n.updateLastEmittedAt()),ft=new URL(window.location),o=new URLSearchParams(ft.search),g&&v.unshift(g),c=0;c