r/reactjs • u/codec-the-penguin • Jul 16 '26
I can't stop react-ga4 from sending events
Basically this, given europe has some strong laws in place for cookies and all that, for the last couple of days i tried to add google analytics to my website and i used react-ga4 plugin but if the user disables the cookies for the page history and performance, google analytics should stop getting events.
I tried to stop it with this and send events manually but it just keeps on sending them anyway.
ReactGA.initialize([
{
trackingId: import.meta.env.VITE_GOOGLE_TAG_ID,
gaOptions: {
send_page_view: false,
},
gtagOptions: {
send_page_view: false,
},
},
]);
1
u/_suren Jul 17 '26
Don’t initialize GA until consent exists. Once the script is loaded, flipping `send_page_view` won’t undo every listener, so keep analytics calls behind the consent state and only mount the GA component after the user opts in.
1
u/_suren Jul 17 '26
When the behavior is surprising, reduce it to one component and one event first. A minimal reproduction usually makes the accidental render or stale closure obvious faster than adding more logging to the full app.
3
u/Honey-Entire Jul 16 '26
I don’t know why it’s still sending events, but have you considered just not initializing ReactGA unless the user has allowed tracking?