Skip to content

Commit

Permalink
[Statsig] Instrument feed display (#3455)
Browse files Browse the repository at this point in the history
* [Statsig] Instrument feed display

* Back out leftover change
  • Loading branch information
gaearon authored Apr 9, 2024
1 parent c96bc92 commit c300d4c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
6 changes: 6 additions & 0 deletions src/lib/statsig/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export type LogEvents = {
}
'onboarding:moderation:nextPressed': {}
'onboarding:finished:nextPressed': {}
'home:feedDisplayed': {
feedUrl: string
feedType: string
index: number
reason: 'focus' | 'tabbar-click' | 'pager-swipe' | 'desktop-sidebar-click'
}
'feed:endReached': {
feedUrl: string
feedType: string
Expand Down
28 changes: 21 additions & 7 deletions src/view/com/pager/Pager.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import React, {forwardRef} from 'react'
import {Animated, View} from 'react-native'
import PagerView, {
PagerViewOnPageSelectedEvent,
PagerViewOnPageScrollEvent,
PagerViewOnPageSelectedEvent,
PageScrollStateChangedNativeEvent,
} from 'react-native-pager-view'

import {LogEvents} from '#/lib/statsig/events'
import {s} from 'lib/styles'

export type PageSelectedEvent = PagerViewOnPageSelectedEvent
const AnimatedPagerView = Animated.createAnimatedComponent(PagerView)

export interface PagerRef {
setPage: (index: number) => void
setPage: (
index: number,
reason: LogEvents['home:feedDisplayed']['reason'],
) => void
}

export interface RenderTabBarFnProps {
Expand All @@ -25,7 +30,10 @@ interface Props {
initialPage?: number
renderTabBar: RenderTabBarFn
onPageSelected?: (index: number) => void
onPageSelecting?: (index: number) => void
onPageSelecting?: (
index: number,
reason: LogEvents['home:feedDisplayed']['reason'],
) => void
onPageScrollStateChanged?: (
scrollState: 'idle' | 'dragging' | 'settling',
) => void
Expand All @@ -51,7 +59,13 @@ export const Pager = forwardRef<PagerRef, React.PropsWithChildren<Props>>(
const pagerView = React.useRef<PagerView>(null)

React.useImperativeHandle(ref, () => ({
setPage: (index: number) => pagerView.current?.setPage(index),
setPage: (
index: number,
reason: LogEvents['home:feedDisplayed']['reason'],
) => {
pagerView.current?.setPage(index)
onPageSelecting?.(index, reason)
},
}))

const onPageSelectedInner = React.useCallback(
Expand Down Expand Up @@ -79,14 +93,14 @@ export const Pager = forwardRef<PagerRef, React.PropsWithChildren<Props>>(
// -prf
if (scrollState.current === 'settling') {
if (lastDirection.current === -1 && offset < lastOffset.current) {
onPageSelecting?.(position)
onPageSelecting?.(position, 'pager-swipe')
setSelectedPage(position)
lastDirection.current = 0
} else if (
lastDirection.current === 1 &&
offset > lastOffset.current
) {
onPageSelecting?.(position + 1)
onPageSelecting?.(position + 1, 'pager-swipe')
setSelectedPage(position + 1)
lastDirection.current = 0
}
Expand All @@ -113,7 +127,7 @@ export const Pager = forwardRef<PagerRef, React.PropsWithChildren<Props>>(
const onTabBarSelect = React.useCallback(
(index: number) => {
pagerView.current?.setPage(index)
onPageSelecting?.(index)
onPageSelecting?.(index, 'tabbar-click')
},
[pagerView, onPageSelecting],
)
Expand Down
22 changes: 16 additions & 6 deletions src/view/com/pager/Pager.web.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import {flushSync} from 'react-dom'
import {View} from 'react-native'
import {flushSync} from 'react-dom'

import {LogEvents} from '#/lib/statsig/events'
import {s} from 'lib/styles'

export interface RenderTabBarFnProps {
Expand All @@ -14,7 +16,10 @@ interface Props {
initialPage?: number
renderTabBar: RenderTabBarFn
onPageSelected?: (index: number) => void
onPageSelecting?: (index: number) => void
onPageSelecting?: (
index: number,
reason: LogEvents['home:feedDisplayed']['reason'],
) => void
}
export const Pager = React.forwardRef(function PagerImpl(
{
Expand All @@ -31,11 +36,16 @@ export const Pager = React.forwardRef(function PagerImpl(
const anchorRef = React.useRef(null)

React.useImperativeHandle(ref, () => ({
setPage: (index: number) => onTabBarSelect(index),
setPage: (
index: number,
reason: LogEvents['home:feedDisplayed']['reason'],
) => {
onTabBarSelect(index, reason)
},
}))

const onTabBarSelect = React.useCallback(
(index: number) => {
(index: number, reason: LogEvents['home:feedDisplayed']['reason']) => {
const scrollY = window.scrollY
// We want to determine if the tabbar is already "sticking" at the top (in which
// case we should preserve and restore scroll), or if it is somewhere below in the
Expand All @@ -54,7 +64,7 @@ export const Pager = React.forwardRef(function PagerImpl(
flushSync(() => {
setSelectedPage(index)
onPageSelected?.(index)
onPageSelecting?.(index)
onPageSelecting?.(index, reason)
})
if (isSticking) {
const restoredScrollY = scrollYs.current[index]
Expand All @@ -73,7 +83,7 @@ export const Pager = React.forwardRef(function PagerImpl(
{renderTabBar({
selectedPage,
tabBarAnchor: <View ref={anchorRef} />,
onSelect: onTabBarSelect,
onSelect: e => onTabBarSelect(e, 'tabbar-click'),
})}
{React.Children.map(children, (child, i) => (
<View style={selectedPage === i ? s.flex1 : s.hidden} key={`page-${i}`}>
Expand Down
30 changes: 28 additions & 2 deletions src/view/screens/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React from 'react'
import {ActivityIndicator, AppState, StyleSheet, View} from 'react-native'
import {useFocusEffect} from '@react-navigation/native'

import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
import {useSetTitle} from '#/lib/hooks/useSetTitle'
import {useGate} from '#/lib/statsig/statsig'
import {logEvent, LogEvents, useGate} from '#/lib/statsig/statsig'
import {emitSoftReset} from '#/state/events'
import {FeedSourceInfo, usePinnedFeedsInfos} from '#/state/queries/feed'
import {FeedDescriptor, FeedParams} from '#/state/queries/post-feed'
Expand Down Expand Up @@ -79,7 +80,7 @@ function HomeScreenReady({
// This is supposed to only happen on the web when you use the right nav.
if (selectedIndex !== lastPagerReportedIndexRef.current) {
lastPagerReportedIndexRef.current = selectedIndex
pagerRef.current?.setPage(selectedIndex)
pagerRef.current?.setPage(selectedIndex, 'desktop-sidebar-click')
}
}, [selectedIndex])

Expand All @@ -96,6 +97,17 @@ function HomeScreenReady({
}, [setDrawerSwipeDisabled, selectedIndex, setMinimalShellMode]),
)

useFocusEffect(
useNonReactiveCallback(() => {
logEvent('home:feedDisplayed', {
index: selectedIndex,
feedType: selectedFeed.split('|')[0],
feedUrl: selectedFeed,
reason: 'focus',
})
}),
)

const disableMinShellOnForegrounding = useGate(
'disable_min_shell_on_foregrounding',
)
Expand Down Expand Up @@ -123,6 +135,19 @@ function HomeScreenReady({
[setDrawerSwipeDisabled, setSelectedFeed, setMinimalShellMode, allFeeds],
)

const onPageSelecting = React.useCallback(
(index: number, reason: LogEvents['home:feedDisplayed']['reason']) => {
const feed = allFeeds[index]
logEvent('home:feedDisplayed', {
index,
feedType: feed.split('|')[0],
feedUrl: feed,
reason,
})
},
[allFeeds],
)

const onPressSelected = React.useCallback(() => {
emitSoftReset()
}, [])
Expand Down Expand Up @@ -175,6 +200,7 @@ function HomeScreenReady({
ref={pagerRef}
testID="homeScreen"
initialPage={selectedIndex}
onPageSelecting={onPageSelecting}
onPageSelected={onPageSelected}
onPageScrollStateChanged={onPageScrollStateChanged}
renderTabBar={renderTabBar}>
Expand Down

0 comments on commit c300d4c

Please sign in to comment.