marcogermani87/filament-cookie-consent
Installation Run:
composer require marcogermani87/filament-cookie-consent
Publish the config file:
php artisan vendor:publish --provider="MarcoGermani87\FilamentCookieConsent\FilamentCookieConsentServiceProvider"
Configure
Edit config/filament-cookie-consent.php to define:
statistics, marketing, functional).denied, granted, or prompt).First Use Case
Add the widget to your Filament panel in app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->widgets([
\MarcoGermani87\FilamentCookieConsent\Widgets\CookieConsent::class,
]);
}
The cookie consent banner will now appear automatically in your Filament admin panel.
Consent Management
CookieConsent facade to check or update consent status:
use MarcoGermani87\FilamentCookieConsent\Facades\CookieConsent;
// Check if consent is granted for a specific type
if (CookieConsent::isGranted('statistics')) {
// Enable analytics
}
// Grant consent programmatically
CookieConsent::grant('marketing');
Customizing the Banner
php artisan vendor:publish --tag="filament-cookie-consent-views"
resources/views/vendor/filament-cookie-consent/cookie-consent.blade.php to adjust HTML/CSS.Integration with Third-Party Services
ConsentService contract to extend functionality:
use MarcoGermani87\FilamentCookieConsent\Contracts\ConsentService;
class CustomConsentService implements ConsentService {
public function isGranted(string $type): bool { ... }
public function grant(string $type): void { ... }
public function deny(string $type): void { ... }
}
AppServiceProvider:
$this->app->bind(ConsentService::class, CustomConsentService::class);
Dynamic Consent Logic
CookieConsent::listen('granted', function (string $type) {
// Example: Load Google Analytics script
if ($type === 'statistics') {
\Livewire\wire('analytics')->loadScript();
}
});
Multi-Tenant or Role-Based Consent
CookieConsent facade to fetch consent from a database (e.g., per user/tenant):
CookieConsent::setConsentResolver(function () {
return User::find(auth()->id())->cookieConsents;
});
Cookie Domain Mismatch
domain in config/filament-cookie-consent.php matches your Filament panel’s domain (e.g., null for same-domain or .example.com for subdomains).\Illuminate\Support\Facades\Cookie::get('filament_cookie_consent');
Caching Issues
php artisan filament:cache:clear
Conflicting JavaScript
Default Consent State
default_consent to granted in config may violate GDPR/CCPA. Test thoroughly with prompt to ensure compliance.Log Consent Changes Add a listener to debug consent states:
CookieConsent::listen('*', function ($event, $type) {
\Log::info("Cookie consent {$event} for {$type}");
});
Inspect Cookies
Use browser dev tools (Application > Cookies) to verify cookie names (filament_cookie_consent_*) and values.
Disable Banner Temporarily
Set enabled: false in config to test functionality without the UI.
Custom Cookie Names Override the cookie prefix in config:
'cookie' => [
'prefix' => 'myapp_',
],
Now cookies will use names like myapp_cookie_consent_statistics.
Localization Extend the banner text via language files:
// resources/lang/en/cookie-consent.php
return [
'title' => 'Custom Cookie Consent Title',
'description' => 'Custom description with {link} for details.',
];
Conditional Rendering
Hide the banner for specific users/roles by overriding the widget’s canView() method:
public static function canView(): bool
{
return !auth()->user()->isAdmin; // Example: Hide for admins
}
Persistent Storage
For complex apps, store consent in the database instead of cookies. Extend the ConsentService to use Eloquent models:
class DatabaseConsentService implements ConsentService {
public function isGranted(string $type): bool {
return auth()->user()->cookieConsents()->where('type', $type)->exists();
}
// ...
}
A/B Testing Use Filament’s widget variants to test different banner designs:
->widgets([
\MarcoGermani87\FilamentCookieConsent\Widgets\CookieConsent::class,
])
->widgetVariants([
'cookie-consent' => CookieConsent::class,
]);
How can I help you explore Laravel packages today?