Send route name with Statsig events (#3194)

* Add types to Statsig events

* Send route name with events
This commit is contained in:
dan
2024-03-13 03:29:03 +00:00
committed by GitHub
parent 653240bc05
commit f1d55f49fa
3 changed files with 36 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
export type Events = {
init: {
initMs: number
}
}
+22 -5
View File
@@ -6,6 +6,7 @@ import {
} from 'statsig-react-native-expo'
import {useSession} from '../../state/session'
import {sha256} from 'js-sha256'
import {Events} from './events'
const statsigOptions = {
environment: {
@@ -17,12 +18,28 @@ const statsigOptions = {
initTimeoutMs: 1,
}
export function logEvent(
eventName: string,
value?: string | number | null,
metadata?: Record<string, string> | null,
type FlatJSONRecord = Record<
string,
string | number | boolean | null | undefined
>
let getCurrentRouteName: () => string | null | undefined = () => null
export function attachRouteToLogEvents(
getRouteName: () => string | null | undefined,
) {
Statsig.logEvent(eventName, value, metadata)
getCurrentRouteName = getRouteName
}
export function logEvent<E extends keyof Events>(
eventName: E & string,
rawMetadata?: Events[E] & FlatJSONRecord,
) {
const fullMetadata = {
...rawMetadata,
} as Record<string, string> // Statsig typings are unnecessarily strict here.
fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)'
Statsig.logEvent(eventName, null, fullMetadata)
}
export function useGate(gateName: string) {