Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move user prop updater into statsigProvider #7442

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
110 changes: 59 additions & 51 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ export default function App() {
const location = useLocation()
const { pathname } = location
const currentPage = getCurrentPageFromLocation(pathname)
const isDarkMode = useIsDarkMode()
const [routerPreference] = useRouterPreference()

const [scrollY, setScrollY] = useState(0)
const scrolledState = scrollY > 0
const isUniswapXDefaultEnabled = useUniswapXDefaultEnabled()
const userOptedOutOfUniswapX = useUserOptedOutOfUniswapX()

const routerConfig = useRouterConfig()

const originCountry = useAppSelector((state: AppState) => state.user.originCountry)
Expand All @@ -122,53 +120,6 @@ export default function App() {
}
}, [searchParams, setShouldDisableNFTRoutes])

useEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all this code is just relocated and unchanged

// User properties *must* be set before sending corresponding event properties,
// so that the event contains the correct and up-to-date user properties.
user.set(CustomUserProperties.USER_AGENT, navigator.userAgent)
user.set(CustomUserProperties.BROWSER, getBrowser())
user.set(CustomUserProperties.SCREEN_RESOLUTION_HEIGHT, window.screen.height)
user.set(CustomUserProperties.SCREEN_RESOLUTION_WIDTH, window.screen.width)
user.set(CustomUserProperties.GIT_COMMIT_HASH, process.env.REACT_APP_GIT_COMMIT_HASH ?? 'unknown')

// Service Worker analytics
const isServiceWorkerInstalled = Boolean(window.navigator.serviceWorker?.controller)
const isServiceWorkerHit = Boolean((window as any).__isDocumentCached)
const serviceWorkerProperty = isServiceWorkerInstalled ? (isServiceWorkerHit ? 'hit' : 'miss') : 'uninstalled'

const pageLoadProperties = { service_worker: serviceWorkerProperty }
sendInitializationEvent(SharedEventName.APP_LOADED, pageLoadProperties)
const sendWebVital =
(metric: string) =>
({ delta }: Metric) =>
sendAnalyticsEvent(SharedEventName.WEB_VITALS, { ...pageLoadProperties, [metric]: delta })
getCLS(sendWebVital('cumulative_layout_shift'))
getFCP(sendWebVital('first_contentful_paint_ms'))
getFID(sendWebVital('first_input_delay_ms'))
getLCP(sendWebVital('largest_contentful_paint_ms'))
}, [])

useEffect(() => {
user.set(CustomUserProperties.DARK_MODE, isDarkMode)
}, [isDarkMode])

useEffect(() => {
// If we're not in the transition period to UniswapX opt-out, set the router preference to whatever is specified.
if (!isUniswapXDefaultEnabled) {
user.set(CustomUserProperties.ROUTER_PREFERENCE, routerPreference)
return
}

// In the transition period, override the stored API preference to UniswapX if the user hasn't opted out.
if (routerPreference === RouterPreference.API && !userOptedOutOfUniswapX) {
user.set(CustomUserProperties.ROUTER_PREFERENCE, RouterPreference.X)
return
}

// Otherwise, the user has opted out or their preference is UniswapX/client, so set the preference to whatever is specified.
user.set(CustomUserProperties.ROUTER_PREFERENCE, routerPreference)
}, [routerPreference, isUniswapXDefaultEnabled, userOptedOutOfUniswapX])

useEffect(() => {
const scrollListener = () => {
setScrollY(window.scrollY)
Expand Down Expand Up @@ -221,6 +172,7 @@ export default function App() {
api: process.env.REACT_APP_STATSIG_PROXY_URL,
}}
>
<UserPropertyUpdater />
{renderUkBannner && <UkBanner />}
<HeaderWrapper transparent={isHeaderTransparent} bannerIsVisible={renderUkBannner} scrollY={scrollY}>
<NavBar blur={isHeaderTransparent} />
Expand Down Expand Up @@ -255,3 +207,59 @@ export default function App() {
</ErrorBoundary>
)
}

function UserPropertyUpdater() {
const isDarkMode = useIsDarkMode()

const [routerPreference] = useRouterPreference()
const userOptedOutOfUniswapX = useUserOptedOutOfUniswapX()
const isUniswapXDefaultEnabled = useUniswapXDefaultEnabled()

useEffect(() => {
// User properties *must* be set before sending corresponding event properties,
// so that the event contains the correct and up-to-date user properties.
user.set(CustomUserProperties.USER_AGENT, navigator.userAgent)
user.set(CustomUserProperties.BROWSER, getBrowser())
user.set(CustomUserProperties.SCREEN_RESOLUTION_HEIGHT, window.screen.height)
user.set(CustomUserProperties.SCREEN_RESOLUTION_WIDTH, window.screen.width)
user.set(CustomUserProperties.GIT_COMMIT_HASH, process.env.REACT_APP_GIT_COMMIT_HASH ?? 'unknown')

// Service Worker analytics
const isServiceWorkerInstalled = Boolean(window.navigator.serviceWorker?.controller)
const isServiceWorkerHit = Boolean((window as any).__isDocumentCached)
const serviceWorkerProperty = isServiceWorkerInstalled ? (isServiceWorkerHit ? 'hit' : 'miss') : 'uninstalled'

const pageLoadProperties = { service_worker: serviceWorkerProperty }
sendInitializationEvent(SharedEventName.APP_LOADED, pageLoadProperties)
const sendWebVital =
(metric: string) =>
({ delta }: Metric) =>
sendAnalyticsEvent(SharedEventName.WEB_VITALS, { ...pageLoadProperties, [metric]: delta })
getCLS(sendWebVital('cumulative_layout_shift'))
getFCP(sendWebVital('first_contentful_paint_ms'))
getFID(sendWebVital('first_input_delay_ms'))
getLCP(sendWebVital('largest_contentful_paint_ms'))
}, [])

useEffect(() => {
user.set(CustomUserProperties.DARK_MODE, isDarkMode)
}, [isDarkMode])

useEffect(() => {
// If we're not in the transition period to UniswapX opt-out, set the router preference to whatever is specified.
if (!isUniswapXDefaultEnabled) {
user.set(CustomUserProperties.ROUTER_PREFERENCE, routerPreference)
return
}

// In the transition period, override the stored API preference to UniswapX if the user hasn't opted out.
if (routerPreference === RouterPreference.API && !userOptedOutOfUniswapX) {
user.set(CustomUserProperties.ROUTER_PREFERENCE, RouterPreference.X)
return
}

// Otherwise, the user has opted out or their preference is UniswapX/client, so set the preference to whatever is specified.
user.set(CustomUserProperties.ROUTER_PREFERENCE, routerPreference)
}, [routerPreference, isUniswapXDefaultEnabled, userOptedOutOfUniswapX])
return null
}
Loading