Set up consent mode in Google Analytics
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about Google Tag Manager and GA4 consent, since it shows official code for setting default and updated consent via gtag.
Recommended by 2 people across 2 different months, 2023 to 2024.
What it is
An 8-minute tutorial from the official Google Analytics channel, presented by Matt Landers, on setting up consent mode so a site can pass users' cookie consent status to Google. It walks through when to get consent, building a consent banner, wiring consent into Tag Manager, and checking that user input is received. Demo code is provided on GitHub.
What people said
Recommends Luke · 2023-12 · Stack Overflow · on “How to combine two GDPR consent messages for Adsense and Google Analytics 4 into one message?”
Here's a very useful video [this video] It looks like most CMPs integrate with Google Analytics consent already. So you shouldn't need to hand ball the code yourself...
Recommends ajbeaven · 2024-03 · Stack Overflow · on “Can't add default consent to Google Tag Manager”
If you're implementing consent manually (ie. without the help of a Consent Management Platform and their associated tags in GTM), you do need to call gtag directly - how else is GTM going to know what consent has been given? Here is a video from the official Google Analytics channel showing code that does just this: [this video]
Cites Peter F · 2023-09 · Stack Overflow · on “Setting Google Tag Manager consent with react-gtm-module”
My advice, don't use react-gtm-module if you want to enable the google tag consent by default for your SPA react app. I don't think its possible at the moment. I've been trying for the last 3 hours, including debugging with the tag assisant (go to google tag manager and click the Preview button, you can test your setup on localhost). But the problem is that the react-gtm-module does not add the default consent via the API and also has no way of updating the consent values. Its not that hard to setup google tag manually and it will allow you to use their latest funcionalities without depending on a package with very little documentation In step 5 you see that you need an extra script to run e