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

Is it possible to set "send_page_view" to "false" with this "react-ga4" ?

Open sin-to-jin opened this issue 1 year ago • 10 comments

What

This time, I wrote the following pattern code to avoid sending "page_view", but all "page_view" were sent to GA4, so I made this issue. Is there any solution? If anyone knows of any solutions or workarounds, it would be appreciated.

ReactGA.initialize([
  {
    trackingId: "xxxxxxx",
    gaOptions: { send_page_view: false },
  }
])

Or,

ReactGA.ga('config', "xxxxxxx", { send_page_view: false })

Or,

ReactGA.initialize([
  {
    trackingId: "xxxxxxx",
  }
])
ReactGA.send({ send_page_view: false })

Refference

  • https://github.com/codler/react-ga4/issues/27
  • https://developers.google.com/analytics/devguides/collection/ga4/views?client_type=gtag#disable_pageview_measurement

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

From my testing this appears to be working, I manually trigger the first page view in my app

ReactGA.initialize([{
    trackingId: TRACKING_ID,
    gaOptions: {
        send_page_view: false,
    }
}]);

EPGDigital-MW avatar May 24 '24 10:05 EPGDigital-MW

Apologies, after more testing that wasn't working!

This appears to be working:

ReactGA.initialize(TRACKING_ID, {
    gaOptions: {
        send_page_view: false,
    }
});

This results in:

{
	0: "config",
	1: "TRACKING_ID",
	2: {
		send_page_view: false
	}
}

EPGDigital-MW avatar May 28 '24 14:05 EPGDigital-MW

@EPGDigital-MW Thanks for the advice, I'll give it a shot! I'll post more on this ish if I find anything else.

sin-to-jin avatar May 30 '24 02:05 sin-to-jin

I tried this approach, but it seems that page_view is still being sent in my environment...

sin-to-jin avatar May 30 '24 02:05 sin-to-jin

~~+1 I am facing the same issue as above. page_view events are sent even when send_page_view is set to false in the initialize call~~

Edit: This example works for me

ReactGA.initialize(TRACKING_ID, { gaOptions: { send_page_view: false, } });

In my case turning off "Page changes based on browser history events" in the GA4 admin and setting the send_page_view stopped all the page_view events from being collected automatically.

soapraj avatar Jun 05 '24 13:06 soapraj

Thanks for the advice!

ReactGA.initialize(TRACKING_ID, {
    gaOptions: {
        send_page_view: false,
    }
});

It still doesn't seem to work...

sin-to-jin avatar Jun 06 '24 00:06 sin-to-jin

In my case turning off "Page changes based on browser history events" in the GA4 admin and setting the send_page_view stopped all the page_view events from being collected automatically.

I've disabled this, then also disabled enhanced measurement as I don't need any other events, everything is triggered within the app manually.

EPGDigital-MW avatar Jun 06 '24 07:06 EPGDigital-MW

I see, so you are saying that the GA side of the configuration needs to be changed. I'll have to check it out.

sin-to-jin avatar Jun 06 '24 07:06 sin-to-jin

Any updates?

mleister97 avatar Oct 16 '24 15:10 mleister97

The gaOptions is for the old analytics, as in the readme the reference link leads to nowhere. You could use the gtagOptions now. My example:

  ReactGA.gtag("consent", "default", {
    ad_storage: "denied",
    ad_user_data: "denied",
    ad_personalization: "denied",
    analytics_storage: "denied",
  });
  // https://developers.google.com/tag-platform/security/guides/consent?consentmode=advanced#update-consent
  if (hasUserConsent()) {
    allConsentGranted();
  }
  const config = [{ trackingId: ga4Id }];

  if (gAdsId) {
    config.push({ trackingId: gAdsId });
  }

  ReactGA.initialize(config, {
    gtagOptions: {
      user_id: userId, // Set user ID globally if available, or undefined
      send_page_view: false, // Without sending default pageviews
    },
  });

  ReactGA.set({
    send_to: ga4Id, // Only send most events to GA4 unless locally specified otherwise
  });


  // Default pageview for every page
  ReactGA.send({
    hitType: "pageview",
    page: window.location.pathname,
    title: document.title,
  });

Firsh avatar Mar 11 '25 00:03 Firsh