vanilla-cookieconsent
repository·master·Indexed 26 days ago
https://github.com/orestbida/cookieconsentA lightweight, GDPR-compliant cookie consent management plugin written in vanilla JavaScript (v3.1.0). It provides a client-side solution for managing user consent with support for custom callbacks, no-code button actions via data-cc attributes, and integration with Google Consent Mode and iframemanager.
What's inside vanilla-cookieconsent
- CookieConsent v3 is a lightweight and GDPR-compliant cookie consent plugin written in plain JavaScript. It is designed to help developers manage user consent for cookies in a compliant manner.
Introduction to CookieConsent
masterCookieConsent is a free, open-source vanilla JavaScript plugin designed to manage scripts and cookies in compliance with GDPR. It is framework-agnostic and can be integrated into any web platform or framework.Introduction to CookieConsent v3
masterCookieConsent v3 is a lightweight, GDPR and CCPA compliant Consent Management Tool written in vanilla JavaScript. It is designed to have a minimal impact on website performance while providing accessibility (a11y) support for keyboard and screen reader users. It ensures compliance by blocking scripts until explicit consent is granted and providing clear opt-out options.Evaluate if CookieConsent is suitable for your project
masterBefore integrating CookieConsent, consider the following limitations and requirements:
- Requirements: Requires basic knowledge of JavaScript.
- Configuration: Does not provide default categories or translations; these must be configured manually.
- Not a CMP: CookieConsent is not a Consent Management Platform (CMP).
- No Consent Records: It does not store Consent Records.
- No IAB TCF: It does not implement the IAB Framework (Transparency and Consent Framework).
- Google Ads Warning: Google AdSense, Ad Manager, and AdMob will no longer work with CookieConsent as of January 2024.
Open the Preferences Modal via HTML
masterTo allow users to reopen their cookie preferences at any time, add a button to your HTML using the
data-ccattribute with the valueshow-preferencesModal.<button type="button" data-cc="show-preferencesModal">Manage cookie preferences</button>Access CookieConsent v3 Documentation
masterFull technical documentation, including API references and configuration details, is available at https://cookieconsent.orestbida.com.Connect IframeManager to CookieConsent
masterTo ensure that user interactions with iframes (like clicking an 'Accept' button inside an iframe) are reflected in the CookieConsent state, use the
onChangecallback iniframemanager.run().When an event occurs, you must retrieve the current accepted services for the relevant category, merge them with the newly changed services, and call
CookieConsent.acceptService().Note: The
onChangecallback requiresiframemanagerversion 1.2.0 or higher.const im = iframemanager(); im.run({ onChange: ({ changedServices, eventSource }) => { if(eventSource.type === 'click') { const servicesToAccept = [ ...CookieConsent.getUserPreferences().acceptedServices['analytics'], ...changedServices ]; CookieConsent.acceptService(servicesToAccept, 'analytics'); } }, services: { youtube: { // ... }, vimeo: { // ... } } });Implement Google Consent Mode support
masterTo support Google Tag Manager's consent mode, you must synchronize CookieConsent's state with Google's
gtagfunction.- Initialize
dataLayerandgtag: Define thedataLayerarray and thegtagfunction globally. - Set Default Consent: Call
gtag('consent', 'default', { ... })with all relevant services set to'denied'before any other dataLayer changes occur. - Map Services to Categories: Use
CookieConsent.acceptedService(serviceName, categoryName)to check if a user has granted consent for a specific service within a category. This allows you to map CookieConsent categories (e.g.,analytics,advertisement) to specific Google Consent Mode parameters (e.g.,analytics_storage,ad_storage). - Update on Consent Changes: Use the
onFirstConsent,onConsent, andonChangehooks in theCookieConsent.run()configuration to trigger agtag('consent', 'update', { ... })call whenever the user's preferences change.
- Initialize
Block scripts and cookies for GDPR compliance
masterSimply displaying a consent banner is insufficient for GDPR compliance. You must also implement logic to block scripts and cookies from executing until the user has provided explicit consent. Refer to the scripts management documentation for implementation details.Enable dark mode theme
masterCookieConsent includes both a
light(default) and adarktheme. To enable the dark theme, add thecc--darkmodeclass to the<html>element of your document.document.documentElement.classList.add('cc--darkmode');Access the CookieConsent v3 Playground
masterYou can interact with the CookieConsent v3 Playground to test configurations and see live demonstrations of the library in action. The playground is available at the following URL:
https://playground.cookieconsent.orestbida.comChoose between CookieConsent v2 and v3
masterDecide which version to use based on your browser support and feature requirements:
Use v2 if:
- You need to support older browsers such as IE10.
- Note: v2 is in maintenance mode and only receives bugfix updates.
Use v3 if:
- You want a simpler and clearer API and configuration.
- You need a wider variety of layouts and button arrangements.
- You require more flexible script management options.
- You need to support individually togglable Services.
- You want to use UMD or ESM variants.
- You are targeting modern browsers only.