From 6335be14e17c7bf1a36e9103148277717e3a7a90 Mon Sep 17 00:00:00 2001
From: dan <dan.abramov@gmail.com>
Date: Wed, 6 Dec 2023 20:04:05 +0000
Subject: Move analytics out of init (#2115)

* Remove listenSessionLoaded from analytics

* Move analytics init call to navigation ready

* Remove zod dependency from analytics

* Mirror changes on the web

* Delete listenSessionLoaded

* Only set up listeners once
---
 src/lib/analytics/analytics.tsx | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

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

diff --git a/src/lib/analytics/analytics.tsx b/src/lib/analytics/analytics.tsx
index 3a8254eb1..1650dfd3f 100644
--- a/src/lib/analytics/analytics.tsx
+++ b/src/lib/analytics/analytics.tsx
@@ -7,20 +7,17 @@ import {
   useAnalytics as useAnalyticsOrig,
   ClientMethods,
 } from '@segment/analytics-react-native'
-import {z} from 'zod'
-import {useSession} from '#/state/session'
+import {useSession, SessionAccount} from '#/state/session'
 import {sha256} from 'js-sha256'
 import {ScreenEvent, TrackEvent} from './types'
 import {logger} from '#/logger'
-import {listenSessionLoaded} from '#/state/events'
 
-export const appInfo = z.object({
-  build: z.string().optional(),
-  name: z.string().optional(),
-  namespace: z.string().optional(),
-  version: z.string().optional(),
-})
-export type AppInfo = z.infer<typeof appInfo>
+type AppInfo = {
+  build?: string | undefined
+  name?: string | undefined
+  namespace?: string | undefined
+  version?: string | undefined
+}
 
 const segmentClient = createClient({
   writeKey: '8I6DsgfiSLuoONyaunGoiQM7A6y2ybdI',
@@ -58,8 +55,10 @@ export function useAnalytics() {
   }, [hasSession, methods])
 }
 
-export function init() {
-  listenSessionLoaded(account => {
+export function init(account: SessionAccount | undefined) {
+  setupListenersOnce()
+
+  if (account) {
     if (account.did) {
       const did_hashed = sha256(account.did)
       segmentClient.identify(did_hashed, {did_hashed})
@@ -68,8 +67,15 @@ export function init() {
       logger.debug('Ping w/o hash')
       segmentClient.identify()
     }
-  })
+  }
+}
 
+let didSetupListeners = false
+function setupListenersOnce() {
+  if (didSetupListeners) {
+    return
+  }
+  didSetupListeners = true
   // NOTE
   // this is a copy of segment's own lifecycle event tracking
   // we handle it manually to ensure that it never fires while the app is backgrounded
-- 
cgit 1.4.1

