andrezimpel / gatsby-plugin-gdpr-cookies

Gatsby plugin to add Google Analytics (V4 is supported), Google Tag Manager, Facebook Pixel, TikTok Pixel and Hotjar in a GDPR form to your site.
Apache License 2.0
100 stars 68 forks source link

When the user hits declined the tracking continues #66

Open deodat opened 2 years ago

deodat commented 2 years ago

Hello, I'm facing an issue when trying to implement the Decline function: the tracking continues whenever the user hits the "Decline" button (and it begins before the user accepts or declines). Here's my Gatsby configuration:

    {
      resolve: `gatsby-plugin-gdpr-cookies`,
      options: {
        googleTagManager: {
          trackingId: 'UA-XXXXXX',
          cookieName: 'gatsby-gdpr-google-tagmanager',
          defaultDataLayer: { platform: 'gatsby' },
        },
        facebookPixel: {
          pixelId: 'XXXXXXXXXXXX',
          cookieName: 'gatsby-gdpr-facebook-pixel',
        },
        environments: ['production'],
      },
    },

and in my Layout.js component:

import CookieConsent, { Cookies } from 'react-cookie-consent';
import { useLocation } from '@gatsbyjs/reach-router'; // this helps tracking the location
import { initializeAndTrack } from 'gatsby-plugin-gdpr-cookies';

export default function Layout({ children }) {
  const location = useLocation();
  const slug = location.pathname.replace(/[\/\\]/g, '');

  return (
    <div>
      <LayoutStyles>
        <Header />
        <div className="content">
          {children}
        </div>
        <Footer footerItems={footerItems} />
        <CookieConsent
          location="bottom"
          buttonText="J'accepte"
          declineButtonText="Je refuse"
          cookieName="gatsby-gdpr-google-tagmanager"
          expires={150}
          onAccept={() => {
            Cookies.set('gatsby-gdpr-google-tagmanager', true);
            Cookies.set('gatsby-gdpr-facebook-pixel', true);
            initializeAndTrack(location);
          }}
          enableDeclineButton
          onDecline={() => {
            Cookies.set('gatsby-gdpr-google-tagmanager', false);
            Cookies.set('gatsby-gdpr-facebook-pixel', false);
          }}
        >
          En continuant à utiliser le site, vous acceptez l’utilisation de
          cookies.
        </CookieConsent>
      </LayoutStyles>
    </div>
  );
}

I don't know what I'm doing wrong here.

Thanks for any help on this!

andrezimpel commented 2 years ago

Hm, could you give me access to a repo to reproduce this?

deodat commented 2 years ago

Hello @andrezimpel, sorry for the delay, I was on holydays :) My repo is on Gitlab, do you have a profile there or an email address so I can give you access to it? Thanks a lot!

andrezimpel commented 2 years ago

@deodat yea just use my GitHub email :-)

madmonkeyworks commented 2 years ago

Hi, same issue here. I'm setting the gatsby-gdpr-google-tagmanager and gatsby-gdpr-google-analytics to false on user decline, but still see the ga cookies present, even on route change or reload.

andrezimpel commented 2 years ago

@madmonkeyworks any chance we can schedule a screenshare and see where the problem might come from?

ansmlc commented 1 year ago

Same issue here.

@madmonkeyworks Have you found a solution perhaps?

For me it happens in production only. (gatsby build / serve) Tracking is initialized and cookies are set regardless of accept/decline action.

However, in development (gatsby develop) it works as expected.