@beamimpact/web-sdk
Version:
The Beam SDK enables brands to connect with their customers over shared values, not transactional discounts, to build stronger loyalty. Our integration achieves this by allowing customers to (a) choose a nonprofit where the brand will donate part of their
18 lines (17 loc) • 5.6 kB
JavaScript
import{c as M}from"../chunks/routes-BvX92EDq.esm.js";import{B as D}from"../chunks/cart-contents-DkoytiZh.esm.js";import{h as O,b as W,c as K}from"../chunks/events-Dki0ka4F.esm.js";import{B as g}from"../chunks/beam-errors-Ci0d3926.esm.js";import{getCookieValue as _,setCookieValue as P}from"../utils/cookies.js";import{logger as A}from"../utils/logger.js";import{saveRemoteSession as j}from"../utils/remote-session.js";import"../chunks/promo-types-DKAOFHJr.esm.js";import"../utils/local-storage.js";var b=(e,t,i)=>{if(!t.has(e))throw TypeError("Cannot "+i)},d=(e,t,i)=>(b(e,t,"read from private field"),i?i.call(e):t.get(e)),n=(e,t,i)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,i)},T=(e,t,i,a)=>(b(e,t,"write to private field"),t.set(e,i),i),h=(e,t,i)=>(b(e,t,"access private method"),i),l,p,E,L,v,B,m,f,$,u,S,y,k,I,x;const z=4,G=`https://cdn.jsdelivr.net/npm/statsig-js@${z}/build/statsig-prod-web-sdk.min.js`,N="beam_statsig_session_id",s="data-beam-visibility",F="beam-sync-visibility",w={STATSIG_INIT:"statsig_init",CART_CREATED:"cart_created",ORDER_CREATED:"order_created",BEAM_SELECTION:"beam_selection"};class U{constructor(t){n(this,E),n(this,v),n(this,f),n(this,u),n(this,y),n(this,I),this.name="statsig",this.experiments={shouldShowBeam:!1,isLoggingEnabled:!0},n(this,l,void 0),n(this,p,void 0),n(this,m,[]),this.statsigApiKey=t.statsigApiKey,this.beforeLogEvent=t.beforeLogEvent}async init(t){T(this,l,t),h(this,E,L).call(this),h(this,u,S).call(this,"hide");const i=document.createElement("style");if(i.innerHTML=`
[${s}="hide"] beam-select-nonprofit,
[${s}="hide"] beam-post-purchase,
[${s}="hide"] beam-redeem-transaction,
[${s}="hide"] beam-impact-overview,
[${s}="hide"] beam-community-impact,
[${s}="hide"] beam-cumulative-impact,
[${s}="hide"] beam-product-details-page,
[${s}="hide"] beam-subscription-management,
[${s}="hide"] beam-subscription-impact,
[${s}="hide"] beam-select-subscription-nonprofit,
[${s}="hide"] beam-social-share,
[${s}="hide"] .${F} {
display: none;
}
`,document.head.append(i),await h(this,v,B).call(this),this.stableId=this.statsig.getStableID(),!this.stableId)throw new g("Statsig failed to assign stableId",{name:"StatsigInitError"});h(this,f,$).call(this),j({remoteSessionId:this.stableId,apiKey:t.apiKey}),T(this,p,_(D)),P({name:N,value:this.stableId,path:"/",domain:t.domain}),this.experiments={shouldShowBeam:this.statsig.getLayer("beam_trial_layer").get("show_beam",!1),isLoggingEnabled:this.statsig.getLayer("beam_trial_layer").get("enable_logging",!0)},await Promise.all([h(this,y,k).call(this),h(this,I,x).call(this)]),h(this,u,S).call(this,this.experiments.shouldShowBeam?"show":"hide"),A.debug("Statsig Experiment Assignments",this.experiments)}logEvent(t,i,a={}){if(!this.statsig){d(this,m).push([t,i,a]);return}const o=this.experiments.shouldShowBeam,r=window.location.hostname,c=window.location.pathname,C=window.location.href;if(this.experiments.isLoggingEnabled){const R=this.beforeLogEvent?this.beforeLogEvent(t,i,a):a;this.statsig.logEvent(t,i,{...R,withBeam:o,domain:r,path:c,page:C})}A.debug(t,i,{...a,withBeam:o,domain:r,path:c,page:C})}}l=new WeakMap,p=new WeakMap,E=new WeakSet,L=function(){window.addEventListener(O.eventName,t=>{const i=t,{itemCount:a,cartId:o,subtotal:r,currencyCode:c}=i.detail;this.logEvent(w.CART_CREATED,!0,{itemCount:a,cartId:o,subtotal:r,currencyCode:c})}),window.addEventListener(W.eventName,t=>{const i=t,{orderId:a,cartTotal:o,currencyCode:r}=i.detail;this.logEvent(w.ORDER_CREATED,o,{orderId:a,cartTotal:o,currencyCode:r,hasCart:!!d(this,p)})});let e;window.addEventListener(K.eventName,t=>{const i=t,{selectedNonprofitId:a,selectionId:o,source:r}=i.detail;a!==e&&(this.logEvent(w.BEAM_SELECTION,!0,{nonprofitId:a,widget:r,selectionId:o}),e=a)})},v=new WeakSet,B=async function(){const e=document.createElement("script");e.src=G,e.async=!1;const t=new Promise((a,o)=>{e.addEventListener("load",a),e.addEventListener("error",o)});document.head.append(e);try{await t}catch{throw new g("Failed to load Statsig script",{name:"StatsigInitError"})}if(this.statsig=window.statsig,!this.statsig)throw new g("Failed to detect Statsig",{name:"StatsigInitError"});const i={disableErrorLogging:!0,disableAutoMetricsLogging:!0,disableCurrentPageLogging:!0,loggingIntervalMillis:1e3,loggingBufferMaxSize:2,overrideStableID:_(N)??null};await this.statsig.initialize(this.statsigApiKey,{},i)},m=new WeakMap,f=new WeakSet,$=function(){for(;d(this,m).length>0;){const e=d(this,m).shift();e&&this.logEvent(...e)}},u=new WeakSet,S=function(e){document.body.setAttribute(s,e)},y=new WeakSet,k=async function(){const e="https://events.statsigapi.net/v1/log_event";try{await fetch(e,{method:"POST",headers:{"content-type":"application/json","statsig-api-key":this.statsigApiKey},body:JSON.stringify({eventName:w.STATSIG_INIT,value:"true",time:new Date().getTime()/1e3,metadata:{show_beam:this.experiments.shouldShowBeam.toString(),domain:window.location.hostname,path:window.location.pathname,page:window.location.href},user:{userID:this.stableId}})})}catch(t){throw new g("Could not call Statsig API",{name:"StatsigInitError",cause:t})}},I=new WeakSet,x=async function(){return M({baseUrl:d(this,l)?.baseUrl,headers:{authorization:`Api-Key ${d(this,l)?.apiKey}`},requestBody:{remoteSessionId:this.stableId,storeId:d(this,l)?.storeId,experiments:{showBeam:this.experiments.shouldShowBeam}}})};export{U as StatsigPlugin};
//# sourceMappingURL=statsig.js.map