UNPKG

@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.54 kB
'use strict';var chunkT4BXL6JM_js=require('./chunk-T4BXL6JM.js'),analyticsJs=require('@rudderstack/analytics-js');/* @deriv-com/analytics - NPM Package - Built with tsup */ var k=4320*60*60,n=class n{constructor(e,t,i=false){this.analytics=new analyticsJs.RudderAnalytics;this.has_identified=false;this.has_initialized=false;this.current_page="";this.rudderstack_anonymous_cookie_key="rudder_anonymous_id";this.debug=false;this.log=chunkT4BXL6JM_js.f("[RudderStack]",()=>this.debug);this.getAnonymousId=()=>document.cookie.match("(^|;)\\s*"+this.rudderstack_anonymous_cookie_key+"\\s*=\\s*([^;]+)")?.pop();this.setCookieIfNotExists=()=>{if(!this.getAnonymousId())try{let t=window.location.hostname,o=["webflow.io"].some(s=>t.endsWith(s))?t:t.split(".").slice(-2).join("."),d;if(crypto?.randomUUID)d=crypto.randomUUID();else if(crypto?.getRandomValues){let s=new Uint8Array(16);crypto.getRandomValues(s),s[6]=s[6]&15|64,s[8]=s[8]&63|128;let a=Array.from(s,g=>g.toString(16).padStart(2,"0")).join("");d=`${a.slice(0,8)}-${a.slice(8,12)}-${a.slice(12,16)}-${a.slice(16,20)}-${a.slice(20)}`;}else throw new Error("Crypto API not available for secure random UUID generation");let h=window.location.protocol==="https:"?"; Secure":"";document.cookie=`${this.rudderstack_anonymous_cookie_key}=${d}; path=/; Domain=${o}; max-age=${k}; SameSite=Lax${h}`;}catch(t){console.warn("RudderStack: Failed to set anonymous ID cookie",t);}};this.getUserId=()=>this.analytics.getUserId();this.init=e=>{if(!e){console.warn("RudderStack: Initialization skipped - no key provided");return}this.log("init | loading SDK",{dataplane:chunkT4BXL6JM_js.b});try{this.setCookieIfNotExists(),this.analytics.load(e,chunkT4BXL6JM_js.b,{externalAnonymousIdCookieName:this.rudderstack_anonymous_cookie_key,storage:{type:"localStorage"},lockIntegrationsVersion:!0,onLoaded:()=>{this.has_initialized=!0,this.has_identified=!!this.getUserId(),this.log("init | SDK loaded successfully",{userId:this.getUserId(),anonymousId:this.getAnonymousId()}),this.onLoadedCallback?.();}});}catch(t){console.error("RudderStack: Failed to initialize",t);}};this.identifyEvent=(e,t)=>{if(!this.has_initialized){console.warn("RudderStack: Cannot identify - not initialized");return}let i=this.getUserId();if(!i||i!==e)try{this.log("identifyEvent | identifying user",{user_id:e,traits:t}),this.analytics.identify(e,t||{}),this.has_identified=!0;}catch(r){console.error("RudderStack: Failed to identify user",r);}else this.log("identifyEvent | user already identified",{user_id:e}),this.has_identified=true;};this.pageView=(e,t="Deriv App",i,r)=>{if(!(!this.has_initialized||e===this.current_page))try{let o={...i&&{user_id:i},...r};this.log("pageView | tracking page view",{platform:t,current_page:e,properties:o}),this.analytics.page(t,e,o),this.current_page=e;}catch(o){console.error("RudderStack: Failed to track page view",o);}};this.reset=()=>{if(this.has_initialized)try{this.log("reset | resetting RudderStack session"),this.analytics.reset(),this.has_identified=!1;}catch(e){console.error("RudderStack: Failed to reset",e);}};this.track=(e,t)=>{if(this.has_initialized)try{this.log("track | sending event to RudderStack",{event:e,payload:t}),this.analytics.track(e,t);}catch(i){console.warn("RudderStack: Failed to track event",i);}};this.onLoadedCallback=t,this.debug=i,this.init(e);}};n.getRudderStackInstance=(e,t,i=false)=>(n._instance||(n._instance=new n(e,t,i)),n._instance);var u=n;exports.a=u;//# sourceMappingURL=chunk-6BHMKTVG.js.map //# sourceMappingURL=chunk-6BHMKTVG.js.map