react-ga4 icon indicating copy to clipboard operation
react-ga4 copied to clipboard

How to disable the automatic pageview measurement ( send_page_view: false) in multi-page apps.

Open EmaAlina opened this issue 2 years ago • 4 comments

Hello, On multi page apps we want to have only the custom/manually page_view events and to switch off the automatic page_view measurement. The ReactGA is initialized when the root component of the app is rendered and the custom/manually page_view events are sent on each route change.

According to the following sample we must reset and reinitialize the config on each route change which is time consuming. https://github.com/codler/react-ga4/issues/12#issuecomment-1444458685

We've added the following source code(not ideal) on each route change but the automatic page_view measurement is still triggered.

ReactGA.reset(); // reset the `isInitialized` flag
ReactGA.initialize("your GA measurement id", {
  gtagOptions: {
    send_page_view: false
  }
});
ReactGA.send({ hitType: 'pageview', page: window.location.pathname });

Is there a way to set the 'send_page_view` flag to false, from source code, without reinitializing the RectGA ?

fyi: Browser-history events is disabled in GA4. image

EmaAlina avatar Apr 07 '23 13:04 EmaAlina

Had the same issue, and I was also setting send_page_view: false under the gtagOptions object.

Fixed it by disabling enhanced measurements in the GA admin dashboard, like suggested in https://developers.google.com/analytics/devguides/collection/ga4/views?client_type=gtag#disable_pageview_measurement

ciampo avatar Apr 24 '23 00:04 ciampo

My understanding of the problem is that in GA4 page view events happen automatically so defining a ReactGA.pageview in the code would make it count twice.

Is there a reason yall are opting to disable enhanced measurements/send send_page_view: false as opposed to just removing the ReactGA.pageview calls?

I also see it mentioned in the README here: https://github.com/codler/react-ga4#migrate-from-old-react-ga

I am trying to learn if there is something I have not accounted for in my migration. TYIA🚀

cc @ciampo @EmaAlina

trevv16 avatar May 11 '23 19:05 trevv16

The example in the migration guide does mention "Send pageview with a custom path"

Maybe that should be interpreted as: "page view events are sent automatically without the need for an explicit call in the code. Use the send function only to send custom page views, ie. with custom paths, or from updates that didn't result in a page history alteration"

ciampo avatar May 16 '23 10:05 ciampo

I wrote a new issue here. https://github.com/codler/react-ga4/issues/72

sin-to-jin avatar Apr 09 '24 08:04 sin-to-jin