英文:
react_ga4__WEBPACK_IMPORTED_MODULE_0__.default.pageview is not a function
问题
尝试将 Google Analytics 更新为 GA4 以供我的 React 项目使用,我正在使用此包,正如您从标题中所见,它给我带来了一些错误。
//import ReactGA from 'react-ga';
import ReactGA from "react-ga4";
function init() {
  // 在本地开发环境启用调试模式
  const isDev = false; // !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
 // ReactGA.initialize(process.env.GOOGLE_ANALYTICS_TRACKING_ID, { debug: isDev });
  ReactGA.initialize(process.env.GOOGLE_ANALYTICS_TRACKING_ID, { debug: isDev });
}
function sendEvent(payload) {
  ReactGA.event(payload);
}
function sendPageview(path) {
  ReactGA.set({ page: path });
  ReactGA.pageview(path);
}
export default {
  init,
  sendEvent,
  sendPageview,
};
已经使用 npm 安装了该包。
检查了语法。
英文:
was tring to update GA to GA4 for my React project, I am using this package, which gives me some errors as you can see from the title.
//import ReactGA from 'react-ga';
import ReactGA from "react-ga4";
function init() {
  // Enable debug mode on the local development environment
  const isDev = false; // !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
 // ReactGA.initialize(process.env.GOOGLE_ANALYTICS_TRACKING_ID, { debug: isDev });
  ReactGA.initialize(process.env.GOOGLE_ANALYTICS_TRACKING_ID, { debug: isDev });
}
function sendEvent(payload) {
  ReactGA.event(payload);
}
function sendPageview(path) {
  ReactGA.set({ page: path });
  ReactGA.pageview(path);
}
export default {
  init,
  sendEvent,
  sendPageview,
};
had npm installed the package.
checked the syntax
答案1
得分: 2
根据 react-ga4,你需要删除 ReactGA.pageview(),因为它已被弃用。应该使用 .send("pageview") 代替。
要发送自定义路径的 pageview:
ReactGA.send({ hitType: "pageview", page: "/my-path" });
英文:
According to react-ga4 you have to   remove ReactGA.pageview() , because it's  Deprecated You should  Use .send("pageview") instead .
To Send pageview with a custom path :
ReactGA.send({ hitType: "pageview", page: "/my-path" });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论