@web3auth/no-modal
Version:
Multi chain wallet aggregator for web3Auth
153 lines (149 loc) • 5.13 kB
JavaScript
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",
// Consent Flow
USER_CONSENT_STARTED: "User Consent Started",
USER_CONSENT_ACCEPTED: "User Consent Accepted",
USER_CONSENT_DECLINED: "User Consent Declined",
USER_CONSENT_ERRORED: "User Consent Errored",
TERMS_OF_SERVICE_CLICKED: "Terms of Service Clicked",
PRIVACY_POLICY_CLICKED: "Privacy Policy Clicked",
// 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",
// Account Linking
ACCOUNT_LINKING_STARTED: "Account Linking Started",
ACCOUNT_LINKING_COMPLETED: "Account Linking Completed",
ACCOUNT_LINKING_FAILED: "Account Linking Failed",
// Account Unlinking
ACCOUNT_UNLINKING_STARTED: "Account Unlinking Started",
ACCOUNT_UNLINKING_COMPLETED: "Account Unlinking Completed",
ACCOUNT_UNLINKING_FAILED: "Account Unlinking Failed",
// Account switch (active connected wallet)
ACCOUNT_SWITCH_STARTED: "Account Switch Started",
ACCOUNT_SWITCH_COMPLETED: "Account Switch Completed",
ACCOUNT_SWITCH_FAILED: "Account Switch Failed"
};
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 };