22

My website gathers information for Google Analytics, so I need to include a Cookie consent banner for the users to opt in/out of.

I saw the plugin gatsby-plugin-gdpr-cookies and thought it looked perfect. I've followed the startup and have it inside my config file. However I'm not sure what to do next. Do I need to create a banner component and link it all up somehow? I've tried to look around for other examples but can't see any.

Any help appreciated, thanks.

3 Answers 3

48

You have to combine a Gatsby plugin and build your own cookie consent banner or use a ready made component to achieve this.

First as AskaNor_29 suggested you need to install and configure the gatsby-plugin-gdpr-cookies plugin. You can get the plugin here.

Configure the plugin in gatsby-config.js

// In your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-gdpr-cookies`,
      options: {
        googleAnalytics: {
          trackingId: 'YOUR_GOOGLE_ANALYTICS_TRACKING_ID',
          // Setting this parameter is optional
          anonymize: true
        },
        facebookPixel: {
          pixelId: 'YOUR_FACEBOOK_PIXEL_ID'
        },
        // Defines the environments where the tracking should be available  - default is ["production"]
        environments: ['production', 'development']
      },
    },
  ],
}

The second part is showing a cookie consent banner or modal so the user can make his choice.

For this you can use the react-cookie-consent npm module. You can get the npm package here.

To make it work with the gatsby-plugin-gdpr-cookies, you need to set the cookieName="gatsby-gdpr-google-analytics" prop.

Then you put the CookieConsent component in your layout.js file so it's activated on any page the user visits first.

<CookieConsent
          location="bottom"
          buttonText="Accept"
          declineButtonText="Decline"
          cookieName="gatsby-gdpr-google-analytics">
This site uses cookies ...
</CookieConsent>

The component takes many props so you can tweak the behaviour and looks.

If you want both Google Analytics and Facebook Pixel cookies to be set, there are callbacks for accepting/declining cookies where you can add your custom code to set both cookies.

If you're interested I wrote a longer how-to describing the steps.

12
  • 2
    This works! Thanks for this detailed tutorial. I've been searching all over and this is the first time and article that covers for beginner. I wonder if it's possible for user to trigger Cookie Consent by choice? I've seen sites have Cookie Settings and upon clicking, triggering Cookie Consent modal. Tutorial v2 perhaps? :p
    – ajmalafif
    Jan 27, 2020 at 6:30
  • You mean like the sort of modal that some sites are using where you can configure a bunch of cookies at once? Do you have a specific list of tracking services in mind? Jan 27, 2020 at 8:20
  • @BojanBedrač Partly yes. So for example, this site Framer at the very bottom in the footer, there's this Cookie Settings link. Clicking it triggers back the Cookie Consent modal it asked the very first time. Use case I'm imagining is maybe user say Yes by accidental or at first, then decide to Decline. This seems like a great implementation of that.
    – ajmalafif
    Jan 28, 2020 at 9:10
  • Has someone an idea for an optout on the privacy page after accepting the cookies? My quickfix is to place a link with onklick event and delete all cookies with pure js. But it would be better if all of this came from the same component.
    – freesh
    Apr 21, 2020 at 19:37
  • Can you elaborate how to actually stop the tracking? I have set the cookie and set it to false but when I'm in my analytics account it still tracks me and shows my presence. Im not able to tell if it really prevents tracking. Also I see _ga in the cookies which are from my root page. And I have only installed the libraries you described above.
    – TheWeeezel
    Jun 18, 2020 at 7:17
2

Attention, I had an issue with tracking via Google Analytics. After a lot of research I found the solution in the reactGaOptions which is used under the hood by gatsby-plugin-google-analytics-gdpr. Use the sampleRate option to enable 100% tracking so that also mobilephones will send the requests to Google. In normal mode it is set to 1% so in low bandwith you will loose a lot of user information.

reactGaOptions: {
  debug: false,
  gaOptions: {
    sampleRate: 100,
    siteSpeedSampleRate: 100
  }
}
1

From the plugin page

First of all the plugin checks in which environment your site is running. If it’s currently running in one of your defined environments it will add the Facebook Pixel javascript by default to the of your site. It will not be activated or initialized by this.

By default this plugin will not send any data to Google or Facebook to make it GDPR compliant. The user first needs to accept your cookie policy. By accepting that you need to set two cookies - gatsby-gdpr-google-analytics and gatsby-gdpr-facebook-pixel. Depending on the user input the value of each of the cookies should be true or false.

If the gatsby-gdpr-google-analytics cookie is set to true, Google Analytics will be initialized onClientEntry. Same is for the Facebook Pixel.

The page view will then be tracked on onRouteUpdate.

So you should build a banner component and set the cookies to true or false, it depends on the user choice.

1
  • 2
    This is a good answer - it's important to set each of your cookies to have a true value otherwise the plugin won't know what to do (Thats if you're tracking with more than one cookie like analytics and facebook for example). If you're using a package like react-cookie-consent for your banner (as outlined in the popular answer above). Your onAccept method would look something like this: onAccept={() => {Cookies.set("gatsby-gdpr-google-analytics", "true"); Cookies.set("gatsby-gdpr-facebook-pixel", "true"); initializeAndTrack(location); }}
    – pr0tipZ
    Jul 10, 2021 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.