Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367import { createContext, useCallback, useContext, useMemo, useSyncExternalStore,} from 'react'import {Platform} from 'react-native'import {type Result, type WidenPrimitives} from '@growthbook/growthbook-react'
import {Logger} from '#/logger'import {recordFeatureFlagEvaluation} from '#/logger/sentry/featureFlags'import { Features, features as feats, init, refresh, setAttributes,} from '#/analytics/features'import { getAndMigrateDeviceId, getDeviceId, getSessionId,} from '#/analytics/identifiers'import { getMetadataForLogger, getNavigationMetadata, type MergeableMetadata, type Metadata, type MetricMetadata,} from '#/analytics/metadata'import {type Metrics, metrics} from '#/analytics/metrics'import * as refParams from '#/analytics/misc/refParams'import * as env from '#/env'import {account} from '#/storage'
export * as utils from '#/analytics/utils'export const features = {init, refresh}export {Features} from '#/analytics/features'export {type Metrics} from '#/analytics/metrics'
type LoggerType = { debug: Logger['debug'] info: Logger['info'] log: Logger['log'] warn: Logger['warn'] error: Logger['error'] /** * Clones the existing logger and overrides the `context` value. Existing * metadata is inherited. * * ```ts * const ax = useAnalytics() * const logger = ax.logger.useChild(ax.logger.Context.Notifications) * ``` */ useChild: (context: Exclude<Logger['context'], undefined>) => LoggerType Context: typeof Logger.Context}export type AnalyticsContextType = { metadata: Metadata logger: LoggerType metric: <E extends keyof Metrics>( event: E, payload: Metrics[E], metadata?: MergeableMetadata, ) => void features: typeof Features & { enabled(feature: Features): boolean getValue<T>(feature: Features, defaultValue: T): WidenPrimitives<T> }}export type AnalyticsBaseContextType = Omit<AnalyticsContextType, 'features'>
function createLogger( context: Logger['context'], metadata: Record<string, unknown>,): LoggerType { const logger = Logger.create(context, metadata) const currentLogger = () => { // Replace the snapshot so previously recorded entries stay unchanged. logger.ambientMetadata = { ...metadata, deviceId: metadata.deviceId ?? getDeviceId() ?? 'unknown', sessionId: getSessionId(), } return logger } return { debug: (...args) => currentLogger().debug(...args), info: (...args) => currentLogger().info(...args), log: (...args) => currentLogger().log(...args), warn: (...args) => currentLogger().warn(...args), error: (...args) => currentLogger().error(...args), useChild: (context: Exclude<Logger['context'], undefined>) => { // oxlint-disable-next-line react-hooks/exhaustive-deps return useMemo(() => createLogger(context, metadata), [context, metadata]) }, Context: Logger.Context, }}
const Context = createContext<AnalyticsBaseContextType>({ logger: createLogger(Logger.Context.Default, {}), /** * Session IDs are captured when an event is emitted. Deferred exposure events * use the reporting session, which may differ from the evaluation session. */ metric: (event, payload, metadata: Partial<Metadata> = {}) => { const snapshot: MetricMetadata = { ...metadata, base: { ...metadata.base, deviceId: metadata.base?.deviceId ?? getDeviceId() ?? 'unknown', sessionId: getSessionId(), }, navigation: getNavigationMetadata(), } if ('__meta' in snapshot) { delete snapshot.__meta } metrics.track(event, payload, snapshot) }, metadata: { base: { deviceId: getDeviceId() ?? 'unknown', platform: Platform.OS, appVersion: env.APP_VERSION, bundleIdentifier: env.BUNDLE_IDENTIFIER, bundleDate: env.BUNDLE_DATE, referrerSrc: refParams.src, referrerUrl: refParams.url, }, geolocation: { countryCode: '', regionCode: '', }, },})Context.displayName = 'AnalyticsContext'
/** * Ensures that deviceId is set and migrated from legacy storage. Handled on * startup in `App.<platform>.tsx`. This must be awaited prior to the app * booting up. */export const setupDeviceId = getAndMigrateDeviceId()
/** * Reads the per-account cached `isBetaUser` flag for `did`, kept in sync with * writes from `BetaUserStorageSync` and the beta settings toggle. * * This deliberately does not use `useStorage`, whose `useState` seeds once and * only updates via the change listener. The consuming `AnalyticsContext` lives * above the `<Fragment key={did}>` remount breaker, so on an account switch it * re-renders (with a new did) rather than remounting. `useStorage` would keep * serving the previous account's seeded value until a write happened to fire * its listener, leaking a beta account's flag into a non-beta account. Reading * via `useSyncExternalStore` re-evaluates `getSnapshot` every render, so the * value is always correct for the current did. */function useAccountIsBetaUser(did: string | undefined): boolean | undefined { const subscribe = useCallback( (onChange: () => void) => { if (!did) return () => {} const sub = account.addOnValueChangedListener( [did, 'isBetaUser'], onChange, ) return () => sub.remove() }, [did], ) const getSnapshot = useCallback(() => { if (!did) return undefined return account.get([did, 'isBetaUser']) }, [did]) return useSyncExternalStore(subscribe, getSnapshot)}
/** * Analytics context provider. Decorates the parent analytics context with * additional metadata. Nesting should be done carefully and sparingly. */export function AnalyticsContext({ children, metadata,}: { children: React.ReactNode metadata?: MergeableMetadata}) { if (metadata) { if (!('__meta' in metadata)) { throw new Error( 'Use the useMeta() helper when passing metadata to AnalyticsContext', ) } } // Device identity is initialized before mount and stays stable across sessions. const deviceId = getDeviceId() ?? 'unknown' const parentContext = useContext(Context) /* * `isBetaUser` is account-specific, so it's cached per account. Read it * scoped to the did for this render's session (from the `metadata` prop when * set, otherwise inherited from the parent context). Without a did (e.g. * logged out, or the top-level context above the session provider) there's * no value, so beta-gated features are never evaluated for an ineligible or * absent account. */ const did = metadata?.session?.did ?? parentContext.metadata.session?.did const isBetaUser = useAccountIsBetaUser(did) const childContext = useMemo(() => { const combinedMetadata = { ...parentContext.metadata, ...metadata, base: { ...parentContext.metadata.base, deviceId, isBetaUser, }, } const context: AnalyticsBaseContextType = { ...parentContext, logger: createLogger( Logger.Context.Default, getMetadataForLogger(combinedMetadata), ), metadata: combinedMetadata, metric: (event, payload, extraMetadata) => { parentContext.metric(event, payload, { ...combinedMetadata, ...extraMetadata, }) }, } return context }, [parentContext, metadata, deviceId, isBetaUser]) return <Context.Provider value={childContext}>{children}</Context.Provider>}
/** * GrowthBook attribute name for the did. Must match the key used in * `setAttributes` (`#/analytics/features`) and the assignment unit configured * in the GrowthBook dashboard. */const DID_HASH_ATTRIBUTE = 'did'
/** * Builds the session metadata override for an exposure event so the event's * `did` is sourced from the unit GrowthBook actually bucketed on * (`result.hashValue`) rather than from ambient React session metadata. This * keeps the `did` and the variation in sync, since both then come from the * same evaluation. See APP-2461. * * Only did-bucketed experiments are overridden. Experiments bucketed on * another attribute (e.g. `deviceId`) have a `hashValue` that is not a did, so * we leave their session metadata untouched and let the ambient did stand. */function sessionMetadataForResult( parentContext: AnalyticsBaseContextType, result: Result<unknown>,): MergeableMetadata | undefined { if (result.hashAttribute !== DID_HASH_ATTRIBUTE) return undefined const {session} = parentContext.metadata if (!session) return undefined return { session: { ...session, did: result.hashValue, }, }}
/** * Feature gates provider. Decorates the parent analytics context with * feature gate capabilities. Should be mounted within `AnalyticsContext`, * and below the `<Fragment key={did} />` breaker in `App.<platform>.tsx`. */export function AnalyticsFeaturesContext({ children,}: { children: React.ReactNode}) { const parentContext = useContext(Context)
/** * Side-effects: we need to synchronously set these during the same render * cycle. These calls do not trigger re-renders, they just set properties on * the singleton GrowthBook instance. * * Order matters here. We register the tracking callbacks _before_ calling * `setAttributes`, because `setAttributes` triggers a synchronous * re-evaluation that can fire exposure events. Registering first guarantees * those events run through this render's callback (with this render's * metadata) rather than a stale callback left over from a previous render, * e.g. after an account switch remounts this provider via the * `<Fragment key={did} />` breaker in `App.<platform>.tsx`. See APP-2461. * * We deliberately keep these synchronous rather than moving them into a * `useEffect`: `setAttributes` must run before children evaluate gates (or * they bucket on the previous account's attributes), and the feature usage * callback has no deferred-replay, so a feature evaluated before it is * registered would lose its `feature:viewed` event entirely. */ feats.setTrackingCallback((experiment, result) => { parentContext.metric( 'experiment:viewed', { experimentId: experiment.key, variationId: result.key, }, sessionMetadataForResult(parentContext, result), ) }) feats.setFeatureUsageCallback((feature, result) => { parentContext.metric( 'feature:viewed', { featureId: feature, featureResultValue: result.value, experimentId: result.experiment?.key, variationId: result.experimentResult?.key, }, result.experimentResult ? sessionMetadataForResult(parentContext, result.experimentResult) : undefined, ) recordFeatureFlagEvaluation(feature, result.value) }) setAttributes(parentContext.metadata)
const childContext = useMemo<AnalyticsContextType>(() => { return { ...parentContext, features: { enabled: feats.isOn.bind(feats), getValue: feats.getFeatureValue.bind(feats), ...Features, }, } }, [parentContext])
return <Context.Provider value={childContext}>{children}</Context.Provider>}
/** * Basic analytics context without feature gates. Should really only be used * above the `AnalyticsFeaturesContext` provider. */export function useAnalyticsBase() { return useContext(Context)}
/** * The main analytics context, including feature gates. Use this everywhere you * need metrics, features, or logging within the React tree. */export function useAnalytics() { const ctx = useContext(Context) if (!('features' in ctx)) { throw new Error( 'useAnalytics must be used within an AnalyticsFeaturesContext', ) } return ctx as AnalyticsContextType}