Giter Site home page Giter Site logo

michal-nahlik / gdpr-cookie-consent-minimal Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 83 KB

Simple example of GDPR opt-in cookie consent using HTML, CSS, Javascript and jQuery with Google Tag Manager integration

HTML 56.36% JavaScript 30.33% CSS 13.31%
gdpr-cookie google-tag-manager cookie-consent gtm-consent

gdpr-cookie-consent-minimal's Introduction

Simple GDPR opt-in cookie consent using HTML, CSS, Javascript and jQuery

Example of simple Cookie Consent dialog with page overlay that allows user to set cookie preferences.

The user choices are saved to cookie with 1 year expiration. Link or button can by used to reopen the dialog and change user preferences by setting the element class "showCookieDialog"

Javascript for Cookie Consent dialog: src/cookieConsent.js
HTML for Cookie Consent dialog: src/cookieConsent.html
CSS for Cookie Consent dialog: src/style.html
One page example: src/index.html

Google Tag Manager consent settings

Example contains Google Tag Manager definition with default consent denied that gets updated based on user settings.

 // Default consent settings
gtag('consent', 'default', {'functionality_storage': 'granted',
                            'security_storage': 'granted',
                            'ad_storage': 'denied', 
                            'analytics_storage': 'denied',
                            'personalization_storage': 'denied',
                            'wait_for_update': 500}); // so you can update consent later and it gets correctly triggered in GTM

The default settings waits 500 milliseconds before being pushed to data layer so it can be updated based on user settings.

if (currentPreferences["marketing"] == 1) {
    gtag('consent', 'update', {
                    'ad_storage': 'granted', 
                    'analytics_storage': 'granted',
                    'personalization_storage': 'granted'});
} else if (currentPreferences["marketing"] == 0) {
    gtag('consent', 'update', {
                    'ad_storage': 'denied', 
                    'analytics_storage': 'denied',
                    'personalization_storage': 'denied'});
}

This triggers tags correctly if Cookie Consent preferences are already saved but does not trigger tags when user saves preferences using consent dialog for the first time (because it takes more than 500 milliseconds). To trigger tags after saving preferences using Cookie Consent dialog a custom event is pushed to data layer that can be used to trigger them without reloading the page.

triggerGTMEvent('consentFormSubmit');
// push GTM event to data layer
function triggerGTMEvent(eventName) {
    window.dataLayer.push({
        'event' : eventName
    });
}

GTM tag and triggers configuration example

Tag with specified additional consent requirement (some types of tags have this requirement set up by default)

"Page View - DOM Ready" event has to be used as default trigger ("Page View" trigger will not fire the tag because at the time "Page View" is triggered the consent is set to denied)

Trigger with custom event is used to trigger tag after Cookie Consent form is saved

Both triggers are used for event so it is correctly fired after page load (in case user already saved cookie preferences) or after setting preferences using Cookie Consent dialog.

Sources

Sources for Google Tag Manager settings:

Source for nice switch slider:

gdpr-cookie-consent-minimal's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.