Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you use the Consent API? #447

Open
limegorilla opened this issue Apr 24, 2023 · 2 comments
Open

How do you use the Consent API? #447

limegorilla opened this issue Apr 24, 2023 · 2 comments

Comments

@limegorilla
Copy link

Hi there!

I can see as per #367 you have enabled a consent API - there are no docs on your implementation of this.

Is this a function I can call from other code or am I supposed to use cookies? And if it is the latter - I assume that the basic way of implementing would be:

  • If GA cookies do not exist, deny by default (as per GDPR)
  • If user consents (or cookies are already set to allow) set GA cookies to allow
  • nextjs-google-analytics then automatically picks that up and starts sending data back to Google.

I don't mind writing the docs if you can let me know how this works!

@LeunensMichiel
Copy link
Contributor

LeunensMichiel commented Jun 1, 2023

I did it like this:

type CONSENT = 'granted' | 'denied' | 'pending';
const [cookieConsent, setCookieConsent] = useState<CONSENT>('pending');

  useEffect(() => {
    const storedCookieConsent: CONSENT = getLocalStorage(
      'cookie_consent',
      'pending'
    );

    setCookieConsent(storedCookieConsent);
  }, [setCookieConsent]);

  useEffect(() => {
    if (cookieConsent === 'pending') return;

    consent({
      arg: 'update',
      params: {
        ad_storage: cookieConsent,
        analytics_storage: cookieConsent,
      },
    });

    setLocalStorage('cookie_consent', cookieConsent);
  }, [cookieConsent]);

  if (cookieConsent !== 'pending') {
    return null;
  }

 return (<>My Cookie Banner</>)

@LeunensMichiel
Copy link
Contributor

Made a PR #479

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants