Download the PHP package elegantly/laravel-cookies-consent without Composer
On this page you can find all versions of the php package elegantly/laravel-cookies-consent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elegantly/laravel-cookies-consent
More information about elegantly/laravel-cookies-consent
Files in elegantly/laravel-cookies-consent
Package laravel-cookies-consent
Short Description Cookie consent for Laravel
License MIT
Homepage https://github.com/ElegantEngineeringTech/laravel-cookies-consent
Informations about the package laravel-cookies-consent
Laravel Cookies Consent Manager
This package provides a simple yet extremely flexible way to manage cookie consent in your Laravel application.
The default cookie banner design requires Tailwind CSS and Alpine.js, but you can publish the component and customize it with your own stack.
Requirements
Backend
- Laravel
Frontend
The default cookie consent banner included in this package requires:
- Blade components
- Alpine.js
- Tailwind CSS
- js-cookie
Installation
You can install the package via Composer:
You can publish the config file with:
This is the content of the published config file:
Usage
This package covers both backend and frontend cookie consent management.
You can choose to use the package only for backend capabilities or for both.
Backend Usage
In the backend, you will register the cookies and a callback associated with each of them. This callback will be a JavaScript script to run when the consent is granted.
Register Your Cookies
First, you should register all the cookies requiring user consent.
To manage cookies, the package provides a service accessible via the Facade: Elegantly\CookiesConsent\Facades\CookiesConsent
.
Cookie registration should be done in middleware to access the app and request context. This also allows you to choose the routes relying on those cookies.
To register your cookies, create a new middleware App\Http\Middleware\RegisterCookiesConsent
.
In this middleware, call CookiesConsent::register
to register groups of cookies.
- Cookies are always registered in groups.
- A cookie is defined by its
name
,lifetime
, and an optionaldescription
. - A cookie group can be defined as
required
. Such cookies cannot be rejected by the user, which is useful for essential cookies like the session cookie.
For example, all cookies related to "Marketing" can be registered together:
Registering Essential Cookies
The package provides a preset for essential cookies. Essential cookies are those that cannot be removed without compromising the application. By default, Laravel includes 2 essential cookies:
XSRF-TOKEN
- Session cookie
This package adds a third one:
- Consents (a cookie to store consents).
You can automatically register these three essential cookies using:
Registering Cookie Callbacks
Using the onAccepted
parameter, you can define the JavaScript code to execute when consent is granted to a specific cookie group.
In the previous example, we grant consent using the Facebook pixel.
Frontend Usage
Using the Default Cookie Banner
You can use the default cookie banner included with this package. It requires js-cookie, Alpine and tailwindcss.
js-cookie Requirement
The default banner implementation requires the js-cookie library to parse cookies in the browser.
Add it to your project using the CDN:
Or see their documentation to install it via npm.
Alpine.js Requirement
The default banner implementation requires Alpine.js for reactivity. Ensure it is included in your page.
Simply put the banner component <x-cookies-consent::banner />
at the end of your HTML page, and you are ready to go!
Tailwindcss Requirement
The default banner is based on elegantly/blade-kit
which is styled with tailwindcss.
You must then add the following paths to your tailwind config file:
Customizing the Default Component
You can customize the default component by publishing the views:
Using a Custom Component
You can design your own frontend cookie banner.
To retrieve all the cookie definitions, simply call:
Facebook Pixel Cookie Consent
The Facebook Pixel tracks users and conversions on the client side. Documentation available here.
This is the historic way to track conversions. Facebook & Meta now also provide a way to track your conversions directly from your backend. It is called "API conversions" and the documentation is available here.
This example will only cover the Facebook Pixel as the "API conversions" do not need cookie consent.
Example
The Pixel provides a built-in manager for consent. This example relies on this.
1. Revoke consent on load
Before calling fbq('init', ...)
and immediately after the Pixel script, revoke the consent:
2. Grant consent
In your middleware, register a cookie group and call fbq('consent', 'grant')
in the onAccepted
callback.
Every call to fbq
done before the consent will be triggered after fbq('consent', 'grant')
is called.
References
Facebook Guide: General Data Protection Regulation
Testing
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
- Quentin Gabriele
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-cookies-consent with dependencies
elegantly/blade-kit Version ^1.0.0
illuminate/contracts Version ^11.0||^12.0
spatie/laravel-package-tools Version ^1.16