Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Cookie Consent Laravel Package

marcogermani87/filament-cookie-consent

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Run:

    composer require marcogermani87/filament-cookie-consent
    

    Publish the config file:

    php artisan vendor:publish --provider="MarcoGermani87\FilamentCookieConsent\FilamentCookieConsentServiceProvider"
    
  2. Configure Edit config/filament-cookie-consent.php to define:

    • Required cookie types (e.g., statistics, marketing, functional).
    • Default consent state (e.g., denied, granted, or prompt).
    • Customize the banner text, buttons, and styling.
  3. 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.


Implementation Patterns

Core Workflow

  1. Consent Management

    • Use the 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');
      
  2. Customizing the Banner

    • Override the default widget by publishing views:
      php artisan vendor:publish --tag="filament-cookie-consent-views"
      
    • Modify resources/views/vendor/filament-cookie-consent/cookie-consent.blade.php to adjust HTML/CSS.
  3. Integration with Third-Party Services

    • Use the 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 { ... }
      }
      
    • Bind your service in AppServiceProvider:
      $this->app->bind(ConsentService::class, CustomConsentService::class);
      
  4. Dynamic Consent Logic

    • Leverage Filament’s widget events to trigger actions when consent changes:
      CookieConsent::listen('granted', function (string $type) {
          // Example: Load Google Analytics script
          if ($type === 'statistics') {
              \Livewire\wire('analytics')->loadScript();
          }
      });
      
  5. Multi-Tenant or Role-Based Consent

    • Extend the CookieConsent facade to fetch consent from a database (e.g., per user/tenant):
      CookieConsent::setConsentResolver(function () {
          return User::find(auth()->id())->cookieConsents;
      });
      

Gotchas and Tips

Common Pitfalls

  1. Cookie Domain Mismatch

    • If cookies aren’t persisting, ensure the domain in config/filament-cookie-consent.php matches your Filament panel’s domain (e.g., null for same-domain or .example.com for subdomains).
    • Debug with:
      \Illuminate\Support\Facades\Cookie::get('filament_cookie_consent');
      
  2. Caching Issues

    • Clear Filament’s cache after publishing views or config:
      php artisan filament:cache:clear
      
  3. Conflicting JavaScript

    • If the banner doesn’t render, check for JS conflicts. Ensure no other package is modifying the DOM before Filament’s widgets load. Use browser dev tools to inspect the banner’s presence.
  4. Default Consent State

    • Setting default_consent to granted in config may violate GDPR/CCPA. Test thoroughly with prompt to ensure compliance.

Debugging Tips

  • 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.

Extension Points

  1. Custom Cookie Names Override the cookie prefix in config:

    'cookie' => [
        'prefix' => 'myapp_',
    ],
    

    Now cookies will use names like myapp_cookie_consent_statistics.

  2. 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.',
    ];
    
  3. 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
    }
    
  4. 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();
        }
        // ...
    }
    
  5. A/B Testing Use Filament’s widget variants to test different banner designs:

    ->widgets([
        \MarcoGermani87\FilamentCookieConsent\Widgets\CookieConsent::class,
    ])
    ->widgetVariants([
        'cookie-consent' => CookieConsent::class,
    ]);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope