diff options
author | dan <dan.abramov@gmail.com> | 2023-12-06 20:04:05 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-06 12:04:05 -0800 |
commit | 6335be14e17c7bf1a36e9103148277717e3a7a90 (patch) | |
tree | c10f139dc2507a38bb3c58819cc19382279a675f /src/lib/analytics/analytics.tsx | |
parent | 748212e000f963976bca5d63e0961d75a7e8b296 (diff) | |
download | voidsky-6335be14e17c7bf1a36e9103148277717e3a7a90.tar.zst |
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
Diffstat (limited to 'src/lib/analytics/analytics.tsx')
-rw-r--r-- | src/lib/analytics/analytics.tsx | 32 |
1 files changed, 19 insertions, 13 deletions
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 |