nandorojo / expo-next-react-navigation

⛴ Make Next.js and react-navigation play nicely together with an Expo/React Native Web app.
404 stars 32 forks source link

How/Where to add analytics when using this library? #97

Closed IHIutch closed 2 years ago

IHIutch commented 2 years ago

This is just a question, not a bug.. But I'm wondering where an analytics code would be added when using this library?

Does it work just like a normal NextJS app?

import { useEffect } from 'react'
import { useRouter } from 'next/router'

function MyApp({ Component, pageProps }) {
  const router = useRouter()

  useEffect(() => {
    const handleRouteChange = url => {
      window.gtag('config', process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS, {
        page_path: url,
      })
    }
    router.events.on('routeChangeComplete', handleRouteChange)
    return () => {
      router.events.off('routeChangeComplete', handleRouteChange)
    }
  }, [router.events])

  return <Component {...pageProps} />
}

export default MyApp

Or is there more to consider for native environments too?

nandorojo commented 2 years ago

for ios/android, you’ll probably want a listener for state change in NavigationContainer https://reactnavigation.org/docs/screen-tracking/

nandorojo commented 2 years ago

for ios/android, you’ll probably want a listener for state change in NavigationContainer https://reactnavigation.org/docs/screen-tracking/

IHIutch commented 2 years ago

Good to know, thanks