about summary refs log tree commit diff
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-03-20 03:25:37 +0000
committerGitHub <noreply@github.com>2024-03-19 20:25:37 -0700
commit20337ceef1bf86d5190bc240cf11ecf9bfa88042 (patch)
tree75113a43857cbaba570d2e8fe67f708d8da1b21c
parent3d8d1dd1737c11d924bec7d51ae4deb6cb6336b0 (diff)
downloadvoidsky-20337ceef1bf86d5190bc240cf11ecf9bfa88042.tar.zst
[Statsig] Track active time (#3289)
-rw-r--r--src/lib/statsig/events.ts4
-rw-r--r--src/lib/statsig/statsig.tsx11
2 files changed, 13 insertions, 2 deletions
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,
+    })
   }
 })