martin-schenk/laravel-cookie-consent

GDPR-compliant cookie consent system with minimal dependencies for Laravel 11

v0.1.1 2025-05-27 18:44 UTC

This package is auto-updated.

Last update: 2025-05-27 20:34:54 UTC


README

A lightweight, GDPR-compliant cookie consent system with minimal dependencies for Laravel 11

Perfect for Laravel applications that need a simple, effective, and legally compliant cookie consent solution without the bloat of external JavaScript frameworks.

Laravel Version License Minimal Dependencies GDPR Compliant

πŸ“Έ Screenshots

Cookie Consent Banner

Cookie Consent Banner

Cookie Settings Modal

Cookie Settings Modal

GDPR Compliance: No Analytics Cookies on Load

No Analytics Cookies on Initial Load

Screenshot shows the cookie consent banner with developer tools open, confirming no Google Analytics cookies are loaded before user consent.

✨ Highlights

  • Zero external dependencies - No Livewire, no Filament, no JS frameworks
  • Alpine.js & Vanilla JS - Uses Alpine.js that comes with Laravel
  • GDPR-compliant - Legally sound implementation with opt-in for all cookie types
  • Multilingual - German, English, Spanish and Chinese included
  • Google Analytics Integration - Dynamic loading and removal of GA scripts
  • Locale Cookie Management - Stores language preferences only with consent
  • Blade Components - Easy integration into existing Laravel applications
  • Tailwind CSS - Elegant, customizable design (optional)
  • Mobile-First - Fully responsive for all devices

πŸ’» Requirements

Laravel 11 includes Alpine.js by default, so no additional installation is needed for this dependency. Tailwind CSS is recommended but not strictly required - the component will still work with your own CSS, but you'll need to modify the published views to match your styling framework.

Checking Your Installed Versions

Use these commands to verify your installed versions:

# Check Laravel version
php artisan --version

# Check Tailwind CSS version (if installed via npm)
npx tailwindcss --version

# Check Alpine.js version
# For Laravel 11, inspect the package.json file or check the browser console:
cat package.json | grep alpine
# or in browser console on a page with Alpine.js:
console.log(Alpine.version)

🧠 How It Works

This plugin provides a lightweight, GDPR-compliant cookie consent system for Laravel 11 - fully implemented with Blade, Alpine.js, and vanilla JavaScript. It works without Livewire, Inertia, or external dependencies.

πŸ‡ͺπŸ‡Ί GDPR Compliance

The EU General Data Protection Regulation (GDPR) and similar privacy laws worldwide require explicit user consent before storing non-essential cookies. This package implements a fully compliant solution that:

  • Blocks all non-essential cookies by default
  • Provides clear opt-in choices for different cookie categories
  • Allows users to change their preferences at any time
  • Properly documents consent for compliance purposes

🌐 Google Analytics Integration

  • The plugin blocks any connection to Google Analytics by default
  • Only when a user explicitly consents to the "Analytics" category, the GA script (gtag.js) is dynamically inserted into the <head>
  • Website owners only need to enter their Google Tag ID (G-XXXXXXXXXX) - no additional customization required

🚫 Tracking Prevention When Declined

  • When analytics consent is declined, the GA script is not loaded
  • Additionally, the plugin attempts to delete all typical GA cookies, including:
    • _ga, _gid, gat*
    • __ga*, __gads
  • Even if some cookies cannot be fully deleted for technical reasons (e.g., HttpOnly), no tracking will occur as the JavaScript is not loaded

Result: Google has no access to user interactions unless consent is given.

πŸ‡ͺπŸ‡Έ Personal Preferences

The plugin currently stores a single preference: the language setting (locale).

  • The language is automatically read from the <html lang="..."> attribute
  • When the user consents to the preferences category, a locale=xx cookie is set (e.g., locale=en), which Laravel can use for automatic language switching
  • If consent is revoked, the locale cookie is deleted

πŸ”§ Technical Implementation

Component Technology
UI / Modals Blade + Tailwind
Logic Alpine.js (x-data)
Consent Storage localStorage
Cookie Handling JavaScript (document.cookie)
Language Detection document.documentElement.lang
GA Activation document.createElement('script')
GA Blocking No script inclusion + Cookie deletion

πŸš€ Installation

composer require martin-schenk/laravel-cookie-consent
php artisan vendor:publish --provider="MartinSchenk\CookieConsent\CookieConsentServiceProvider"

πŸ”§ Configuration

After publishing the package assets, you can configure the cookie consent system in config/cookie-consent.php:

// config/cookie-consent.php
return [
    'google_analytics_id' => '',  // Set your Google Analytics ID here (e.g. 'G-XXXXXXXXXX')
    
    'cookie_names' => [
        'consent' => 'cookieConsent',
        'locale' => 'locale',
    ],
    
    'cookie_lifetime' => 31536000,  // 1 year in seconds
    
    'categories' => [
        'essential' => true,  // Always required
        'analytics' => false, // Optional
        'preferences' => false, // Optional
    ],
];

You can modify the configuration values directly in the published configuration file:

πŸ“Š Usage

Include the cookie consent component in your main layout file:

<!-- In your layout file -->
<x-cookie-consent::cookie-consent />

Example Integration

Here's how to integrate the cookie consent component in your Laravel layout:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <!-- Your head content... -->
</head>
<body>
    <!-- Your site content... -->
    
    <!-- Footer with cookie settings link -->
    <footer>
        <a href="javascript:void(0);" onclick="window.openCookieSettings()">Cookie Settings</a>
    </footer>
    
    <!-- Cookie Consent Component (Place before closing body tag) -->
    <x-cookie-consent::cookie-consent />
</body>
</html>

Note: The cookie consent component should be placed before the closing </body> tag to ensure all other elements are loaded first. This provides the best user experience.

πŸ”— Adding a Cookie Settings Link to Your Footer

An important feature is the ability to reopen the cookie settings menu at any time. Here are different implementation options:

Option 1: Direct JavaScript Call

<a href="javascript:void(0);" onclick="window.openCookieSettings()" class="text-gray-400 hover:text-teal-400">
    Cookie Settings
</a>

Option 2: For Complex Layouts (e.g., in a Vue or React component)

// Trigger the cookie settings event
document.dispatchEvent(new CustomEvent('open-cookie-settings'));

Option 3: Use as a Blade Component

// In any of your Blade files:
<x-cookie-consent::settings-link class="text-gray-400 hover:text-teal-400" />

Practical Example: Typical Footer Implementation

<!-- Footer with legal links -->
<footer class="bg-gray-800 text-white py-6">
    <div class="container mx-auto">
        <div class="flex flex-wrap justify-center gap-4">
            <a href="{{ route('home') }}" class="text-gray-300 hover:text-white">Home</a>
            <a href="{{ route('imprint') }}" class="text-gray-300 hover:text-white">Imprint</a>
            <a href="{{ route('privacy') }}" class="text-gray-300 hover:text-white">Privacy Policy</a>
            
            <!-- Cookie Settings Link -->
            <a href="javascript:void(0);" 
               onclick="window.openCookieSettings()" 
               class="text-gray-300 hover:text-white">
                Cookie Settings
            </a>
        </div>
    </div>
</footer>

🌐 Supported Languages

This package comes with translations for:

  • English (en)
  • German (de)
  • Spanish (es)
  • Chinese (Simplified) (zh_CN)

You can publish the language files to customize them:

php artisan vendor:publish --provider="MartinSchenk\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-lang"

🎨 Customizing Appearance

The cookie consent component uses Tailwind CSS by default. You can publish the views to customize the appearance:

php artisan vendor:publish --provider="MartinSchenk\CookieConsent\CookieConsentServiceProvider" --tag="cookie-consent-views"

πŸ’― Why Choose This Package?

This plugin is the ideal solution for modern Laravel 11 projects that want to:

  • Stay GDPR-compliant βœ…
  • Maintain maximum control βœ…
  • Avoid unnecessary external consent tools βœ…
  • Remain fully customizable and open-source βœ…

πŸ” Keywords

Laravel cookie consent, GDPR cookie banner, Laravel 11 cookie solution, cookie consent manager, Laravel GDPR compliance, cookie opt-in system, Alpine.js cookie consent, privacy cookie banner, EU cookie law Laravel, cookie consent popup, minimal cookie notice, lightweight cookie consent, Laravel cookie management, cookie consent without JavaScript frameworks

πŸ‘₯ Contributing

Contributions are highly welcome and appreciated!

This project is open for contributions, and we would love for you to help make it better. Whether you want to fix a bug, add a new feature, improve the documentation, or just give feedback, your help is welcome.

Please see CONTRIBUTING.md for detailed guidelines on how to contribute to this project.

🌱 Good First Issues

Looking for something to work on? Issues labeled with good first issue are a great way to start contributing.

πŸ—ΊοΈ Roadmap

We're planning to add the following features in future releases:

  • Support for additional cookie categories
  • More styling options and themes
  • Additional analytics services integration
  • Cookie expiration management
  • Support for older Laravel versions

Feel free to pick up any of these tasks or suggest new ones!

β˜• Support this project

If this package made your life a little easier,
you can buy me a coffee – or make my day by sending a little note:

πŸ“¬ Send a postcard to:
Martin Schenk
Calle Hiruela 3, 5-7
28035 Madrid, Spain

Small gestures, big smiles. Danke! πŸ’š

πŸ“ License

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