From 5bec5877172ac0f18c3c1bebec7ad6c46ca0ae39 Mon Sep 17 00:00:00 2001
From: dan <dan.abramov@gmail.com>
Date: Tue, 19 Mar 2024 20:11:26 +0000
Subject: [Statsig] Include OS and track app state changes (#3273)

* Include platform in identify

* Track back/foregrounding
---
 src/lib/statsig/statsig.tsx | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

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

diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx
index 5745d204a..3abec5c4f 100644
--- a/src/lib/statsig/statsig.tsx
+++ b/src/lib/statsig/statsig.tsx
@@ -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'
@@ -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(
-- 
cgit 1.4.1


From 20337ceef1bf86d5190bc240cf11ecf9bfa88042 Mon Sep 17 00:00:00 2001
From: dan <dan.abramov@gmail.com>
Date: Wed, 20 Mar 2024 03:25:37 +0000
Subject: [Statsig] Track active time (#3289)

---
 src/lib/statsig/events.ts   |  4 +++-
 src/lib/statsig/statsig.tsx | 11 ++++++++++-
 2 files changed, 13 insertions(+), 2 deletions(-)

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

diff --git a/src/lib/statsig/events.ts b/src/lib/statsig/events.ts
index f57c8d416..b83095976 100644
--- a/src/lib/statsig/events.ts
+++ b/src/lib/statsig/events.ts
@@ -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
diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx
index 3abec5c4f..9fa6cce2d 100644
--- a/src/lib/statsig/statsig.tsx
+++ b/src/lib/statsig/statsig.tsx
@@ -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,
+    })
   }
 })
 
-- 
cgit 1.4.1

