Files
speckle-server/packages/frontend-2/lib/core/clients/mp.ts
T
Kristaps Fabians Geikins ee5ae8af62 fix(fe2): accept invite before onboarding after sign up (#2491)
* explicitly ordering global middlewares

* various subscription fixes & WIP project invite middleware

* SSR invite accept & toast notifs seem to work

* backend support for mixpanel

* mixpanel be logic -> shared

* minor fix

* finissh

* lint fix

* minor comment adjustments

* better adblock handling
2024-07-11 11:45:11 +03:00

83 lines
2.3 KiB
TypeScript

/* eslint-disable camelcase */
import { type Nullable, resolveMixpanelServerId } from '@speckle/shared'
import mixpanel from 'mixpanel-browser'
import { useOnAuthStateChange } from '~/lib/auth/composables/auth'
import {
HOST_APP,
HOST_APP_DISPLAY_NAME,
type MixpanelClient
} from '~/lib/common/helpers/mp'
import { useTheme } from '~/lib/core/composables/theme'
/**
* Get mixpanel server identifier
*/
function getMixpanelServerId(): string {
return resolveMixpanelServerId(window.location.hostname)
}
/**
* Composable that builds the user (re-)identification function. Needs to be invoked on app
* init and when the active user changes (e.g. after signing out/in)
*/
function useMixpanelUserIdentification() {
if (import.meta.server) return { reidentify: () => void 0 }
const { distinctId } = useActiveUser()
const { isDarkTheme } = useTheme()
const serverId = getMixpanelServerId()
const {
public: { speckleServerVersion }
} = useRuntimeConfig()
return {
reidentify: (mp: MixpanelClient) => {
// Reset previous user data, if any
mp.reset()
// Register session
mp.register({
server_id: serverId,
hostApp: HOST_APP,
speckleVersion: speckleServerVersion
})
// Identify user, if any
if (distinctId.value) {
mp.identify(distinctId.value)
mp.people.set('Identified', true)
mp.people.set('Theme Web', isDarkTheme.value ? 'dark' : 'light')
mp.add_group('server_id', serverId)
}
}
}
}
export const useClientsideMixpanelClientBuilder = () => {
const {
public: { mixpanelApiHost, mixpanelTokenId, logCsrEmitProps }
} = useRuntimeConfig()
const { reidentify } = useMixpanelUserIdentification()
const onAuthStateChange = useOnAuthStateChange()
return async (): Promise<Nullable<MixpanelClient>> => {
if (!mixpanel || !mixpanelTokenId.length || !mixpanelApiHost.length) {
return null
}
// Init
mixpanel.init(mixpanelTokenId, {
api_host: mixpanelApiHost,
debug: !!import.meta.dev && logCsrEmitProps
})
// Reidentify on auth change
await onAuthStateChange(() => reidentify(mixpanel), { immediate: true })
// Track app visit
mixpanel.track(`Visit ${HOST_APP_DISPLAY_NAME}`)
return mixpanel
}
}