[英]Next.js Google Analytics with ReactGA
Hi there i'm using ReactGA4 for track my Analytics in Next app.您好,我正在使用 ReactGA4 在 Next 应用程序中跟踪我的分析。 I am facing the issue that i cant see the path where the user is until they reload the page.我面临的问题是在用户重新加载页面之前我看不到用户所在的路径。 This is my code.这是我的代码。
ReactGA.initialize("UA-xxxxxxx");
ReactGA.send("pageview");
Subscribe to the Router
events
and use the GA pageview
interface.订阅Router
events
并使用 GA pageview
界面。
Remember to use the routeChangeComplete
which will send the pageview via the ReactGA method请记住使用routeChangeComplete
它将通过 ReactGA 方法发送页面浏览量
Modified nextjs example修改nextjs示例
import { useEffect } from 'react'
import { useRouter } from 'next/router'
export default function MyApp({ Component, pageProps }) {
const router = useRouter()
useEffect(() => {
const handleRouteChange = (url, { shallow }) => {
// REACTGA
// Send pageview with a custom path
ReactGA.send({ hitType: "pageview", page: "/my-path" });
console.log(
`App is changing to ${url} ${
shallow ? 'with' : 'without'
} shallow routing`
)
}
router.events.on('routeChangeComplete', handleRouteChange)
// If the component is unmounted, unsubscribe
// from the event with the `off` method:
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])
return <Component {...pageProps} />
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.