Cookie Consent Modal
Cookie consent banners are annoying, we know. But they are legally required in many countries, so we have to deal with them.
This guide will help you dynamically add or remove cookies from your app via the Cookie Consent modal that comes with this template.
This is needed for non-essential cookies that are not necessary for the basic functionality of your app, such as analytics cookies or marketing cookies.
The Modal can be found at app/src/client/components/cookie-consent/
and contains two main files:
Banner.tsx
- the component that displays the banner at the bottom of the page.Config.ts
- the configuration file that contains the cookies/scripts that will be dynamically added.
The Banner.tsx
component is imported in app/src/client/App.tsx
and is rendered at the bottom of the page, while all the changes to the banner itself are done within the Config.ts
file, which we explain below.
Configuration
We decided to use the vanilla-cookieconsent
library to handle the cookie consent. We’ve set it up to give you some basic functionality, using mostly the default settings. For a full list of options, you can check the official documentation.
Below, we will guide you through the necessary steps to get the cookie consent modal set up for your app.
Google Analytics
What’s impotant to note for this template is that we are simply using the onAccept
callbacks to dynamically add or remove our Google Analytics cookies from the page. In order for it to work correctly with your app, you need to add your Google Analytics ID to your .env.client
file.
And that’s it! The cookie consent modal will now dynamically add or remove the Google Analytics cookies based on the user’s choice.
To check if it’s working correctly, you can open the browser’s developer tools and check the cookies tab. You should see the following cookies being added or removed based on the user’s choice:
Plausible Analytics
If you decide to go with Plausible Analytics, you DO NOT need to ask users for their consent to use cookies because Plausible, as a privacy-first analytics provider, does not use cookies. Instead, It collects website usage data anonymously and in aggregate form only, without any personally identifiable information
By avoiding cookies, Plausible Analytics avoids the need for cookie consent banners.
Your Terms / Privacy Policy
You should also add a link to your terms and privacy policy within consentModal
section of config.language
:
Allowing Users to Control Certain Cookies (OPTIONAL)
If you’ve added more than just Google Analytics cookies to your app, you can allow users to control which cookies they want to accept or reject. For example, if you’ve added marketing cookies, you can add a button to the modal that allows users to reject them, while accepting analytics cookies.
To do that, you can change the preferencesModal.sections
property in config.language
. Any section that you add to preferencesModal.sections
must match a linkedCategory
in the config.categories
property. Make sure you also add a showPreferencesBtn
property to consentModal
(highlighted below).
Below is an example of what your config might look like if you want to give users the option to control over multiple cookie preferences:
For more information on how to do that, check the official documentation.