keepsuit/laravel-cookie-solution

Make your site comply with EU cookie law

0.5.1 2024-07-24 09:35 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a configurable cookie banner for your Laravel application. It also includes templates for cookie policy and privacy policy pages.

Note that this package is not a legal advice. You should always consult a lawyer and change texts and policies for your needs.

Installation

You can install the package via composer:

composer require keepsuit/laravel-cookie-solution

You can publish the config file with:

php artisan vendor:publish --tag="cookie-solution-config"

This is the contents of the published config file:

return [
    /**
     * Enable or disable the cookie solution.
     */
    'enabled' => env('COOKIE_SOLUTION_ENABLED', true),

    /**
     * Name of the cookie where we store the user's consent.
     */
    'cookie_name' => 'laravel_cookie_solution',

    /**
     * The cookie's lifetime in days.
     */
    'cookie_lifetime' => 365,
    
    /**
     * Banner highlight color (ex. #3522dd).
     * If null, the default color will be used.
     */
    'highlight_color' => null,
    
    /**
     * Cookie toggle position (left or right).
     */
    'toggle_position' => 'right',
    
    /**
     * The entity responsible for the processing of the data.
     */
    'data_owner' => [
        /**
         * Email address of the data owner.
         */
        'contact_email' => null,

        /**
         * Name/Company name and address of the data owner.
         * This is parsed as Markdown (you can use __text__ for bold and _text_ for italic).
         */
        'name_and_address' => null,
    ],
];

Optionally, you can publish the assets, views and translations using.

php artisan vendor:publish --tag="cookie-solution-assets"
php artisan vendor:publish --tag="cookie-solution-views"
php artisan vendor:publish --tag="cookie-solution-translations"

If you publish the assets, remember to publish new versions when you update the package. You can automate this using composer post-update-cmd script:

{
    "scripts": {
        "post-update-cmd": [
            // ...
            "@php artisan vendor:publish --tag=cookie-solution-assets --force"
        ]
    }
}

Usage

Include the cookie solution script in your layout (it's recommended to include it in the <head> tag):

@include('cookie-solution::script')

Register used services in your 'AppServiceProvider' (You can register your own services with Service class):

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Keepsuit\CookieSolution\CookieSolution;
use Keepsuit\CookieSolution\ServiceFactories\Google\GoogleAnalytics4ServiceFactory;
use Keepsuit\CookieSolution\ServiceFactories\Google\GoogleDataProcessingLocation;
use Keepsuit\CookieSolution\ServiceFactories\Google\GoogleTagManagerServiceFactory;
use Keepsuit\CookieSolution\ServiceFactories\Meta\FacebookPixelServiceFactory;
use Keepsuit\CookieSolution\ServiceFactories\Meta\MetaDataProcessingLocation;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this->app->afterResolving(CookieSolution::class, function (CookieSolution $cookieSolution) {
            $cookieSolution->register(GoogleAnalytics4ServiceFactory::new()->location(GoogleDataProcessingLocation::IRELAND)->build())
                ->register(GoogleTagManagerServiceFactory::new()->location(GoogleDataProcessingLocation::IRELAND)->build())
                ->register(FacebookPixelServiceFactory::new()->location(MetaDataProcessingLocation::IRELAND)->build());
        });
    }
}

Update the config config/cookie-consent.php file with your data owner's contact email and name and address.

return [
    // ...
    /**
     * The entity responsible for the processing of the data.
     */
    'data_owner' => [
        /**
         * Email address of the data owner.
         */
        'contact_email' => 'your_email@example.com',

        /**
         * Name/Company name and address of the data owner.
         * This is parsed as Markdown (you can use __text__ for bold and _text_ for italic).
         */
        'name_and_address' => <<<MARKDOWN
            __Your Company Name__
            Your Street 1
            City, Country
            MARKDOWN,
    ],
];

Create a route for the cookie policy page and include the cookie policy partial:

<body>
    @include('cookie-solution::cookie-policy')
</body>

Create a route for the privacy policy page and include the privacy policy partial:

<body>
    @include('cookie-solution::privacy-policy')
</body>

See views customization section for more information about the partials and how to customize them.

Customization

Highlight color

Yon can customize the highlight color of the banner and toggle changing the highlight_color config value.

Toggle position

You can change the toggle position to left or right by changing the toggle_position config value (default to right). If you need a more advanced customization, you can edit the toggle position with some CSS:

:root {
    --cs--toggle-position-bottom: 4rem; /* position from the bottom */
    --cs--toggle-position-x: 2rem; /* position from the left or right (depending on `toggle_position` value) */
}

/* change the position for desktop */
@media (min-width: 1024px) {
    :root {
        --cs--toggle-position-bottom: 2rem;
        --cs--toggle-position-x: 1rem;
    }
}

Views

If you want to customize the views, you can publish them with php artisan vendor:publish --tag="cookie-solution-views" and style them however you like. The <cookie-solution-policy-formatter/> custom component is used to apply the default styles, you can safely remove it to customize the views.

Checking status

You can check the user's consent status from laravel using the CookieSolution::status() method, which returns a CookieSolutionStatus object with helpers to check if a purpose has been accepted.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.