UNPKG

@web3auth/no-modal

Version:
134 lines (130 loc) 4.22 kB
import _objectSpread from '@babel/runtime/helpers/objectSpread2'; import _defineProperty from '@babel/runtime/helpers/defineProperty'; import { AnalyticsBrowser } from '@segment/analytics-next'; import { log } from './loglevel.js'; const SEGMENT_WRITE_KEY = "f6LbNqCeVRf512ggdME4b6CyflhF1tsX"; class Analytics { constructor() { _defineProperty(this, "segment", void 0); _defineProperty(this, "globalProperties", {}); _defineProperty(this, "enabled", true); } init() { if (!this.enabled) { return; } if (this.isSkipped()) { return; } if (this.segment) { throw new Error("Analytics already initialized"); } this.segment = new AnalyticsBrowser(); this.segment.load({ writeKey: SEGMENT_WRITE_KEY }, { user: { cookie: { key: "web3auth_ajs_user_id" }, localStorage: { key: "web3auth_ajs_user_traits" } }, globalAnalyticsKey: "web3auth_analytics" }).then(() => { log.debug("Analytics initialized"); return true; }).catch(error => { log.error("Failed to initialize Analytics", error); }); } enable() { this.enabled = true; } disable() { this.enabled = false; } setGlobalProperties(properties) { this.globalProperties = _objectSpread(_objectSpread({}, this.globalProperties), properties); } async identify(userId, traits) { if (!this.enabled) return; if (this.isSkipped()) return; try { return this.getSegment().identify(userId, _objectSpread({}, traits)); } catch (error) { log.error(`Failed to identify user ${userId} in analytics`, error); } } async track(event, properties) { if (!this.enabled) return; if (this.isSkipped()) return; try { return this.getSegment().track(event, _objectSpread(_objectSpread({}, properties), this.globalProperties)); } catch (error) { log.error(`Failed to track event ${event}`, error); } } getSegment() { if (!this.segment) { log.error("Analytics not initialized. Call Analytics.init() first."); throw new Error("Analytics not initialized. Call Analytics.init() first."); } return this.segment; } isSkipped() { const dappOrigin = window.location.origin; // skip if the protocol is http if (dappOrigin.startsWith("http://")) { return true; } // skip if dapp contains localhost if (dappOrigin.includes("localhost")) { return true; } return false; } } const ANALYTICS_EVENTS = { // SDK Initialization SDK_INITIALIZATION_COMPLETED: "SDK Initialization Completed", SDK_INITIALIZATION_FAILED: "SDK Initialization Failed", // Connection CONNECTION_STARTED: "Connection Started", CONNECTION_COMPLETED: "Connection Completed", CONNECTION_FAILED: "Connection Failed", // Identity Token IDENTITY_TOKEN_STARTED: "Identity Token Started", IDENTITY_TOKEN_COMPLETED: "Identity Token Completed", IDENTITY_TOKEN_FAILED: "Identity Token Failed", // MFA MFA_ENABLEMENT_STARTED: "MFA Enablement Started", MFA_ENABLEMENT_COMPLETED: "MFA Enablement Completed", MFA_ENABLEMENT_FAILED: "MFA Enablement Failed", MFA_MANAGEMENT_SELECTED: "MFA Management Selected", MFA_MANAGEMENT_FAILED: "MFA Management Failed", // Login Modal LOGIN_MODAL_OPENED: "Login Modal Opened", LOGIN_MODAL_CLOSED: "Login Modal Closed", SOCIAL_LOGIN_SELECTED: "Social Login Selected", EXTERNAL_WALLET_SELECTED: "External Wallet Selected", EXTERNAL_WALLET_LIST_EXPANDED: "External Wallet List Expanded", // Wallet Plugin WALLET_UI_CLICKED: "Wallet UI Clicked", WALLET_CONNECT_SCANNER_CLICKED: "Wallet Connect Scanner Clicked", WALLET_FUNDING_CLICKED: "Wallet Funding Clicked", WALLET_CHECKOUT_CLICKED: "Wallet Checkout Clicked", WALLET_RECEIVE_CLICKED: "Wallet Receive Clicked", WALLET_SWAP_CLICKED: "Wallet Swap Clicked" }; const ANALYTICS_INTEGRATION_TYPE = { REACT_HOOKS: "React Hooks", VUE_COMPOSABLES: "Vue Composables", NATIVE_SDK: "Native SDK" }; const ANALYTICS_SDK_TYPE = { WEB_NO_MODAL: "Web NoModal", WEB_MODAL: "Web Modal" }; export { ANALYTICS_EVENTS, ANALYTICS_INTEGRATION_TYPE, ANALYTICS_SDK_TYPE, Analytics };