From dd86d0964d391e9748843aa1a6400d73c3a6d9f9 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 8 Mar 2024 04:33:42 +0000 Subject: Enable gating and experimentation on native, send init event (#3149) * Add the mobile fork * Add init event --- src/Navigation.tsx | 4 +++ src/lib/statsig/statsig.tsx | 62 +++++++++++++++++++++++++++++++++++++---- src/lib/statsig/statsig.web.tsx | 14 +++++++++- 3 files changed, 74 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Navigation.tsx b/src/Navigation.tsx index b30f8f982..8a9f69b5d 100644 --- a/src/Navigation.tsx +++ b/src/Navigation.tsx @@ -78,6 +78,7 @@ import {createNativeStackNavigatorWithAuth} from './view/shell/createNativeStack import {msg} from '@lingui/macro' import {i18n, MessageDescriptor} from '@lingui/core' import HashtagScreen from '#/screens/Hashtag' +import {logEvent} from './lib/statsig/statsig' const navigationRef = createNavigationContainerRef() @@ -649,11 +650,14 @@ function logModuleInitTime() { return } didInit = true + const initMs = Math.round( // @ts-ignore Emitted by Metro in the bundle prelude performance.now() - global.__BUNDLE_START_TIME__, ) console.log(`Time to first paint: ${initMs} ms`) + logEvent('init', initMs) + if (__DEV__) { // This log is noisy, so keep false committed const shouldLog = false diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index 88a57c3fc..43822dda8 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -1,11 +1,63 @@ import React from 'react' +import { + Statsig, + StatsigProvider, + useGate as useStatsigGate, +} from 'statsig-react-native-expo' +import {useSession} from '../../state/session' +import {sha256} from 'js-sha256' -export function useGate(_gateName: string) { - // Not enabled for native yet. - return false +const statsigOptions = { + environment: { + tier: process.env.NODE_ENV === 'development' ? 'development' : 'production', + }, + // Don't block on waiting for network. The fetched config will kick in on next load. + // This ensures the UI is always consistent and doesn't update mid-session. + // Note this makes cold load (no local storage) and private mode return `false` for all gates. + initTimeoutMs: 1, +} + +export function logEvent( + eventName: string, + value?: string | number | null, + metadata?: Record | null, +) { + Statsig.logEvent(eventName, value, metadata) +} + +export function useGate(gateName: string) { + const {isLoading, value} = useStatsigGate(gateName) + if (isLoading) { + // This should not happen because of waitForInitialization={true}. + console.error('Did not expected isLoading to ever be true.') + } + return value +} + +function toStatsigUser(did: string | undefined) { + let userID: string | undefined + if (did) { + userID = sha256(did) + } + return {userID} } export function Provider({children}: {children: React.ReactNode}) { - // Not enabled for native yet. - return children + const {currentAccount} = useSession() + const currentStatsigUser = React.useMemo( + () => toStatsigUser(currentAccount?.did), + [currentAccount?.did], + ) + return ( + + {children} + + ) } diff --git a/src/lib/statsig/statsig.web.tsx b/src/lib/statsig/statsig.web.tsx index 6508131c4..fc66e8d9f 100644 --- a/src/lib/statsig/statsig.web.tsx +++ b/src/lib/statsig/statsig.web.tsx @@ -1,5 +1,9 @@ import React from 'react' -import {StatsigProvider, useGate as useStatsigGate} from 'statsig-react' +import { + Statsig, + StatsigProvider, + useGate as useStatsigGate, +} from 'statsig-react' import {useSession} from '../../state/session' import {sha256} from 'js-sha256' @@ -13,6 +17,14 @@ const statsigOptions = { initTimeoutMs: 1, } +export function logEvent( + eventName: string, + value?: string | number | null, + metadata?: Record | null, +) { + Statsig.logEvent(eventName, value, metadata) +} + export function useGate(gateName: string) { const {isLoading, value} = useStatsigGate(gateName) if (isLoading) { -- cgit 1.4.1