[Statsig] Track active time (#3289)

zio/stable
dan 2024-03-20 03:25:37 +00:00 committed by GitHub
parent 3d8d1dd173
commit 20337ceef1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -10,7 +10,9 @@ export type LogEvents = {
logContext: 'SwitchAccount' | 'Settings' | 'Deactivated'
}
'notifications:openApp': {}
'state:background': {}
'state:background': {
secondsActive: number
}
'state:foreground': {}
'feed:endReached': {
feedType: string

View File

@ -67,15 +67,24 @@ function toStatsigUser(did: string | undefined) {
}
let lastState: AppStateStatus = AppState.currentState
let lastActive = lastState === 'active' ? performance.now() : null
AppState.addEventListener('change', (state: AppStateStatus) => {
if (state === lastState) {
return
}
lastState = state
if (state === 'active') {
lastActive = performance.now()
logEvent('state:foreground', {})
} else {
logEvent('state:background', {})
let secondsActive = 0
if (lastActive != null) {
secondsActive = Math.round((performance.now() - lastActive) / 1e3)
}
lastActive = null
logEvent('state:background', {
secondsActive,
})
}
})