diff --git a/src/analytics/metrics/types.ts b/src/analytics/metrics/types.ts index b602466ea..9570cacfd 100644 --- a/src/analytics/metrics/types.ts +++ b/src/analytics/metrics/types.ts @@ -7,6 +7,10 @@ import {type Platform} from 'react-native' import {type NotificationReason} from '#/lib/hooks/useNotificationHandler' import {type NotificationType} from '#/state/queries/notifications/types' import {type FeedDescriptor} from '#/state/queries/post-feed' +import { + type RsvpLogContext, + type RsvpMetricStatus, +} from '#/features/events/rsvp-state' import {type LiveEventFeedMetricContext} from '#/features/liveEvents/types' export type Events = { @@ -418,6 +422,19 @@ export type Events = { langs: string logContext: 'Composer' } + 'event:rsvp': { + uri: string + hostDid: string + status: RsvpMetricStatus + previousStatus?: RsvpMetricStatus + logContext: RsvpLogContext + } + 'event:rsvp:remove': { + uri: string + hostDid: string + status: RsvpMetricStatus + logContext: RsvpLogContext + } 'thread:create': { postCount: number isReply: boolean diff --git a/src/features/events/__tests__/events.test.ts b/src/features/events/__tests__/events.test.ts index 7aa2c9fde..86e634e8c 100644 --- a/src/features/events/__tests__/events.test.ts +++ b/src/features/events/__tests__/events.test.ts @@ -10,7 +10,7 @@ import { import {inLiveWindow, isLiveOnStreamplace} from '../live' import {eventPoster} from '../poster' import {coverUrl, parseEventRecord} from '../record' -import {applyRsvp} from '../rsvp-state' +import {applyRsvp, rsvpMetricStatus} from '../rsvp-state' import {mergeEventTimeline} from '../timeline' import { type EventView, @@ -285,6 +285,19 @@ describe('applyRsvp', () => { }) }) +describe('rsvpMetricStatus', () => { + it('shortens the lexicon statuses', () => { + expect(rsvpMetricStatus(RSVP_GOING)).toBe('going') + expect(rsvpMetricStatus(RSVP_INTERESTED)).toBe('interested') + expect(rsvpMetricStatus(RSVP_NOTGOING)).toBe('notgoing') + }) + + it('drops statuses outside the lexicon', () => { + expect(rsvpMetricStatus('going')).toBeUndefined() + expect(rsvpMetricStatus(undefined)).toBeUndefined() + }) +}) + describe('eventPhase', () => { const now = new Date('2026-10-01T20:00:00.000Z') const base = {name: 'x', cancelled: false, links: []} diff --git a/src/features/events/__tests__/rsvp-metrics.test.tsx b/src/features/events/__tests__/rsvp-metrics.test.tsx new file mode 100644 index 000000000..f69cc615e --- /dev/null +++ b/src/features/events/__tests__/rsvp-metrics.test.tsx @@ -0,0 +1,111 @@ +import {QueryClient, QueryClientProvider} from '@tanstack/react-query' +import {act, renderHook} from '@testing-library/react-native' + +const mockMetric = jest.fn() +const mockCreateRecord = jest.fn() +const mockPutRecord = jest.fn() +const mockDeleteRecord = jest.fn() + +jest.mock('#/analytics', () => ({ + useAnalytics: () => ({metric: mockMetric}), +})) +jest.mock('#/state/session', () => ({ + useAgent: () => ({ + com: { + atproto: { + repo: { + createRecord: mockCreateRecord, + putRecord: mockPutRecord, + deleteRecord: mockDeleteRecord, + }, + }, + }, + }), + useSession: () => ({currentAccount: {did: 'did:plc:me'}}), +})) +jest.mock('#/lib/api/community', () => ({communityXrpc: jest.fn()})) +jest.mock('#/state/queries', () => ({STALE: {MINUTES: {ONE: 60e3}}})) + +import {useRsvpMutation} from '../queries' +import { + type EventView, + RSVP_GOING, + RSVP_INTERESTED, + type RsvpStatus, +} from '../types' + +const EVENT_URI = 'at://did:plc:host/community.lexicon.calendar.event/1' +const RSVP_URI = 'at://did:plc:me/community.lexicon.calendar.rsvp/1' + +const event: EventView = { + uri: EVENT_URI, + cid: 'cid', + creator: {did: 'did:plc:host', handle: 'host.test'}, + record: {}, + goingCount: 0, + interestedCount: 0, + indexedAt: '', +} + +async function rsvp( + status: RsvpStatus, + current?: {uri: string; status: string}, +) { + const queryClient = new QueryClient({ + defaultOptions: {mutations: {retry: false}}, + }) + const {result} = renderHook(() => useRsvpMutation(event, 'EventPage'), { + wrapper: ({children}) => ( + {children} + ), + }) + await act(async () => { + await result.current.mutateAsync({status, current}).catch(() => {}) + }) +} + +describe('RSVP metrics', () => { + beforeEach(() => { + jest.clearAllMocks() + mockCreateRecord.mockResolvedValue({data: {uri: RSVP_URI}}) + mockPutRecord.mockResolvedValue({data: {uri: RSVP_URI}}) + mockDeleteRecord.mockResolvedValue({}) + }) + + it('logs a new RSVP', async () => { + await rsvp(RSVP_GOING) + expect(mockMetric).toHaveBeenCalledTimes(1) + expect(mockMetric).toHaveBeenCalledWith('event:rsvp', { + uri: EVENT_URI, + hostDid: 'did:plc:host', + status: 'going', + previousStatus: undefined, + logContext: 'EventPage', + }) + }) + + it('logs a changed RSVP with the previous status', async () => { + await rsvp(RSVP_GOING, {uri: RSVP_URI, status: RSVP_INTERESTED}) + expect(mockMetric).toHaveBeenCalledWith( + 'event:rsvp', + expect.objectContaining({status: 'going', previousStatus: 'interested'}), + ) + }) + + it('logs a removed RSVP', async () => { + await rsvp(RSVP_GOING, {uri: RSVP_URI, status: RSVP_GOING}) + expect(mockMetric).toHaveBeenCalledTimes(1) + expect(mockMetric).toHaveBeenCalledWith('event:rsvp:remove', { + uri: EVENT_URI, + hostDid: 'did:plc:host', + status: 'going', + logContext: 'EventPage', + }) + }) + + it('logs nothing when the write fails', async () => { + mockCreateRecord.mockRejectedValue(new Error('offline')) + await rsvp(RSVP_GOING) + expect(mockMetric).not.toHaveBeenCalled() + }) +}) diff --git a/src/features/events/components/EventCard.tsx b/src/features/events/components/EventCard.tsx index 8094e1f29..fd4fa6210 100644 --- a/src/features/events/components/EventCard.tsx +++ b/src/features/events/components/EventCard.tsx @@ -101,7 +101,7 @@ export function EventCard({ {showRsvp && !parsed.cancelled && phase !== 'past' && ( - + )} diff --git a/src/features/events/components/EventEmbed.tsx b/src/features/events/components/EventEmbed.tsx index 907f6e864..f020b89bb 100644 --- a/src/features/events/components/EventEmbed.tsx +++ b/src/features/events/components/EventEmbed.tsx @@ -128,7 +128,7 @@ export function EventEmbed({ )} - + )} diff --git a/src/features/events/components/RsvpButtons.tsx b/src/features/events/components/RsvpButtons.tsx index ac3d773a6..9377ec3b9 100644 --- a/src/features/events/components/RsvpButtons.tsx +++ b/src/features/events/components/RsvpButtons.tsx @@ -9,6 +9,7 @@ import {Button, ButtonIcon, ButtonText} from '#/components/Button' import {Check_Stroke2_Corner0_Rounded as CheckIcon} from '#/components/icons/Check' import * as Toast from '#/components/Toast' import {useRsvpMutation} from '../queries' +import {type RsvpLogContext} from '../rsvp-state' import { type EventView, RSVP_GOING, @@ -19,14 +20,16 @@ import { export function RsvpButtons({ event, + logContext, size = 'small', }: { event: EventView + logContext: RsvpLogContext size?: 'small' | 'large' }) { const {_} = useLingui() const requireAuth = useRequireAuth() - const {mutate, isPending} = useRsvpMutation(event) + const {mutate, isPending} = useRsvpMutation(event, logContext) const current = event.viewer?.rsvp?.status const onPress = (status: RsvpStatus) => diff --git a/src/features/events/queries.ts b/src/features/events/queries.ts index fbe458a29..763ea80a9 100644 --- a/src/features/events/queries.ts +++ b/src/features/events/queries.ts @@ -11,7 +11,8 @@ import { import {communityXrpc} from '#/lib/api/community' import {STALE} from '#/state/queries' import {useAgent, useSession} from '#/state/session' -import {applyRsvp} from './rsvp-state' +import {useAnalytics} from '#/analytics' +import {applyRsvp, type RsvpLogContext, rsvpMetricStatus} from './rsvp-state' import {mergeEventTimeline} from './timeline' import { type EventView, @@ -187,7 +188,8 @@ export type RsvpChange = { * cached one. The PDS result is written through without refetching, since * the AppView lags the write. */ -export function useRsvpMutation(event: EventView) { +export function useRsvpMutation(event: EventView, logContext: RsvpLogContext) { + const ax = useAnalytics() const agent = useAgent() const queryClient = useQueryClient() const {currentAccount} = useSession() @@ -244,7 +246,21 @@ export function useRsvpMutation(event: EventView) { onError: (_error, _change, context) => { queryClient.setQueryData(queryKey, context?.prev ?? event) }, - onSuccess: result => { + onSuccess: (result, {status, current}) => { + const metric = { + uri: event.uri, + hostDid: event.creator.did, + status: rsvpMetricStatus(status)!, + logContext, + } + if (result) { + ax.metric('event:rsvp', { + ...metric, + previousStatus: rsvpMetricStatus(current?.status), + }) + } else { + ax.metric('event:rsvp:remove', metric) + } const base = queryClient.getQueryData(queryKey) ?? event queryClient.setQueryData(queryKey, { ...base, diff --git a/src/features/events/rsvp-state.ts b/src/features/events/rsvp-state.ts index 2aaff837f..de31fb948 100644 --- a/src/features/events/rsvp-state.ts +++ b/src/features/events/rsvp-state.ts @@ -2,9 +2,26 @@ import { type EventView, RSVP_GOING, RSVP_INTERESTED, + RSVP_NOTGOING, type RsvpStatus, } from './types' +export type RsvpMetricStatus = 'going' | 'interested' | 'notgoing' +export type RsvpLogContext = 'EventEmbed' | 'EventCard' | 'EventPage' + +export function rsvpMetricStatus( + status: string | undefined, +): RsvpMetricStatus | undefined { + switch (status) { + case RSVP_GOING: + return 'going' + case RSVP_INTERESTED: + return 'interested' + case RSVP_NOTGOING: + return 'notgoing' + } +} + const countKey = (status: string | undefined) => status === RSVP_GOING ? 'goingCount' diff --git a/src/screens/Event/EventHeader.tsx b/src/screens/Event/EventHeader.tsx index db0165227..2987338cc 100644 --- a/src/screens/Event/EventHeader.tsx +++ b/src/screens/Event/EventHeader.tsx @@ -257,7 +257,7 @@ function RsvpChoices({event}: {event: EventView}) { const t = useTheme() const {_} = useLingui() const requireAuth = useRequireAuth() - const {mutate, isPending} = useRsvpMutation(event) + const {mutate, isPending} = useRsvpMutation(event, 'EventPage') const current = event.viewer?.rsvp?.status const choices: { status: RsvpStatus