@deriv-com/analytics
Version:
Comprehensive analytics package for Deriv applications. Provides unified event tracking, A/B testing, and user analytics through RudderStack, PostHog and GrowthBook integrations with built-in caching and offline support.
3 lines • 3.58 kB
JavaScript
/* @deriv-com/analytics - Browser ESM Bundle (RudderStack + PostHog) - Built with tsup */
import{a as y}from"./chunk-MWNJTXSL.mjs";import{b as B,g as m}from"./chunk-F3U33XPR.mjs";import{GrowthBook as A}from"@growthbook/growthbook";var i=class i{constructor(t,e,o={},n=!1){this.analytics=new y;this.isLoaded=!1;this.status=void 0;this.debug=!1;this.log=m("[GrowthBook]",()=>this.debug);this.setAttributes=({id:t,country:e,user_language:o,device_language:n,device_type:s,utm_source:r,utm_medium:a,utm_campaign:d,is_authorised:h,url:u,domain:w,utm_content:l,residence_country:f,loggedIn:c,network_type:g,network_downlink:k,user_id:b,anonymous_id:p,account_mode:G})=>{let v={...this.GrowthBook.getAttributes(),id:t,...b!==void 0&&{user_id:b},...p!==void 0&&{anonymous_id:p},...e!==void 0&&{country:e},...f!==void 0&&{residence_country:f},...o!==void 0&&{user_language:o},...n!==void 0&&{device_language:n},...s!==void 0&&{device_type:s},...r!==void 0&&{utm_source:r},...a!==void 0&&{utm_medium:a},...d!==void 0&&{utm_campaign:d},...h!==void 0&&{is_authorised:h},...u!==void 0&&{url:u},...w!==void 0&&{domain:w},...l!==void 0&&{utm_content:l},...c!==void 0&&{loggedIn:c},...g!==void 0&&{network_type:g},...k!==void 0&&{network_downlink:k},...G!==void 0&&{account_mode:G}};this.log("setAttributes | updating GrowthBook attributes",v),this.GrowthBook.setAttributes(v)};this.getFeatureValue=(t,e)=>{let o=this.GrowthBook.getFeatureValue(t,e);return this.log("getFeatureValue",{key:t,value:o,defaultValue:e}),o};this.getStatus=async()=>(await this.waitForIsLoaded(),{isLoaded:this.isLoaded,status:this.status});this.getFeatureState=t=>{let e=this.GrowthBook.evalFeature(t);return this.log("getFeatureState",{id:t,result:e}),e};this.setUrl=t=>{this.log("setUrl",{href:t}),this.GrowthBook.setURL(t)};this.isOn=t=>{let e=this.GrowthBook.isOn(t);return this.log("isOn",{key:t,result:e}),e};this.init=async()=>{this.log("init | initializing GrowthBook SDK");let t=await this.GrowthBook.init({timeout:2e3,streaming:!0}).catch(()=>{});this.status=t,this.isLoaded=!0,this.log("init | GrowthBook SDK loaded",{status:t})};this.destroy=()=>{this.GrowthBook.destroy(),this.isLoaded=!1,this.status=void 0};this.debug=n;let s=typeof window<"u"?window.location.hostname.includes("localhost"):!1;this.GrowthBook=new A({apiHost:B,clientKey:t,decryptionKey:e,antiFlicker:!1,navigateDelay:0,antiFlickerTimeout:3500,subscribeToChanges:!0,enableDevMode:s,trackingCallback:(r,a)=>{this.log("trackingCallback | experiment viewed",{experiment_id:r.key,variation_id:a.variationId}),typeof window<"u"&&window.dataLayer&&window.dataLayer.push({event:"experiment_viewed",event_category:"experiment",rudder_anonymous_id:this.analytics.getAnonymousId(),experiment_id:r.key,variation_id:a.variationId}),this.analytics.track("experiment_viewed",{experimentId:r.key,variationId:a.variationId})},...o}),this.init()}reapplyExperiment(t){let e=t??(typeof window<"u"?window.location.href:"");this.GrowthBook.setURL(e)}waitForIsLoaded(t=1e4){return new Promise((e,o)=>{let n=Date.now(),s=setInterval(()=>{this.isLoaded?(clearInterval(s),e()):Date.now()-n>=t&&(clearInterval(s),o(new Error("GrowthBook initialization timeout")))},100)})}};i.getGrowthBookInstance=(t,e,o,n=!1)=>i._instance?(typeof window<"u"&&console.warn&&console.warn("GrowthBook instance already exists. Ignoring new initialization parameters."),i._instance):(i._instance=new i(t,e??"",o,n),i._instance),i.resetInstance=()=>{i._instance&&(i._instance.destroy(),i._instance=void 0)};var I=i;export{I as Growthbook};
//# sourceMappingURL=growthbook-TBZXPHJZ.mjs.map