PHPackages                             leobsst/laravel-cookie-consent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. leobsst/laravel-cookie-consent

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

leobsst/laravel-cookie-consent
==============================

A cookie consent package for Laravel applications, providing an easy way to manage user consent for cookies in compliance with privacy regulations.

v2.0.1(2mo ago)01.3k↓50%[1 PRs](https://github.com/leobsst/laravel-cookie-consent/pulls)MITPHPPHP ^8.2CI passing

Since Nov 4Pushed 3mo agoCompare

[ Source](https://github.com/leobsst/laravel-cookie-consent)[ Packagist](https://packagist.org/packages/leobsst/laravel-cookie-consent)[ Docs](https://github.com/leobsst/laravel-cookie-consent)[ GitHub Sponsors](https://github.com/LEOBSST)[ RSS](/packages/leobsst-laravel-cookie-consent/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (7)Dependencies (31)Versions (13)Used By (0)

[![Laravel Logo](https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg)](https://laravel.com)

 [![Version](https://camo.githubusercontent.com/4e914e309092d2d9c17520eed9f92bd0d0a6602a73da2000cec3858cb406382c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c656f627373742f6c61726176656c2d636f6f6b69652d636f6e73656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/leobsst/laravel-cookie-consent) [![Downloads](https://camo.githubusercontent.com/4937d9a0dd041033f265ed79c6f944e97b17f8bf3af079a78c23aad8a4f6713f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c656f627373742f6c61726176656c2d636f6f6b69652d636f6e73656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/leobsst/laravel-cookie-consent) [![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](https://opensource.org/licenses/MIT) [![Laravel](https://camo.githubusercontent.com/aa19b7fc29a65745f883c82d87a70317433a86f0844f93a4028d26fb44caaaa3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31312e392b2d4646324432303f6c6f676f3d6c61726176656c)](https://laravel.com) [![PHP](https://camo.githubusercontent.com/c4592483be21a67431df3607bb00d9aa91ffb5c78a669a30d52ae5acded2f96a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322b2d3737374242343f6c6f676f3d706870)](https://www.php.net)

Laravel Cookie Consent
======================

[](#laravel-cookie-consent)

A modern Laravel package for handling cookie consent with Google Tag Manager integration. This package provides a Livewire-based cookie consent banner with built-in support for Google Consent Mode v2, making GDPR compliance simple and straightforward.

Features
--------

[](#features)

- Livewire-based interactive cookie consent banner
- Simple binary consent system (Accept/Refuse)
- Automatic Google Tag Manager integration
- Consent Mode v2 support for Google Analytics
- Persistent user preferences stored in cookies (1 year duration)
- Tailwind CSS styling with customizable accent color
- Easy integration with existing Laravel projects
- Multi-language support (EN, FR, IT, ES, DE)

Requirements
------------

[](#requirements)

- PHP 8.2 or higher
- Laravel 11.9 or higher
- Livewire 3.6.4 or higher
- Tailwind CSS

Installation
------------

[](#installation)

### Step 1: Install the Package

[](#step-1-install-the-package)

```
composer require leobsst/laravel-cookie-consent
```

### Step 2: Publish Assets (Required)

[](#step-2-publish-assets-required)

```
php artisan vendor:publish --tag=cookie-consent-assets
```

This publishes:

- `public/vendor/cookie-consent/laravel-cookie-consent.js` - JavaScript file that handles Google Tag Manager consent updates

### Step 3: Publish config file (highly recommended)

[](#step-3-publish-config-file-highly-recommended)

```
php artisan vendor:publish --tag=cookie-consent-config
```

This will create a `config/cookie-consent.php` file where you can customize:

- **GOOGLE\_TAG\_MANAGER\_ID**: Your Google Tag Manager ID (GTM-XXXXXXX)
- **LEARN\_MORE\_LINK**: URL or route name for the "Learn more" link (default: `/privacy-policy`)
- **CONSENT\_BANNER\_VIEW**: Custom view for the consent banner
- **ACCENT\_COLOR**: Accent color for buttons and links (default: `#3490dc`)

### Alternative: Use Install Command

[](#alternative-use-install-command)

You can also use the built-in install command which will guide you through the setup:

```
php artisan cookie-consent:install
```

This command will:

- Publish the configuration file
- Publish the package assets
- Ask you to star the repository on GitHub

Configuration
-------------

[](#configuration)

### Tailwind CSS Integration

[](#tailwind-css-integration)

Important

To ensure the package's views are properly styled with Tailwind CSS, you need to add the following directive to your project's `app.css` file

```
/* COMPILE TAILWINDCSS DIRECTIVES IN VIEWS */
@source '../../../../vendor/leobsst/laravel-cookie-consent/resources/views/**/*.blade.php';
```

This ensures that Tailwind CSS processes the utility classes used in the cookie consent banner.

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

1. **Add the Google Tag Manager ID to your `.env` file:**

```
GOOGLE_TAG_MANAGER_ID=GTM-XXXXXXX
```

2. **Include the cookie consent scripts and component in your layout file:**

```

    @cookieConsentScripts

```

The `@cookieConsentScripts` directive loads Google Tag Manager with Consent Mode v2 enabled. The banner will automatically appear for users who haven't set their cookie preferences yet.

### How It Works

[](#how-it-works)

The package implements a simple binary consent system:

- **User accepts**: Google Tag Manager consent is updated to grant `ad_storage`, `ad_user_data`, and `ad_personalization` (while `analytics_storage` remains denied)
- **User refuses**: All non-essential consents remain denied
- **Consent status**: Stored in a cookie named `cookie_consent` (value: '1' for accepted, '0' for refused, null when not set) with 1 year duration

The middleware automatically shares the consent status from the cookie with all views, ensuring tracking scripts only load when consent is granted.

### Complete Example

[](#complete-example)

Here's a complete example of a layout file with the cookie consent banner:

```

    {{ config('app.name') }}

    @vite(['resources/css/app.css', 'resources/js/app.js'])

    {{-- Google Tag Manager with Consent Mode --}}
    @cookieConsentScripts

        {{ $slot }}

    {{-- Cookie Consent Banner --}}

```

### Checking User Consent

[](#checking-user-consent)

You can check the user's consent status in your application using the cookie or the shared view variable:

```
// Option 1: Check the cookie directly
$consent = request()->cookie('cookie_consent');

if ($consent === '1') {
    // User has accepted cookies
} elseif ($consent === '0') {
    // User has refused cookies
} else {
    // User hasn't made a choice yet (null)
}

// Option 2: Use the shared view variable (available in all views via middleware)
if ($cookieConsentStatus === '1') {
    // User has accepted cookies
}
```

### Customizing the Banner

[](#customizing-the-banner)

You can customize the banner's appearance by publishing and modifying the views:

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

This will publish the views to `resources/views/vendor/cookie-consent/` where you can customize them to match your design.

### Translations

[](#translations)

The package includes translations for the following languages:

- English (en)
- French (fr)
- Italian (it)
- Spanish (es)
- German (de)

The banner automatically uses the language configured in your Laravel application (`config('app.locale')`).

**Available translation keys:**

- `cookie-consent::translations.description`: Description of cookie usage
- `cookie-consent::translations.question`: Question asking for consent
- `cookie-consent::translations.learn_more`: "Learn more" link text
- `cookie-consent::translations.accept`: "Accept" button text
- `cookie-consent::translations.refuse`: "Refuse" button text

#### Publishing Translations

[](#publishing-translations)

To customize the translations, publish the language files:

```
php artisan vendor:publish --tag=cookie-consent-lang
```

This will publish the translation files to `resources/lang/vendor/cookie-consent/` where you can modify them.

Google Tag Manager Consent Mode
-------------------------------

[](#google-tag-manager-consent-mode)

The package implements Google Consent Mode v2 with the following default settings:

**When user hasn't decided (default state):**

```
{
  'functional_storage': 'granted',
  'security_storage': 'granted',
  'analytics_storage': 'denied',
  'ad_storage': 'denied',
  'ad_user_data': 'denied',
  'ad_personalization': 'denied'
}
```

**When user accepts:**

```
{
  'functional_storage': 'granted',
  'security_storage': 'granted',
  'analytics_storage': 'denied',
  'ad_storage': 'granted',
  'ad_user_data': 'granted',
  'ad_personalization': 'granted'
}
```

**When user refuses:**All non-essential consents remain denied.

The JavaScript file ([resources/js/index.js](resources/js/index.js)) listens for Livewire events and updates the Google Tag Manager consent accordingly.

Advanced Configuration
----------------------

[](#advanced-configuration)

### Environment Variables

[](#environment-variables)

Add these variables to your `.env` file:

```
# Required for Google Tag Manager integration
GOOGLE_TAG_MANAGER_ID=GTM-XXXXXXX

# Optional customizations
COOKIE_LEARN_MORE_LINK=/privacy-policy
COOKIE_CONSENT_BANNER_VIEW=cookie-consent::livewire.cookie-consent
COOKIE_CONSENT_ACCENT_COLOR=#3490dc
```

### Custom View

[](#custom-view)

To use a completely custom view for the consent banner:

1. Create your custom view file
2. Set the `CONSENT_BANNER_VIEW` in your config file or `.env`
3. Ensure your view uses the same Livewire component properties (`$consent`, `$loadScript`, `$learnMoreLink`)

### Middleware

[](#middleware)

The `HandleCookieConsent` middleware is automatically registered in the `web` middleware group. It:

- Retrieves the consent status from the `cookie_consent` cookie
- Shares `$cookieConsentStatus` with all views ('1' for accepted, '0' for refused, null when not set)

FAQ
---

[](#faq)

### Why is analytics\_storage always denied?

[](#why-is-analytics_storage-always-denied)

The package currently sets `analytics_storage` to denied by default to provide a conservative approach to GDPR compliance. You can modify this behavior in the JavaScript file if your use case requires it.

### How do I reset a user's consent?

[](#how-do-i-reset-a-users-consent)

You can clear the cookie by expiring it:

```
use Illuminate\Support\Facades\Cookie;

// Queue cookie for deletion
Cookie::queue(Cookie::forget('cookie_consent'));

// Or in a controller response
return response()->json(['success' => true])
    ->withCookie(Cookie::forget('cookie_consent'));
```

### Can I use this without Google Tag Manager?

[](#can-i-use-this-without-google-tag-manager)

Yes! Simply don't set the `GOOGLE_TAG_MANAGER_ID` environment variable. The banner will still work and store the user's consent preference, which you can use to conditionally load your own tracking scripts.

### How do I style the banner to match my design?

[](#how-do-i-style-the-banner-to-match-my-design)

You have several options:

1. Use the `ACCENT_COLOR` config to change the button/link color
2. Publish the views and modify the Tailwind classes
3. Create a completely custom view and set `CONSENT_BANNER_VIEW`

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [LEOBSST](https://github.com/LEOBSST)
- [B.L.A.M. PRODUCTION](https://linksly.fr/BLAM-PRODUCTION)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance83

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Recently: every ~30 days

Total

11

Last Release

62d ago

Major Versions

1.x-dev → v2.0.02026-03-18

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/46266177?v=4)[LEOBSST](/maintainers/leobsst)[@leobsst](https://github.com/leobsst)

---

Top Contributors

[![leobsst](https://avatars.githubusercontent.com/u/46266177?v=4)](https://github.com/leobsst "leobsst (28 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravellaravel-cookie-consentLEOBSSTBLAM PRODUCTION

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/leobsst-laravel-cookie-consent/health.svg)

```
[![Health](https://phpackages.com/badges/leobsst-laravel-cookie-consent/health.svg)](https://phpackages.com/packages/leobsst-laravel-cookie-consent)
```

###  Alternatives

[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[leandrocfe/filament-apex-charts

Apex Charts integration for Filament PHP.

4861.2M8](/packages/leandrocfe-filament-apex-charts)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
