Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/logger/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,24 @@ export type MetricEvents = {
'feed:suggestion:press': {
feedUrl: string
}
'discover:showMore': {
'feed:showMore': {
feed: string
feedContext: string
}
'discover:showLess': {
'feed:showLess': {
feed: string
feedContext: string
}
'discover:clickthrough': {
'feed:clickthrough': {
feed: string
count: number
}
'discover:engaged': {
'feed:engaged': {
feed: string
count: number
}
'discover:seen': {
'feed:seen': {
feed: string
count: number
}

Expand Down
38 changes: 26 additions & 12 deletions src/state/feed-feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import throttle from 'lodash.throttle'

import {PROD_FEEDS, STAGING_FEEDS} from '#/lib/constants'
import {isNetworkError} from '#/lib/hooks/useCleanError'
import {logEvent} from '#/lib/statsig/statsig'
import {Logger} from '#/logger'
import {
type FeedSourceFeedInfo,
Expand Down Expand Up @@ -78,11 +77,19 @@ export function useFeedFeedback(
const aggregatedStats = useRef<AggregatedStats | null>(null)
const throttledFlushAggregatedStats = useMemo(
() =>
throttle(() => flushToStatsig(aggregatedStats.current), 45e3, {
leading: true, // The outer call is already throttled somewhat.
trailing: true,
}),
[],
throttle(
() =>
flushToStatsig(
aggregatedStats.current,
feed?.feedDescriptor ?? 'unknown',
),
45e3,
{
leading: true, // The outer call is already throttled somewhat.
trailing: true,
},
),
[feed?.feedDescriptor],
)

const sendToFeedNoDelay = useCallback(() => {
Expand Down Expand Up @@ -123,6 +130,7 @@ export function useFeedFeedback(
sendOrAggregateInteractionsForStats(
aggregatedStats.current,
interactionsToSend,
feed?.feedDescriptor ?? 'unknown',
)
throttledFlushAggregatedStats()
logger.debug('flushed')
Expand Down Expand Up @@ -259,19 +267,22 @@ function createAggregatedStats(): AggregatedStats {
function sendOrAggregateInteractionsForStats(
stats: AggregatedStats,
interactions: AppBskyFeedDefs.Interaction[],
feed: string,
) {
for (let interaction of interactions) {
switch (interaction.event) {
// Pressing "Show more" / "Show less" is relatively uncommon so we won't aggregate them.
// This lets us send the feed context together with them.
case 'app.bsky.feed.defs#requestLess': {
logEvent('discover:showLess', {
logger.metric('feed:showLess', {
feed,
feedContext: interaction.feedContext ?? '',
})
break
}
case 'app.bsky.feed.defs#requestMore': {
logEvent('discover:showMore', {
logger.metric('feed:showMore', {
feed,
feedContext: interaction.feedContext ?? '',
})
break
Expand Down Expand Up @@ -301,28 +312,31 @@ function sendOrAggregateInteractionsForStats(
}
}

function flushToStatsig(stats: AggregatedStats | null) {
function flushToStatsig(stats: AggregatedStats | null, feedDescriptor: string) {
if (stats === null) {
return
}

if (stats.clickthroughCount > 0) {
logEvent('discover:clickthrough', {
logger.metric('feed:clickthrough', {
count: stats.clickthroughCount,
feed: feedDescriptor,
})
stats.clickthroughCount = 0
}

if (stats.engagedCount > 0) {
logEvent('discover:engaged', {
logger.metric('feed:engaged', {
count: stats.engagedCount,
feed: feedDescriptor,
})
stats.engagedCount = 0
}

if (stats.seenCount > 0) {
logEvent('discover:seen', {
logger.metric('feed:seen', {
count: stats.seenCount,
feed: feedDescriptor,
})
stats.seenCount = 0
}
Expand Down
Loading