zsoltjanes/laravel-cookie-consent

A flexible, elegant cookie consent banner package for Laravel. Easy to customize, easy to love.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

Language:Blade

pkg:composer/zsoltjanes/laravel-cookie-consent

v0.1.0 2025-11-09 00:26 UTC

This package is auto-updated.

Last update: 2025-11-09 00:32:10 UTC


README

A flexible, elegant and fully GDPR-friendly cookie consent banner for Laravel.
Easy to integrate, easy to customize, and sweet as a fresh-baked cookie.

Built by Zsolt Janes with love, caffeine and far too many browser tabs.

✨ Features

  • ✅ Full cookie consent banner with categories
  • ✅ Google Consent Mode v2 compatible
  • ✅ GA4, Hotjar, Meta Pixel conditional loading
  • ✅ Strict/Performance/Functionality/Targeting categories
  • ✅ Customizable texts, translations, privacy policy link
  • ✅ Middleware-based visibility logic
  • ✅ Publishable Blade view, assets, and language files
  • ✅ Zero front-end dependencies
  • ✅ Works with Laravel 10 & 11

📦 Installation

Require the package via Composer:

composer require zsoltjanes/laravel-cookie-consent

Publish assets, config and view files as needed:

php artisan vendor:publish --tag=cookie-consent-config
php artisan vendor:publish --tag=cookie-consent-assets
php artisan vendor:publish --tag=cookie-consent-lang
# optional:
php artisan vendor:publish --tag=cookie-consent-views

⚙️ Configuration

The configuration can be found in:

config/cookie-consent.php

Key settings:

return [
    'enabled' => true,

    'cookie' => [
        'name' => 'was_cookie_consent_visible',
        'lifetime_days' => 365,
    ],

    'exclude_paths' => [
        '^/admin',
        '^/filament',
        '^/_debugbar',
    ],

    'integrations' => [
        'ga4' => [
            'enabled' => env('COOKIE_CONSENT_GA4_ENABLED', false),
            'measurement_id' => env('GA4_MEASUREMENT_ID'),
        ],
        'hotjar' => [
            'enabled' => env('COOKIE_CONSENT_HOTJAR_ENABLED', false),
            'hjid' => env('HOTJAR_ID'),
            'hjsv' => 6,
        ],
        'meta_pixel' => [
            'enabled' => env('COOKIE_CONSENT_META_PIXEL_ENABLED', false),
            'pixel_id' => env('META_PIXEL_ID'),
        ],
    ],
];

🌐 Usage

Add the middleware to your web group:

// app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        // ...
        \ZsoltJanes\CookieConsent\Http\Middleware\CheckCookieConsent::class,
    ],
];

Then include the banner in your layout (usually before </body>):

<x-cookie-consent::banner />

That’s it.
If the user has not interacted with the banner before, it appears automatically.

🌍 Integration Examples

GA4 (Google Analytics)

Add to .env:

COOKIE_CONSENT_GA4_ENABLED=true
GA4_MEASUREMENT_ID=G-XXXXXXX

Hotjar

COOKIE_CONSENT_HOTJAR_ENABLED=true
HOTJAR_ID=2012719

Meta Pixel

COOKIE_CONSENT_META_PIXEL_ENABLED=true
META_PIXEL_ID=1234567890

Only loads when the appropriate cookie category is granted.

🎨 Customization

You can customize:

  • Blade view
  • Translations
  • Icon assets
  • Cookie category names
  • Privacy policy URL
  • Colors and layout

Simply publish the views:

php artisan vendor:publish --tag=cookie-consent-views

And edit:

resources/views/vendor/cookie-consent/components/banner.blade.php

📡 Events

When the user updates consent settings, a browser event is dispatched:

window.addEventListener('cookieconsent:update', (e) => {
    console.log('Consent updated:', e.detail);
});

Use this to load your own scripts conditionally.

✅ Supported Laravel Versions

  • Laravel 10
  • Laravel 11

PHP 8.1 or higher required.

🤝 Contributing

Pull requests are welcome.
If you have an idea that makes the banner smarter, sweeter, or more powerful, open an issue or PR.

📄 License

MIT License.
Enjoy it, modify it, ship it.

🍪 Closing Note

Cookie banners are usually boring.
This one doesn’t have to be.

If your users must click something, at least make it smooth, clean, and respectful — this package aims exactly for that.

Made by zsoltjanes.