Adjust datalake abstraction

(cherry picked from commit 8ba6a8d45b1bd5698afbd06d9e858a91789f0ea6)
This commit is contained in:
Eric Bailey
2025-02-28 11:38:17 -06:00
parent 36a7a8d8c6
commit e43b74d34d
3 changed files with 27 additions and 13 deletions
+8 -4
View File
@@ -729,16 +729,20 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
linking={LINKING} linking={LINKING}
theme={theme} theme={theme}
onStateChange={() => { onStateChange={() => {
logEvent('lake:router:navigate', { logEvent(
from: prevLoggedRouteName.current, 'router:navigate',
}) {
from: prevLoggedRouteName.current,
},
{lake: true},
)
prevLoggedRouteName.current = getCurrentRouteName() prevLoggedRouteName.current = getCurrentRouteName()
}} }}
onReady={() => { onReady={() => {
attachRouteToLogEvents(getCurrentRouteName) attachRouteToLogEvents(getCurrentRouteName)
logModuleInitTime() logModuleInitTime()
onReady() onReady()
logEvent('lake:router:navigate', {}) logEvent('router:navigate', {}, {lake: true})
}}> }}>
{children} {children}
</NavigationContainer> </NavigationContainer>
+17 -7
View File
@@ -5,12 +5,12 @@ import {Statsig, StatsigProvider} from 'statsig-react-native-expo'
import {BUNDLE_DATE, BUNDLE_IDENTIFIER, IS_TESTFLIGHT} from '#/lib/app-info' import {BUNDLE_DATE, BUNDLE_IDENTIFIER, IS_TESTFLIGHT} from '#/lib/app-info'
import {logger} from '#/logger' import {logger} from '#/logger'
import {MetricEvents} from '#/logger/metrics'
import {isWeb} from '#/platform/detection' import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted' import * as persisted from '#/state/persisted'
import {useSession} from '../../state/session' import {useSession} from '../../state/session'
import {timeout} from '../async/timeout' import {timeout} from '../async/timeout'
import {useNonReactiveCallback} from '../hooks/useNonReactiveCallback' import {useNonReactiveCallback} from '../hooks/useNonReactiveCallback'
import {LogEvents} from './events'
import {Gate} from './gates' import {Gate} from './gates'
const SDK_KEY = 'client-SXJakO39w9vIhl3D44u8UupyzFl4oZ2qPIkjwcvuPsV' const SDK_KEY = 'client-SXJakO39w9vIhl3D44u8UupyzFl4oZ2qPIkjwcvuPsV'
@@ -42,7 +42,7 @@ if (isWeb && typeof window !== 'undefined') {
refUrl = decodeURIComponent(params.get('ref_url') ?? '') refUrl = decodeURIComponent(params.get('ref_url') ?? '')
} }
export type {LogEvents} export type {MetricEvents as LogEvents}
function createStatsigOptions(prefetchUsers: StatsigUser[]) { function createStatsigOptions(prefetchUsers: StatsigUser[]) {
return { return {
@@ -91,15 +91,25 @@ export function toClout(n: number | null | undefined): number | undefined {
} }
} }
export function logEvent<E extends keyof LogEvents>( export function logEvent<E extends keyof MetricEvents>(
eventName: E & string, eventName: E & string,
rawMetadata: LogEvents[E] & FlatJSONRecord, rawMetadata: MetricEvents[E] & FlatJSONRecord,
options: {
/**
* Send to our data lake only, not to StatSig
*/
lake?: boolean
} = {lake: false},
) { ) {
try { try {
const fullMetadata = toStringRecord(rawMetadata) const fullMetadata = toStringRecord(rawMetadata)
fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)' fullMetadata.routeName = getCurrentRouteName() ?? '(Uninitialized)'
if (Statsig.initializeCalled()) { if (Statsig.initializeCalled()) {
Statsig.logEvent(eventName, null, fullMetadata) let ev: string = eventName
if (options.lake) {
ev = `lake:${ev}`
}
Statsig.logEvent(ev, null, fullMetadata)
} }
logger.info(eventName, fullMetadata) logger.info(eventName, fullMetadata)
} catch (e) { } catch (e) {
@@ -108,8 +118,8 @@ export function logEvent<E extends keyof LogEvents>(
} }
} }
function toStringRecord<E extends keyof LogEvents>( function toStringRecord<E extends keyof MetricEvents>(
metadata: LogEvents[E] & FlatJSONRecord, metadata: MetricEvents[E] & FlatJSONRecord,
): Record<string, string> { ): Record<string, string> {
const record: Record<string, string> = {} const record: Record<string, string> = {}
for (let key in metadata) { for (let key in metadata) {
@@ -1,4 +1,4 @@
export type LogEvents = { export type MetricEvents = {
// App events // App events
init: { init: {
initMs: number initMs: number
@@ -30,7 +30,7 @@ export type LogEvents = {
secondsActive: number secondsActive: number
} }
'state:foreground': {} 'state:foreground': {}
'lake:router:navigate': { 'router:navigate': {
from?: string from?: string
} }
'deepLink:referrerReceived': { 'deepLink:referrerReceived': {