analyticsSlice.ts
1 import mixpanel from 'mixpanel-browser'; 2 import { StateCreator } from 'zustand'; 3 4 import { RootStore } from './root'; 5 6 const MIXPANEL_TOKEN = process.env.NEXT_PUBLIC_MIXPANEL || ''; 7 8 export type TrackEventProperties = { 9 [key: string]: string | number | boolean | Date | undefined; 10 }; 11 12 export type TrackEventProps = { 13 eventName: string; 14 eventParams?: TrackEventProperties; 15 }; 16 17 export type AnalyticsSlice = { 18 trackEvent: (eventName: string, properties?: TrackEventProperties) => void; 19 isTrackingEnabled: boolean; 20 initializeMixpanel: () => void; 21 acceptAnalytics: () => void; 22 rejectAnalytics: () => void; 23 analyticsConfigOpen: boolean; 24 setAnalyticsConfigOpen: (eventName: boolean) => void; 25 mixpanelInitialized: boolean; 26 }; 27 28 export const createAnalyticsSlice: StateCreator< 29 RootStore, 30 [['zustand/subscribeWithSelector', never], ['zustand/devtools', never]], 31 [], 32 AnalyticsSlice 33 // eslint-disable-next-line @typescript-eslint/ban-ts-comment 34 // @ts-ignore 35 > = (set, get) => { 36 return { 37 trackEvent: (eventName: string, properties: TrackEventProperties = {}) => { 38 const EXCLUDED_NETWORKS = ['fork_proto_mainnet', 'fork_proto_mainnet_v3']; 39 const trackingEnable = get().isTrackingEnabled; 40 41 if (!trackingEnable) return null; 42 43 const eventProperties = { 44 ...properties, 45 walletAddress: get().account, 46 market: properties.market ?? get().currentMarket, 47 walletType: get().walletType, 48 }; 49 50 try { 51 if (!EXCLUDED_NETWORKS.includes(get().currentMarket)) { 52 mixpanel.track(eventName, eventProperties); 53 } 54 } catch (err) { 55 console.log('something went wrong tracking event', err); 56 } 57 }, 58 59 isTrackingEnabled: false, 60 analyticsConfigOpen: true, 61 mixpanelInitialized: false, 62 63 initializeMixpanel: () => { 64 const userAcceptedAnalytics = localStorage.getItem('userAcceptedAnalytics') === 'true'; 65 const isInitialized = get().mixpanelInitialized; 66 67 if (!MIXPANEL_TOKEN) return; 68 69 if (userAcceptedAnalytics) { 70 if (!isInitialized) { 71 mixpanel.init(MIXPANEL_TOKEN, { ip: false }); 72 set({ mixpanelInitialized: true }); 73 } 74 75 mixpanel.opt_in_tracking(); 76 set({ isTrackingEnabled: true }); 77 } else { 78 if (!isInitialized) { 79 mixpanel.init(MIXPANEL_TOKEN, { ip: false }); 80 set({ mixpanelInitialized: true }); 81 } 82 mixpanel.opt_out_tracking(); 83 set({ isTrackingEnabled: false }); 84 } 85 }, 86 acceptAnalytics: () => { 87 localStorage.setItem('userAcceptedAnalytics', 'true'); 88 set({ isTrackingEnabled: true, analyticsConfigOpen: false }); 89 90 get().initializeMixpanel(); 91 }, 92 rejectAnalytics: () => { 93 localStorage.setItem('userAcceptedAnalytics', 'false'); 94 // mixpanel.opt_out_tracking(); 95 set({ isTrackingEnabled: false, analyticsConfigOpen: false }); 96 }, 97 setAnalyticsConfigOpen: (value: boolean) => { 98 localStorage.removeItem('userAcceptedAnalytics'); 99 100 set({ analyticsConfigOpen: value }); 101 }, 102 }; 103 };