`react_ga4__WEBPACK_IMPORTED_MODULE_0__.default.pageview` 不是一个函数。

huangapple go评论65阅读模式
英文:

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,
};

`react_ga4__WEBPACK_IMPORTED_MODULE_0__.default.pageview` 不是一个函数。

已经使用 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,
};

`react_ga4__WEBPACK_IMPORTED_MODULE_0__.default.pageview` 不是一个函数。

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" });

huangapple
  • 本文由 发表于 2023年6月19日 11:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定