elgorditosalsero / react-gtm-hook

Easily manage the Google Tag Manager via Hook
https://elgorditosalsero-react-gtm-hook.netlify.app/
MIT License
220 stars 28 forks source link

Recommended approach for sending Consent mode data #84

Open michalpulpan opened 8 months ago

michalpulpan commented 8 months ago

Hello! Thank you for this amazing library. I'd like to ask you whether there's any recommended approach how to send consent events using react-gtm-hook

  gtag('consent', 'update', {
    'ad_storage': 'granted',
    'analytics_storage': 'granted',
    'ad_user_data': 'granted',
    'ad_personalization': 'granted'
  });

thank you in advance ✌️

NessDan commented 1 week ago

+1 to this, I was just about to ask the exact same question and saw someone beat me to the punch 🙏

As of right now I'm creating a hook around useGTMDispatch:

const useConsentAwareGTM = () => {
  const analyticsEnabled = useContext(AnalyticsContext)
  const sendDataToGTM = useGTMDispatch()

  return (data: any) => {
    if (analyticsEnabled) {
      sendDataToGTM(data)
    }
  }
}

But I'd prefer to not have to re-write all of our files to support this