Giter Club home page Giter Club logo

yii2-cookie-consent's Introduction

yii2-cookie-consent

solution to the EU Cookie Law

Getting started

Installation

composer require dmstr/yii2-cookie-consent

CookieConsentHelper Component

yii config

'components' => [
    'cookieConsentHelper' => [
        'class' => dmstr\cookieconsent\components\CookieConsentHelper::class
    ]
]

Usage with PHP

use dmstr\cookieconsent\widgets\CookieConsent;
<?= CookieConsent::widget([
    'name' => 'cookie_consent_status',
    'path' => '/',
    'domain' => '',
    'expiryDays' => 365,
    'message' => Yii::t('cookie-consent', 'We use cookies to ensure the proper functioning of our website. For an improved visit experience we use analysis products. These are used when you agree with "Statistics".'),
    'save' => Yii::t('cookie-consent', 'Save'),
    'acceptAll' => Yii::t('cookie-consent', 'Accept all'),
    'controlsOpen' => Yii::t('cookie-consent', 'Change'),
    'detailsOpen' => Yii::t('cookie-consent', 'Cookie Details'),
    'learnMore' => Yii::t('cookie-consent', 'Privacy statement'),
    'visibleControls' => true,
    'visibleDetails' => false,
    'link' => '#',
    'consent' => [
        'necessary' => [
            'label' => Yii::t('cookie-consent', 'Necessary'),
            'checked' => true,
            'disabled' => true
        ],
        'statistics' => [
            'label' => Yii::t('cookie-consent', 'Statistics'),
            'cookies' => [
                ['name' => '_ga'],
                ['name' => '_gat', 'domain' => '', 'path' => '/'],
                ['name' => '_gid', 'domain' => '', 'path' => '/']
            ],
            'details' => [
                [
                    'title' => Yii::t('cookie-consent', 'Google Analytics'),
                    'description' => Yii::t('cookie-consent', 'Create statistics data')

                ],
                [
                    'title' => Yii::t('cookie-consent', 'Goal'),
                    'description' => Yii::t('cookie-consent', '_ga, _gat, _gid, _gali')

                ]
            ]
        ]
    ]
]) ?>
<?php if (\Yii::$app->cookieConsentHelper->hasConsent('statistics')): ?>
    <!-- Google Analytics Script-->
<?php endif; ?>

Usage with TWIG

{{ use('dmstr/cookieconsent/widgets/CookieConsent') }}
{{ CookieConsent_widget({
    "name": "cookie_consent_status",
    "path": "/",
    "domain": "",
    "expiryDays": 365,
    "message": t("cookie-consent", "We use cookies to ensure the proper functioning of our website. For an improved visit experience we use analysis products. These are used when you agree with 'Statistics'."),
    "save": t("cookie-consent", "Save"),
    "acceptAll": t("cookie-consent", "Accept all"),
    "controlsOpen": t("cookie-consent", "Change"),
    "detailsOpen": t("cookie-consent", "Cookie Details"),
    "learnMore": t("cookie-consent", "Privacy statement"),
    "visibleControls": false,
    "visibleDetails": false,
    "link": "#",
    "consent": {
        "necessary": {
            "label": t("cookie-consent", "Necessary"),
            "checked": true,
            "disabled": true
        },
        "statistics": {
            "label": t("cookie-consent", "Statistics"),
            "cookies": [
                {"name": "_ga", "domain": "", "path": "/"},
                {"name": "_gat", "domain": "", "path": "/"},
                {"name": "_gid", "domain": "", "path": "/"},
                {"name": "_gali", "domain": "", "path": "/"}
            ],
            "details": [
                {
                    "title": t("cookie-consent", "Google Analytics"),
                    "description": t("cookie-consent", "Create statistics data")
                },
                {
                    "title": t("cookie-consent", "Goal"),
                    "description": t("cookie-consent", "_ga, _gat, _gid, _gali")
                
                }
            ]
        }
    }
}) }}
{% if app.cookieConsentHelper.hasConsent('statistics') %}
    {# Google Analytics Code #}
{% endif %}

Options

option description default type
name Defines the cookie name that Cookie Consent will use to store the status of the consent. Default is cookie conset helper's cookie name "" STRING
path Defines the cookie path "/" STRING
domain Defines the cookie domain "" STRING
expiryDays Defines the cookie exipration days 365 INT
message The message in the popup "This website uses cookies to ensure you get the best experience on our website." STRING
save The save button text "Save" STRING
acceptAll The accept all button text "Accept all" STRING
controlsOpen The open controls button text "Change" STRING
detailsOpen The open details button text "Details" STRING
learnMore The link text "More info" STRING
visibleControls If the controls panel should start open false BOOLEAN
visibleDetails If the details panel should start open false BOOLEAN
link The link pointing to your privacy policy page "#" STRING
consent A configuration array that will tell the cookie consent what it should do. Keys are the consent values that will be stored in the consent cookie. Labels are the checkbos labes. If no label is set the key will be used instead. The cookies array are a list of cookies names that can be deleted (when possible) when the corresponding consent value is revoked. See the above example "Usage with PHP". Also is possible to start the checkbox checked or disabled [] ARRAY

Toggle popup, controls and details

<button class="cookie-consent-open">open</button>
<button class="cookie-consent-close">close</button>
<button class="cookie-consent-toggle">toggle</button>
<button class="cookie-consent-controls-open">Open controls</button>
<button class="cookie-consent-controls-close">Close controls</button>
<button class="cookie-consent-controls-toggle">Toggle controls</button>
<button class="cookie-consent-details-open">Open Details</button>
<button class="cookie-consent-details-close">Close Details</button>
<button class="cookie-consent-details-toggle">Toggle details</button>

CSS Example

.cookie-consent-popup {
    animation-name: show;
    animation-duration: 1s;
    animation-timing-function: ease;
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 999999;
}

.cookie-consent-popup.open {
    display: block;
    opacity: 1;
    animation-name: show;
    animation-duration: 1s;
    animation-timing-function: ease;
}

.cookie-consent-controls {
    max-height: 0;
    overflow: hidden;
    -webkit-transition: max-height 0.5s ease-out;
    -moz-transition: max-height 0.5s ease-out;
    transition: max-height 0.5s ease-out;
}

.cookie-consent-controls.open {
    margin: 0 0 30px 0;
    max-height: 600px;
}

.cookie-consent-details {
    max-height: 0;
    overflow: hidden;
    -webkit-transition: max-height 0.5s ease-out;
    -moz-transition: max-height 0.5s ease-out;
    transition: max-height 0.5s ease-out;
}

.cookie-consent-details.open {
    max-height: 600px;
}

@keyframes show {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes hide {
    from {opacity: 1;}
    to {opacity: 0;}
}

Settings config example phemellc/yii2-settings

  • section: cookie-consent
  • key: config
  • type: object
{
  "name": "cookie_consent_status",
  "path": "/",
  "domain": "",
  "expiryDays": 365,
  "message": "We use cookies to ensure the proper functioning of our website. For an improved visit experience we use analysis products. These are used when you agree with 'Statistics'.",
  "save": "Speichern",
  "learnMore": "Datenschutzerklärung",
  "link": "#",
  "consent": {
    "necessary": {
        "label": "Necessary",
        "checked": true,
        "disabled": true
    },
    "statistics": {
      "label": "Statistics",
      "cookies": [
        {
          "name": "_ga"
        },
        {
          "name": "_gat",
          "domain": "",
          "path": "/"
        },
        {
          "name": "_gid",
          "domain": "",
          "path": "/"
        }
      ]
    },
    "0": "marketing",
    "1": "external-media"
  }
}
<?php
    $config = Yii::$app->settings->get('config', 'cookie-consent', []);
    $config = isset($config->scalar) ? $config->scalar : '{}';
    $config = json_decode($config, true);
?>

<?= CookieConsent::widget($config) ?>

Worth knowing

Widgets throws an yii\base\InvalidConfigException if you define an invalid cookie consent helper component

yii2-cookie-consent's People

Contributors

eluhr avatar schmunk42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-cookie-consent's Issues

How to modify CSS?

I need to modify the integrated CSS.

How can this be done?

Is there a CSS using Bootstrap buttons?

I have posted a related question on Stackoverflow since I have no experience with overloading AssetBundles.

ERROR : category 'cookie-consent'

Hi,
sorry to post this, but I just don't know what I have to do here.

I get:

Unable to locate message source for category 'cookie-consent'.

as an error message.

Anyone an idea what I need to fix?
(I just installed and copy pasted component, use ..., and widget)

Thanks.

fix composer.json validation errors

Found cached composer.json of dmstr/yii2-cookie-consent (0.1.0)
Reading composer.json of dmstr/yii2-cookie-consent (feature/improvments)
Importing branch feature/improvments (dev-feature/improvments)
Skipped branch feature/improvments, Invalid package information: 
Deprecation warning: require.npm-asset/dmstr--cookie-consent is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*". Make sure you fix this as Composer 2.0 will error.

Reading composer.json of dmstr/yii2-cookie-consent (master)
Importing branch master (dev-master)
Skipped branch master, Invalid package information: 
Deprecation warning: require.npm-asset/dmstr--cookie-consent is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9]([_.-]?[a-z0-9]+)*". Make sure you fix this as Composer 2.0 will error.

CookieConsent is not defined

I've been getting some errors lately:

Uncaught SyntaxError: Unexpected token 'export'
Uncaught ReferenceError: CookieConsent is not defined

The error points to this part of the code:

<script>window.addEventListener('load', function () {
    window.cookieConsent = new CookieConsent({

I tested it with the copy & paste example from the README and still got it.

I can't remember when the problem came up.


current cookie consent version: 1.4.4
PHP: 7.3.12

composer update error

I added this line to composer require: section:

"dmstr/yii2-cookie-consent" : "~1.3.2"

When executing composer update it fails with

Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for dmstr/yii2-cookie-consent ~1.3.2 -> satisfiable by dmstr/yii2-cookie-consent[1.3.2].
    - dmstr/yii2-cookie-consent 1.3.2 requires npm-asset/dmstr--cookie-consent ^0.2.0 -> no matching package found.

Did I miss something?

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.