Files
speckle-server/packages/server/modules/shared/utils/mixpanel.ts
T
Kristaps Fabians Geikins 5d0fceaaf3 feat: proper sign up tracking (#1489)
* feat: register flag passed to fe

* feat: mixpanel tracking for all sign ups

* feat: utm first touch & last touch tracking

* feat(helm): Allows Environment Variable for MP to be configured
- default is enabled
- renames environment variable to ENABLE_MP

* feat(helm network policy): allowlist analytics.speckle.systems

---------

Co-authored-by: Iain Sproat <68657+iainsproat@users.noreply.github.com>
2023-03-30 12:21:59 +03:00

47 lines
1.2 KiB
TypeScript

/* eslint-disable camelcase */
import { Optional, resolveMixpanelUserId } from '@speckle/shared'
import { enableMixpanel } from '@/modules/shared/helpers/envHelper'
import Mixpanel from 'mixpanel'
let client: Optional<Mixpanel.Mixpanel> = undefined
export function initialize() {
if (client || !enableMixpanel()) return
client = Mixpanel.init('acd87c5a50b56df91a795e999812a3a4', {
host: 'analytics.speckle.systems'
})
}
/**
* Mixpanel client. Can be undefined if not initialized or disabled.
*/
export function getClient() {
return client
}
/**
* Mixpanel tracking helper. An abstraction layer over the client that makes it a bit nicer to work with.
*/
export function mixpanel(params: { mixpanelUserId: Optional<string> }) {
const { mixpanelUserId } = params
const userIdentificationProperties = () => ({
...(mixpanelUserId
? {
distinct_id: mixpanelUserId
}
: {})
})
return {
track: (eventName: string, extraProperties?: Record<string, unknown>) => {
return getClient()?.track(eventName, {
...userIdentificationProperties(),
...(extraProperties || {})
})
}
}
}
export { resolveMixpanelUserId }