Skip to content

Commit

Permalink
[Statsig] Include OS and track app state changes (#3273)
Browse files Browse the repository at this point in the history
* Include platform in identify

* Track back/foregrounding
  • Loading branch information
gaearon authored Mar 19, 2024
1 parent e9222a9 commit 5bec587
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib/statsig/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type LogEvents = {
init: {
initMs: number
}
'state:background': {}
'state:foreground': {}
'feed:endReached': {
feedType: string
itemCount: number
Expand Down
20 changes: 19 additions & 1 deletion src/lib/statsig/statsig.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react'
import {Platform} from 'react-native'
import {
Statsig,
StatsigProvider,
useGate as useStatsigGate,
} from 'statsig-react-native-expo'
import {AppState, AppStateStatus} from 'react-native'
import {useSession} from '../../state/session'
import {sha256} from 'js-sha256'
import {LogEvents} from './events'
Expand Down Expand Up @@ -58,9 +60,25 @@ function toStatsigUser(did: string | undefined) {
if (did) {
userID = sha256(did)
}
return {userID}
return {
userID,
platform: Platform.OS,
}
}

let lastState: AppStateStatus = AppState.currentState
AppState.addEventListener('change', (state: AppStateStatus) => {
if (state === lastState) {
return
}
lastState = state
if (state === 'active') {
logEvent('state:foreground', {})
} else {
logEvent('state:background', {})
}
})

export function Provider({children}: {children: React.ReactNode}) {
const {currentAccount} = useSession()
const currentStatsigUser = React.useMemo(
Expand Down

0 comments on commit 5bec587

Please sign in to comment.