Search code examples
reactjsgoogle-analyticsgoogle-analytics-4

react_ga4__WEBPACK_IMPORTED_MODULE_0__.default.pageview is not a function


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

enter image description here

had npm installed the package. checked the syntax


Solution

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