From 157404132f43dc4fc42437fe27348a491161f241 Mon Sep 17 00:00:00 2001
From: Eric Bailey <git@esb.lol>
Date: Thu, 25 Jan 2024 23:12:48 -0600
Subject: Improve type checking for metrics events (#2632)

---
 src/lib/analytics/analytics.web.tsx | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

(limited to 'src/lib/analytics/analytics.web.tsx')

diff --git a/src/lib/analytics/analytics.web.tsx b/src/lib/analytics/analytics.web.tsx
index 1be8e608a..72b28f9c0 100644
--- a/src/lib/analytics/analytics.web.tsx
+++ b/src/lib/analytics/analytics.web.tsx
@@ -3,7 +3,7 @@ import {createClient} from '@segment/analytics-react'
 import {sha256} from 'js-sha256'
 import {Browser} from 'sentry-expo'
 
-import {TrackEvent, AnalyticsMethods} from './types'
+import {ScreenPropertiesMap, TrackPropertiesMap} from './types'
 import {useSession, SessionAccount} from '#/state/session'
 import {logger} from '#/logger'
 
@@ -29,20 +29,30 @@ function getClient(): SegmentClient {
   return segmentClient
 }
 
-export const track: TrackEvent = async (...args) => {
-  await getClient().track(...args)
+export const track = async <E extends keyof TrackPropertiesMap>(
+  event: E,
+  properties?: TrackPropertiesMap[E],
+) => {
+  await getClient().track(event, properties)
 }
 
-export function useAnalytics(): AnalyticsMethods {
+export function useAnalytics() {
   const {hasSession} = useSession()
+
   return React.useMemo(() => {
     if (hasSession) {
       return {
-        async screen(...args) {
-          await getClient().screen(...args)
+        async screen<E extends keyof ScreenPropertiesMap>(
+          event: E,
+          properties?: ScreenPropertiesMap[E],
+        ) {
+          await getClient().screen(event, properties)
         },
-        async track(...args) {
-          await getClient().track(...args)
+        async track<E extends keyof TrackPropertiesMap>(
+          event: E,
+          properties?: TrackPropertiesMap[E],
+        ) {
+          await getClient().track(event, properties)
         },
       }
     }
-- 
cgit 1.4.1

