Clean up core analytics file
This commit is contained in:
+1
-1
@@ -880,7 +880,7 @@ let lastHandledNotificationDateDedupe: number | undefined
|
||||
|
||||
function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
||||
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 {currentAccount, accounts} = useSession()
|
||||
const {onPressSwitchAccount} = useAccountSwitcher()
|
||||
|
||||
+19
-8
@@ -18,6 +18,7 @@ import {
|
||||
import {type Metrics, metrics} from '#/analytics/metrics'
|
||||
import * as refParams from '#/analytics/misc/refParams'
|
||||
import {type MergeableMetadata, type Metadata} from '#/analytics/types'
|
||||
import {getMetadataForLogger} from '#/analytics/utils'
|
||||
import * as env from '#/env'
|
||||
import {useGeolocation} from '#/geolocation'
|
||||
import {device} from '#/storage'
|
||||
@@ -34,9 +35,15 @@ type LoggerType = {
|
||||
warn: Logger['warn']
|
||||
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
|
||||
}
|
||||
export type AnalyticsContextType = {
|
||||
@@ -66,7 +73,7 @@ function createLogger(
|
||||
log: logger.log.bind(logger),
|
||||
warn: logger.warn.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])
|
||||
},
|
||||
Context: Logger.Context,
|
||||
@@ -131,13 +138,18 @@ export function AnalyticsContext({
|
||||
const combinedMetadata = {
|
||||
...parentContext.metadata,
|
||||
...metadata,
|
||||
base: {
|
||||
...parentContext.metadata.base,
|
||||
sessionId,
|
||||
},
|
||||
geolocation,
|
||||
}
|
||||
combinedMetadata.base.sessionId = sessionId
|
||||
combinedMetadata.geolocation = geolocation
|
||||
const context: AnalyticsBaseContextType = {
|
||||
...parentContext,
|
||||
// TODO trim down metadata
|
||||
logger: createLogger(Logger.Context.Default, combinedMetadata),
|
||||
logger: createLogger(
|
||||
Logger.Context.Default,
|
||||
getMetadataForLogger(combinedMetadata),
|
||||
),
|
||||
metadata: combinedMetadata,
|
||||
metric: (event, payload, extraMetadata) => {
|
||||
parentContext.metric(event, payload, {
|
||||
@@ -202,7 +214,6 @@ export function useAnalyticsBase() {
|
||||
export function useAnalytics() {
|
||||
const ctx = useContext(Context)
|
||||
if (!('feature' in ctx) || !('Features' in ctx)) {
|
||||
console.log(ctx)
|
||||
throw new Error(
|
||||
'useAnalytics must be used within an AnalyticsFeaturesContext',
|
||||
)
|
||||
|
||||
+21
-1
@@ -2,7 +2,11 @@ import {useMemo} from 'react'
|
||||
|
||||
import {BSKY_SERVICE} from '#/lib/constants'
|
||||
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
|
||||
@@ -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) {
|
||||
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 {_} = useLingui()
|
||||
const ref = React.useRef<ScrollView>(null)
|
||||
|
||||
@@ -77,7 +77,7 @@ let lastHandledNotificationDateDedupe = 0
|
||||
|
||||
export function useNotificationsHandler() {
|
||||
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 {currentAccount, accounts} = useSession()
|
||||
const {onPressSwitchAccount} = useAccountSwitcher()
|
||||
|
||||
@@ -64,7 +64,7 @@ export function useFeedFeedback(
|
||||
hasSession: boolean,
|
||||
) {
|
||||
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 feed =
|
||||
|
||||
Reference in New Issue
Block a user