Giter Site home page Giter Site logo

loic-fremaux / cookie-consent-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shaack/cookie-consent-js

0.0 0.0 0.0 314 KB

A simple dialog and framework to handle the German and EU law about cookies in a website (February 2021)

License: MIT License

HTML 11.15% JavaScript 69.61% SCSS 19.24%

cookie-consent-js's Introduction

cookie-consent-js

A simple dialog and framework to handle the German and EU law (as written by EuGH, 1.10.2019 – C-673/17) about cookies in a website.

cookie-consent-js has no external dependencies, and it works also with Bootstrap.

Note We made another version with more detailed cookie settings, which needs Bootstrap 4. You find it at https://github.com/shaack/bootstrap-cookie-consent-settings

Usage

Follow these easy steps to integrate the cookie settings in your page.

1. Install cookie-consent-js in your project with npm install cookie-consent-js

Alternatively you can download the git repository.

2. Include cookie-consent.css

<link rel="stylesheet" href="/node_modules/cookie-consent-js/src/cookie-consent.css"/>

This should be done before any bootstrap or other frameworks css. You can overwrite styling in your projects css, take a look at cookie-consent.scss.

3. Include cookie-consent.js

<script src="/node_modules/cookie-consent-js/src/cookie-consent.js"></script>

In your <head> or at the bottom of your <body>.

4. Initialize the Script

var cookieConsent = new CookieConsent({privacyPolicyUrl: "/privacy-policy.html"})

In props you should at least define privacyPolicyUrl. See below "Configuration properties".

5. Enable "Cookie settings" in your service navigation

<a href="javascript:cookieConsent.reset()">Cookie settings</a>

So the user can anytime reconfigure, if he wants tracking or not.

6. Enable or disable tracking depending on configuration

Client side JavaScript: Surround your tracking code with

if (cookieConsent.trackingAllowed()) {
    // Google Analytics code and/or other tracking code
}

Server side PHP: Surround your tracking code with

if($_COOKIE['cookie-consent-tracking-allowed'] === 'true') {
    // do some tracking
}

All other languages: Just read, if the cookie cookie-consent-tracking-allowed is "true"

...that's all! Contact me, if you have questions.

API

This framework writes a cookie (it's default name is cookie-consent-tracking-allowed) with the value "true", if the user has accepted tracking. You can read the value with the JavaScript API (cookieConsent.trackingAllowed()) or from any other language, server or client side, which can read cookies.

JavaScript API

Show the settings dialog again

cookieConsent.reset()

Use this to allow the user to reconfigure the cookie settings, for example, in your service navigation as "Cookie settings".

Read the status

cookieConsent.trackingAllowed()

Returns true if the user did accept tracking cookies. Use this function to disable tracking. Surround tracking code, like the Google Analytics code with

if (cookieConsent.trackingAllowed()) {
    // Google Analytics code and/or other tracking code
}

Read the status from PHP

Read the cookie from a PHP server with

if($_COOKIE['cookie-consent-tracking-allowed'] === 'true') {
    // do some tracking
}

Configuration properties

With default values.

this.props = {
    buttonPrimaryClass: "btn btn-primary", // the "accept all" buttons class, only used for styling
    buttonSecondaryClass: "btn btn-secondary", // the "accept necessary" buttons class, only used for styling
    privacyPolicyUrl: "privacy-policy.html",
    autoShowModal: true, // disable autoShowModal on the privacy policy page, to make that page readable
    lang: navigator.language, // the language, in which the modal is shown
    blockAccess: false, // set "true" to block the access to the website before choosing a cookie configuration
    position: "right", // position ("left" or "right"), if blockAccess is false
    postSelectionCallback: undefined, // callback, after the user has made his selection
    content: { // the content in all needed languages
        de: {
            title: "Cookie-Einstellungen",
            body: "Wir nutzen Cookies, um Inhalte zu personalisieren und die Zugriffe auf unsere Website zu analysieren. " +
                "Sie können wählen, ob Sie nur für die Funktion der Website notwendige Cookies akzeptieren oder auch " +
                "Tracking-Cookies zulassen möchten. Weitere Informationen finden Sie in unserer --privacy-policy--.",
            privacyPolicy: "Datenschutzerklärung",
            buttonAcceptAll: "Alle Cookies akzeptieren",
            buttonAcceptTechnical: "Nur technisch notwendige Cookies akzeptieren"
        },
        en: {
            title: "Cookie settings",
            body: "We use cookies to personalize content and analyze access to our website. " +
                "You can choose whether you only accept cookies that are necessary for the functioning of the website " +
                "or whether you also want to allow tracking cookies. For more information, please refer to our --privacy-policy--.",
            privacyPolicy: "privacy policy",
            buttonAcceptAll: "Accept all cookies",
            buttonAcceptTechnical: "Only accept technically necessary cookies"
        }
    },
    cookieName: "cookie-consent-tracking-allowed",  // the name of the cookie, the cookie is `true` if tracking was accepted
    modalId: "cookieConsentModal" // the id of the modal dialog element
}

Disable autoShow

You can disable autoShowModal, for instance, in the privacy policy and legal notice pages to make these pages ( better) readable.

var cookieConsent = new CookieConsent({linkPrivacyPolicy: "privacy-policy.html", autoShowModal: false})

Styling

See ./src/cookie-consent.scss and overwrite values as you need in your projects stylesheet.

Disclaimer

cookie-consent-js is a project of shaack.com. If you use anything from this project, do so under the MIT-License.

cookie-consent-js's People

Contributors

shaack avatar ensag-dev 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.