Skip to content

Commit

Permalink
[Statsig] Sample router events (#4143)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored May 21, 2024
1 parent 8cec167 commit d6625c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
linking={LINKING}
theme={theme}
onStateChange={() => {
logEvent('router:navigate', {
logEvent('router:navigate:sampled', {
from: prevLoggedRouteName.current,
})
prevLoggedRouteName.current = getCurrentRouteName()
Expand All @@ -620,7 +620,7 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
attachRouteToLogEvents(getCurrentRouteName)
logModuleInitTime()
onReady()
logEvent('router:navigate', {})
logEvent('router:navigate:sampled', {})
}}>
{children}
</NavigationContainer>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/statsig/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type LogEvents = {
secondsActive: number
}
'state:foreground': {}
'router:navigate': {}
'router:navigate:sampled': {}

// Screen events
'splash:signInPressed': {}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/statsig/statsig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@ export function toClout(n: number | null | undefined): number | undefined {
}
}

const DOWNSAMPLED_EVENTS = new Set(['router:navigate:sampled'])
const isDownsampledSession = Math.random() < 0.9 // 90% likely

export function logEvent<E extends keyof LogEvents>(
eventName: E & string,
rawMetadata: LogEvents[E] & FlatJSONRecord,
) {
try {
if (isDownsampledSession && DOWNSAMPLED_EVENTS.has(eventName)) {
return
}
const fullMetadata = {
...rawMetadata,
} as Record<string, string> // Statsig typings are unnecessarily strict here.
Expand Down

0 comments on commit d6625c2

Please sign in to comment.