kapa.ai
๋ฐ๋ก ์ ์ก์ด ๋์ง ์๋ ์ต์
์ด ์๋์?
const waitForMixpanel = (callback: () => void, retries = 40, interval = 50) => {
try {
callback()
} catch (error) {
if (retries > 0) {
setTimeout(
() => waitForMixpanel(callback, retries - 1, interval),
interval
)
} else {
logger.info('Mixpanel failed to initialize after waiting.')
}
}
}
export const MIXPANEL = {
init: () => {
if (!enabled) {
logger.info('Mixpanel token is missing! Check your .env file.')
return
}
mixpanel.init(MIXPANEL_TOKEN as string, {
track_pageview: false,
debug: isDevMode,
record_sessions_percent: 1, //records 1% of all sessions
record_idle_timeout_ms: 1800000, //End a replay capture after 30mins of inactivity
})
},
setUser: (userId: string, user: CurrentUserInfo) => {
if (!enabled) return
waitForMixpanel(() => identifyUser(userId))
},
updateUser: (record: Record<string, any>) => {
if (!enabled) return
waitForMixpanel(() => mixpanel.people.set(record))
},
signOut: () => {
if (!enabled) return
waitForMixpanel(() => resetUser())
},
track: (eventName: string, properties?: Record<string, any>) => {
if (!enabled) return
if (
typeof window !== 'undefined' &&
window.location.href.includes('localhost:6006')
) {
return
}
waitForMixpanel(() => mixpanel.track(eventName, properties))
},
}