Blog Detail

12

Dec
Make your Laravel App compliant with the EU GDPR Cookie Law cover image

arrow_back Make your Laravel App compliant with the EU GDPR Cookie Law

According to the GDPR law, every platform is required to allow the users to decide which cookie categories they will allow. If a cookie category is not permitted, the application should not use the functionality tied to that cookie.

This plugin provides a simple cookie consent window through which the user can specify the cookies they would like to allow.

After the user submission, the page reloads and the relevant cookies are set on the browser, and can then be used in the front end.

Features

  • Customizable cookie categories
  • Customizable pop-up view and style
  • Customizable show/hide “Read more” link
  • Customizable translations (6 languages already included)

Installation

You can install the package via composer:

composer require scify/laravel-cookies-consent

If on Laravel 9 or newer, the assets files (style.css) will automatically be published

If on Laravel 8 or older, make sure to manually publish the styles file, by running:

php artisan vendor:publish \
--provider="SciFY\LaravelCookiesConsent\LaravelCookiesConsentServiceProvider" \
--tag="laravel-assets"

In both cases, the assets files will be copied to public/vendor/cookies_consent.

You can then either decide to include the public/vendor/cookies_consent/style.css file to git (especially if you want to edit it first), or add it to .gitignore, and make sure also to run this command on the staging/production server.

Usage

When the plugin is installed, a laravel-cookies-consent custom Laravel View Component is automatically registered.

This will render the following cookies consent that, will look very much like this one.

You can then use this component in order to display the cookies consent window, wherever you’d like.

Typically, a good strategy is to put the component just before the closing tag:

<body>
    ...
    ...
    ...
    <x-laravel-cookies-consent></x-laravel-cookies-consent>
</body>

After that, you can use the $_COOKIE global object, in order to check for the appropriate cookie.

Now you can use this object in your Blade files like this:

$_COOKIE[config('cookies_consent.cookie_prefix') . {{ COOKIE_NAME }}]

For example, An application that wants to load the Google Analytics script only if the user has given their consent to the targeting cookie category might do the following:

google-analytics.blade.php

<!-- Check the 'targeting' cookie: -->
@if(isset($_COOKIE[config('cookies_consent.cookie_prefix') 
. 'cookies_consent_targeting']) && config('app.google_analytics_id'))
    
    <!-- Google Analytics -->
    <script defer async>
        (function (i, s, o, g, r, a, m) {
            i['GoogleAnalyticsObject'] = r;
            i[r] = i[r] || function () {
                (i[r].q = i[r].q || []).push(arguments)
            }, i[r].l = 1 * new Date();
            a = s.createElement(o),
                m = s.getElementsByTagName(o)[0];
            a.async = 1;
            a.src = g;
            m.parentNode.insertBefore(a, m)
        })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');

        window.ga('create', '{{ config('app.google_analytics_id') }}', 'auto');
        window.ga('set', 'anonymizeIp', true);
        window.ga('send', 'pageview');
    </script>
@endif

In this example, we checked whether the $_COOKIE[config(‘cookies_consent.cookie_prefix’) . ‘cookies_consent_targeting’] key exists or not.

For more details, you can visit Github.

Published at : 12-12-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project