Clean up core analytics file

This commit is contained in:
Eric Bailey
2026-01-21 20:55:55 -06:00
parent a595eb3264
commit 10c44a0e20
6 changed files with 44 additions and 13 deletions
+1 -1
View File
@@ -880,7 +880,7 @@ let lastHandledNotificationDateDedupe: number | undefined
function RoutesContainer({children}: React.PropsWithChildren<{}>) { function RoutesContainer({children}: React.PropsWithChildren<{}>) {
const ax = useAnalytics() const ax = useAnalytics()
const notyLogger = ax.logger.useContext(ax.logger.Context.Notifications) const notyLogger = ax.logger.useChild(ax.logger.Context.Notifications)
const theme = useColorSchemeStyle(DefaultTheme, DarkTheme) const theme = useColorSchemeStyle(DefaultTheme, DarkTheme)
const {currentAccount, accounts} = useSession() const {currentAccount, accounts} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher() const {onPressSwitchAccount} = useAccountSwitcher()
+19 -8
View File
@@ -18,6 +18,7 @@ import {
import {type Metrics, metrics} from '#/analytics/metrics' import {type Metrics, metrics} from '#/analytics/metrics'
import * as refParams from '#/analytics/misc/refParams' import * as refParams from '#/analytics/misc/refParams'
import {type MergeableMetadata, type Metadata} from '#/analytics/types' import {type MergeableMetadata, type Metadata} from '#/analytics/types'
import {getMetadataForLogger} from '#/analytics/utils'
import * as env from '#/env' import * as env from '#/env'
import {useGeolocation} from '#/geolocation' import {useGeolocation} from '#/geolocation'
import {device} from '#/storage' import {device} from '#/storage'
@@ -34,9 +35,15 @@ type LoggerType = {
warn: Logger['warn'] warn: Logger['warn']
error: Logger['error'] error: Logger['error']
/** /**
* Creates a clone of the existing logger and overrides the `context` value * 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)
* ```
*/ */
useContext: (context: Exclude<Logger['context'], undefined>) => LoggerType useChild: (context: Exclude<Logger['context'], undefined>) => LoggerType
Context: typeof Logger.Context Context: typeof Logger.Context
} }
export type AnalyticsContextType = { export type AnalyticsContextType = {
@@ -66,7 +73,7 @@ function createLogger(
log: logger.log.bind(logger), log: logger.log.bind(logger),
warn: logger.warn.bind(logger), warn: logger.warn.bind(logger),
error: logger.error.bind(logger), error: logger.error.bind(logger),
useContext: (context: Exclude<Logger['context'], undefined>) => { useChild: (context: Exclude<Logger['context'], undefined>) => {
return useMemo(() => createLogger(context, metadata), [context, metadata]) return useMemo(() => createLogger(context, metadata), [context, metadata])
}, },
Context: Logger.Context, Context: Logger.Context,
@@ -131,13 +138,18 @@ export function AnalyticsContext({
const combinedMetadata = { const combinedMetadata = {
...parentContext.metadata, ...parentContext.metadata,
...metadata, ...metadata,
base: {
...parentContext.metadata.base,
sessionId,
},
geolocation,
} }
combinedMetadata.base.sessionId = sessionId
combinedMetadata.geolocation = geolocation
const context: AnalyticsBaseContextType = { const context: AnalyticsBaseContextType = {
...parentContext, ...parentContext,
// TODO trim down metadata logger: createLogger(
logger: createLogger(Logger.Context.Default, combinedMetadata), Logger.Context.Default,
getMetadataForLogger(combinedMetadata),
),
metadata: combinedMetadata, metadata: combinedMetadata,
metric: (event, payload, extraMetadata) => { metric: (event, payload, extraMetadata) => {
parentContext.metric(event, payload, { parentContext.metric(event, payload, {
@@ -202,7 +214,6 @@ export function useAnalyticsBase() {
export function useAnalytics() { export function useAnalytics() {
const ctx = useContext(Context) const ctx = useContext(Context)
if (!('feature' in ctx) || !('Features' in ctx)) { if (!('feature' in ctx) || !('Features' in ctx)) {
console.log(ctx)
throw new Error( throw new Error(
'useAnalytics must be used within an AnalyticsFeaturesContext', 'useAnalytics must be used within an AnalyticsFeaturesContext',
) )
+21 -1
View File
@@ -2,7 +2,11 @@ import {useMemo} from 'react'
import {BSKY_SERVICE} from '#/lib/constants' import {BSKY_SERVICE} from '#/lib/constants'
import {type SessionAccount} from '#/state/session' import {type SessionAccount} from '#/state/session'
import {type MergeableMetadata, type SessionMetadata} from '#/analytics/types' import {
type MergeableMetadata,
type Metadata,
type SessionMetadata,
} from '#/analytics/types'
/** /**
* Thin `useMemo` wrapper that marks the metadata as memoized and provides a * Thin `useMemo` wrapper that marks the metadata as memoized and provides a
@@ -27,3 +31,19 @@ export function accountToSessionMetadata(
} }
} }
} }
export function getMetadataForLogger({
base,
geolocation,
session,
}: Metadata): Record<string, any> {
return {
deviceId: base.deviceId,
sessionId: base.sessionId,
platform: base.platform,
appVersion: base.appVersion,
countryCode: geolocation.countryCode,
regionCode: geolocation.regionCode,
isBskyPds: session?.isBskyPds || 'anonymous',
}
}
@@ -103,7 +103,7 @@ function Invalid() {
function Inner(props: ReportDialogProps) { function Inner(props: ReportDialogProps) {
const ax = useAnalytics() const ax = useAnalytics()
const logger = ax.logger.useContext(ax.logger.Context.ReportDialog) const logger = ax.logger.useChild(ax.logger.Context.ReportDialog)
const t = useTheme() const t = useTheme()
const {_} = useLingui() const {_} = useLingui()
const ref = React.useRef<ScrollView>(null) const ref = React.useRef<ScrollView>(null)
+1 -1
View File
@@ -77,7 +77,7 @@ let lastHandledNotificationDateDedupe = 0
export function useNotificationsHandler() { export function useNotificationsHandler() {
const ax = useAnalytics() const ax = useAnalytics()
const logger = ax.logger.useContext(ax.logger.Context.Notifications) const logger = ax.logger.useChild(ax.logger.Context.Notifications)
const queryClient = useQueryClient() const queryClient = useQueryClient()
const {currentAccount, accounts} = useSession() const {currentAccount, accounts} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher() const {onPressSwitchAccount} = useAccountSwitcher()
+1 -1
View File
@@ -64,7 +64,7 @@ export function useFeedFeedback(
hasSession: boolean, hasSession: boolean,
) { ) {
const ax = useAnalytics() const ax = useAnalytics()
const logger = ax.logger.useContext(ax.logger.Context.FeedFeedback) const logger = ax.logger.useChild(ax.logger.Context.FeedFeedback)
const agent = useAgent() const agent = useAgent()
const feed = const feed =