[Statsig] Sample noisy events (#4288)
* Sample state:background and state:foreground * Sample feed events * Add DEV protection against forgetting to add events to the list
This commit is contained in:
parent
9431201026
commit
d6275e98c2
7 changed files with 41 additions and 21 deletions
|
@ -20,10 +20,10 @@ export type LogEvents = {
|
|||
context: 'StartOnboarding' | 'AfterOnboarding' | 'Login'
|
||||
status: 'granted' | 'denied' | 'undetermined'
|
||||
}
|
||||
'state:background': {
|
||||
'state:background:sampled': {
|
||||
secondsActive: number
|
||||
}
|
||||
'state:foreground': {}
|
||||
'state:foreground:sampled': {}
|
||||
'router:navigate:sampled': {}
|
||||
|
||||
// Screen events
|
||||
|
@ -57,18 +57,18 @@ export type LogEvents = {
|
|||
'onboarding:finished:avatarResult': {
|
||||
avatarResult: 'default' | 'created' | 'uploaded'
|
||||
}
|
||||
'home:feedDisplayed': {
|
||||
'home:feedDisplayed:sampled': {
|
||||
feedUrl: string
|
||||
feedType: string
|
||||
index: number
|
||||
reason: 'focus' | 'tabbar-click' | 'pager-swipe' | 'desktop-sidebar-click'
|
||||
}
|
||||
'feed:endReached': {
|
||||
'feed:endReached:sampled': {
|
||||
feedUrl: string
|
||||
feedType: string
|
||||
itemCount: number
|
||||
}
|
||||
'feed:refresh': {
|
||||
'feed:refresh:sampled': {
|
||||
feedUrl: string
|
||||
feedType: string
|
||||
reason: 'pull-to-refresh' | 'soft-reset' | 'load-latest'
|
||||
|
|
|
@ -87,7 +87,14 @@ export function toClout(n: number | null | undefined): number | undefined {
|
|||
}
|
||||
}
|
||||
|
||||
const DOWNSAMPLED_EVENTS = new Set(['router:navigate:sampled'])
|
||||
const DOWNSAMPLED_EVENTS: Set<keyof LogEvents> = new Set([
|
||||
'router:navigate:sampled',
|
||||
'state:background:sampled',
|
||||
'state:foreground:sampled',
|
||||
'home:feedDisplayed:sampled',
|
||||
'feed:endReached:sampled',
|
||||
'feed:refresh:sampled',
|
||||
])
|
||||
const isDownsampledSession = Math.random() < 0.9 // 90% likely
|
||||
|
||||
export function logEvent<E extends keyof LogEvents>(
|
||||
|
@ -98,6 +105,13 @@ export function logEvent<E extends keyof LogEvents>(
|
|||
if (isDownsampledSession && DOWNSAMPLED_EVENTS.has(eventName)) {
|
||||
return
|
||||
}
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
if (eventName.endsWith(':sampled')) {
|
||||
logger.error(
|
||||
'Did you forget to add ' + eventName + ' to DOWNSAMPLED_EVENTS?',
|
||||
)
|
||||
}
|
||||
}
|
||||
const fullMetadata = {
|
||||
...rawMetadata,
|
||||
} as Record<string, string> // Statsig typings are unnecessarily strict here.
|
||||
|
@ -199,14 +213,14 @@ AppState.addEventListener('change', (state: AppStateStatus) => {
|
|||
lastState = state
|
||||
if (state === 'active') {
|
||||
lastActive = performance.now()
|
||||
logEvent('state:foreground', {})
|
||||
logEvent('state:foreground:sampled', {})
|
||||
} else {
|
||||
let secondsActive = 0
|
||||
if (lastActive != null) {
|
||||
secondsActive = Math.round((performance.now() - lastActive) / 1e3)
|
||||
}
|
||||
lastActive = null
|
||||
logEvent('state:background', {
|
||||
logEvent('state:background:sampled', {
|
||||
secondsActive,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue