about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-03-13 03:29:03 +0000
committerGitHub <noreply@github.com>2024-03-13 03:29:03 +0000
commitf1d55f49fa1edeed6ab7399875094f7e052b53f5 (patch)
treede07fe0e5422cad4885db05750f35cd931462136 /src/lib
parent653240bc056236489e8a7882b7b6f902ed0885c2 (diff)
downloadvoidsky-f1d55f49fa1edeed6ab7399875094f7e052b53f5.tar.zst
Send route name with Statsig events (#3194)
* Add types to Statsig events

* Send route name with events
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/statsig/events.ts5
-rw-r--r--src/lib/statsig/statsig.tsx27
2 files changed, 27 insertions, 5 deletions
diff --git a/src/lib/statsig/events.ts b/src/lib/statsig/events.ts
new file mode 100644
index 000000000..bc647710c
--- /dev/null
+++ b/src/lib/statsig/events.ts
@@ -0,0 +1,5 @@
+export type Events = {
+  init: {
+    initMs: number
+  }
+}
diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx
index 6d9ebeb09..a46cef4da 100644
--- a/src/lib/statsig/statsig.tsx
+++ b/src/lib/statsig/statsig.tsx
@@ -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,
+) {
+  getCurrentRouteName = getRouteName
+}
+
+export function logEvent<E extends keyof Events>(
+  eventName: E & string,
+  rawMetadata?: Events[E] & FlatJSONRecord,
 ) {
-  Statsig.logEvent(eventName, value, metadata)
+  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) {