vanilla-cookieconsent

repository·master·Indexed 26 days ago

https://github.com/orestbida/cookieconsent

A 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.

Tokens
21K
Snippets
66
Records
99
Agent score
91%

What's inside vanilla-cookieconsent

  1. Introduction to CookieConsent v3

    master
    CookieConsent 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.
  2. Evaluate if CookieConsent is suitable for your project

    master

    Before 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.
  3. Connect IframeManager to CookieConsent

    master

    To ensure that user interactions with iframes (like clicking an 'Accept' button inside an iframe) are reflected in the CookieConsent state, use the onChange callback in iframemanager.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 onChange callback requires iframemanager version 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: {
                // ...
            }
        }
    });
  4. Implement Google Consent Mode support

    master

    To support Google Tag Manager's consent mode, you must synchronize CookieConsent's state with Google's gtag function.

    1. Initialize dataLayer and gtag: Define the dataLayer array and the gtag function globally.
    2. Set Default Consent: Call gtag('consent', 'default', { ... }) with all relevant services set to 'denied' before any other dataLayer changes occur.
    3. 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).
    4. Update on Consent Changes: Use the onFirstConsent, onConsent, and onChange hooks in the CookieConsent.run() configuration to trigger a gtag('consent', 'update', { ... }) call whenever the user's preferences change.
  5. Choose between CookieConsent v2 and v3

    master

    Decide 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.