我想將 Google Analytics 集成到我的 React 應用程序中。我正在使用到達路由器,這使事情變得更加復雜。如果我像這樣使用位置提供程序,則導航 API 將不起作用。因此,如果單擊任何頁面,頁面都不會呈現import { Router,createHistory,LocationProvider }from "@reach/router";import ReactGA from "react-ga";ReactGA.initialize("G-xxxxxxx");const history= createHistory(window);history.listen( window => { ReactGA.pageview(window.location.pathname+ window.location.search); console.log('page=>',window.location.pathname);});<LocationProvider history={history}> <Router></Router></LocationProvider>如果我使用globalHistory谷歌分析僅在主頁上可用,沒有其他頁面在統計中ReactGA.initialize("G-XXXXXXXXXXX");globalHistory.listen(({ location }) => { window.ga('send', 'pageview'); // or use the new gtag API window.gtag('config', 'G-XXXXXXXXXX', {'page_path': location.pathname});});有人知道如何通過第一種或第二種方式解決這個問題嗎?
在 React 應用程序中將 Google Analytics 與到達路由器集成
精慕HU
2023-07-14 09:50:26