Try Depot: Bring ultra-fast, remote Docker builds directly to your Laravel workflow

Simple Cookie Consent Package for Laravel

Last updated on by

Simple Cookie Consent Package for Laravel image

The Laravel Cookie Guard package provides a simple cookie consent window through which users can specify the cookies they want to allow. After the user submits the modal, these cookies are ready for use on the frontend.

Once you've configured this package and defined the cookies you want on your site, you can check to see if a given cookie category is allowed. The readme example uses PHP's $_COOKIE superglobal, however, you could build a helper for this package to simplify consent cookie checks:

<!-- Check the 'targeting' cookie: -->
@if(isset($_COOKIE[config('cookies_consent.cookie_prefix') . 'cookies_consent']))
@php
$cookiesConsent = json_decode($_COOKIE[config('cookies_consent.cookie_prefix') . 'cookies_consent'], true);
@endphp
@if(isset($cookiesConsent['targeting']) && $cookiesConsent['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
@endif

Note: the readme example depends on server-rendered HTML that is not cached.

On the frontend, you can use window.localStorage to check for appropriate cookies defined in the configuration file. That will allow you to dynamically write cookie consent logic on the frontend.

Main Features

  • Customizable cookie categories
  • 3 Different integration options: All-in-one dialog, internal page, external separate page,
  • A clean and intuitive UI for the cookies consent modal.
  • A sticky cookies button and footer link that allows users to change their cookies preferences at any time. This button is optional, and it's existence can be tweaked in the configuration file.
  • A separate page for the cookie preferences, where users can read more about each cookie category and change their preferences.
  • Multilingual support. The plugin comes with 6 built-in languages: English, Greek, Spanish, German, Italian, and Swedish. You can add your own language by publishing the translations and editing/adding your own translations.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Also, you can install and configure this package using Composer and Artisan:

composer require scify/laravel-cookie-guard
php artisan vendor:publish \
--provider="SciFY\LaravelCookiesConsent\LaravelCookiesConsentServiceProvider" \
--tag="cookies-consent-public" \
--tag="cookies-consent-config"
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Boost Guidelines & Skills Added in Inertia v2.3.16 image

Boost Guidelines & Skills Added in Inertia v2.3.16

Read article
Laravel Launches an Open Directory of AI Agent Skills for Laravel and PHP image

Laravel Launches an Open Directory of AI Agent Skills for Laravel and PHP

Read article
Filament turns five in style! image

Filament turns five in style!

Read article
Cache Concurrency Limiting in Laravel 12.53.0 image

Cache Concurrency Limiting in Laravel 12.53.0

Read article
Stop Failing APIs from Killing Your Queue — Fuse for Laravel image

Stop Failing APIs from Killing Your Queue — Fuse for Laravel

Read article
Blaze: An Optimized Blade Template Compiler for Laravel image

Blaze: An Optimized Blade Template Compiler for Laravel

Read article