From 9ecb866497a44e42214e9ec97cdb9f7347fc7f58 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 21 Jan 2026 13:50:10 -0600 Subject: [PATCH] Migrate toClout --- src/analytics/metrics/index.ts | 1 + src/analytics/metrics/utils.ts | 7 +++++++ src/lib/statsig/statsig.tsx | 8 -------- src/state/queries/post.ts | 16 +++++++--------- src/state/queries/profile.ts | 16 +++++++++------- 5 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 src/analytics/metrics/utils.ts diff --git a/src/analytics/metrics/index.ts b/src/analytics/metrics/index.ts index 42de5e72e9..273e710c0d 100644 --- a/src/analytics/metrics/index.ts +++ b/src/analytics/metrics/index.ts @@ -2,4 +2,5 @@ import {MetricsClient} from '#/analytics/metrics/client' import {type Events} from '#/analytics/metrics/types' export type {Events as Metrics} from '#/analytics/metrics/types' +export * from '#/analytics/metrics/utils' export const metrics = new MetricsClient() diff --git a/src/analytics/metrics/utils.ts b/src/analytics/metrics/utils.ts new file mode 100644 index 0000000000..7cca04f924 --- /dev/null +++ b/src/analytics/metrics/utils.ts @@ -0,0 +1,7 @@ +export function toClout(n: number | null | undefined): number | undefined { + if (n == null) { + return undefined + } else { + return Math.max(0, Math.round(Math.log(n))) + } +} diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index 14988b68ca..b108c7bbb8 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -76,14 +76,6 @@ type FlatJSONRecord = Record< let getCurrentRouteName: () => string | null | undefined = () => null -export function toClout(n: number | null | undefined): number | undefined { - if (n == null) { - return undefined - } else { - return Math.max(0, Math.round(Math.log(n))) - } -} - /** * @deprecated use `logger.metric()` instead */ diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts index 3043485b3e..d7c4250335 100644 --- a/src/state/queries/post.ts +++ b/src/state/queries/post.ts @@ -3,12 +3,12 @@ import {type AppBskyActorDefs, type AppBskyFeedDefs, AtUri} from '@atproto/api' import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query' import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue' -import {type LogEvents, toClout} from '#/lib/statsig/statsig' import {logger} from '#/logger' import {updatePostShadow} from '#/state/cache/post-shadow' import {type Shadow} from '#/state/cache/types' import {useAgent, useSession} from '#/state/session' import * as userActionHistory from '#/state/userActionHistory' +import {type Metrics, toClout} from '#/analytics/metrics' import {useIsThreadMuted, useSetThreadMute} from '../cache/thread-mutes' import {findProfileQueryData} from './profile' @@ -103,8 +103,7 @@ export function usePostLikeMutationQueue( post: Shadow, viaRepost: {uri: string; cid: string} | undefined, feedDescriptor: string | undefined, - logContext: LogEvents['post:like']['logContext'] & - LogEvents['post:unlike']['logContext'], + logContext: Metrics['post:like']['logContext'], ) { const queryClient = useQueryClient() const postUri = post.uri @@ -164,7 +163,7 @@ export function usePostLikeMutationQueue( function usePostLikeMutation( feedDescriptor: string | undefined, - logContext: LogEvents['post:like']['logContext'], + logContext: Metrics['post:like']['logContext'], post: Shadow, ) { const {currentAccount} = useSession() @@ -207,7 +206,7 @@ function usePostLikeMutation( function usePostUnlikeMutation( feedDescriptor: string | undefined, - logContext: LogEvents['post:unlike']['logContext'], + logContext: Metrics['post:unlike']['logContext'], post: Shadow, ) { const agent = useAgent() @@ -228,8 +227,7 @@ export function usePostRepostMutationQueue( post: Shadow, viaRepost: {uri: string; cid: string} | undefined, feedDescriptor: string | undefined, - logContext: LogEvents['post:repost']['logContext'] & - LogEvents['post:unrepost']['logContext'], + logContext: Metrics['post:repost']['logContext'], ) { const queryClient = useQueryClient() const postUri = post.uri @@ -291,7 +289,7 @@ export function usePostRepostMutationQueue( function usePostRepostMutation( feedDescriptor: string | undefined, - logContext: LogEvents['post:repost']['logContext'], + logContext: Metrics['post:repost']['logContext'], post: Shadow, ) { const agent = useAgent() @@ -314,7 +312,7 @@ function usePostRepostMutation( function usePostUnrepostMutation( feedDescriptor: string | undefined, - logContext: LogEvents['post:unrepost']['logContext'], + logContext: Metrics['post:unrepost']['logContext'], post: Shadow, ) { const agent = useAgent() diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index 94b362657b..3dcfc46f58 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -22,7 +22,6 @@ import { import {uploadBlob} from '#/lib/api' import {until} from '#/lib/async/until' import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue' -import {logEvent, type LogEvents, toClout} from '#/lib/statsig/statsig' import {updateProfileShadow} from '#/state/cache/profile-shadow' import {type Shadow} from '#/state/cache/types' import {type ImageMeta} from '#/state/gallery' @@ -36,6 +35,8 @@ import { import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache' import {useAgent, useSession} from '#/state/session' import * as userActionHistory from '#/state/userActionHistory' +import {useAnalytics} from '#/analytics' +import {type Metrics, toClout} from '#/analytics/metrics' import type * as bsky from '#/types/bsky' import { ProgressGuideAction, @@ -243,8 +244,7 @@ export function useProfileUpdateMutation() { export function useProfileFollowMutationQueue( profile: Shadow, - logContext: LogEvents['profile:follow']['logContext'] & - LogEvents['profile:follow']['logContext'], + logContext: Metrics['profile:follow']['logContext'], position?: number, contextProfileDid?: string, ) { @@ -364,11 +364,12 @@ export function useProfileFollowMutationQueue( } function useProfileFollowMutation( - logContext: LogEvents['profile:follow']['logContext'], + logContext: Metrics['profile:follow']['logContext'], profile: Shadow, position?: number, contextProfileDid?: string, ) { + const ax = useAnalytics() const {currentAccount} = useSession() const agent = useAgent() const queryClient = useQueryClient() @@ -381,7 +382,7 @@ function useProfileFollowMutation( ownProfile = findProfileQueryData(queryClient, currentAccount.did) } captureAction(ProgressGuideAction.Follow) - logEvent('profile:follow', { + ax.metric('profile:follow', { logContext, didBecomeMutual: profile.viewer ? Boolean(profile.viewer.followedBy) @@ -401,12 +402,13 @@ function useProfileFollowMutation( } function useProfileUnfollowMutation( - logContext: LogEvents['profile:unfollow']['logContext'], + logContext: Metrics['profile:unfollow']['logContext'], ) { + const ax = useAnalytics() const agent = useAgent() return useMutation({ mutationFn: async ({followUri}) => { - logEvent('profile:unfollow', {logContext}) + ax.metric('profile:unfollow', {logContext}) return await agent.deleteFollow(followUri) }, })