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
This commit is contained in:
dan 2023-12-06 20:04:05 +00:00 committed by GitHub
parent 748212e000
commit 6335be14e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 48 deletions

View file

@ -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

View file

@ -6,9 +6,8 @@ import {
} from '@segment/analytics-react'
import {sha256} from 'js-sha256'
import {useSession} from '#/state/session'
import {useSession, SessionAccount} from '#/state/session'
import {logger} from '#/logger'
import {listenSessionLoaded} from '#/state/events'
const segmentClient = createClient(
{
@ -44,8 +43,8 @@ export function useAnalytics() {
}, [hasSession, methods])
}
export function init() {
listenSessionLoaded(account => {
export function init(account: SessionAccount | undefined) {
if (account) {
if (account.did) {
if (account.did) {
const did_hashed = sha256(account.did)
@ -56,7 +55,7 @@ export function init() {
segmentClient.identify()
}
}
})
}
}
export function Provider({children}: React.PropsWithChildren<{}>) {