{"id":27145444,"url":"https://github.com/elegantengineeringtech/laravel-cookies-consent","last_synced_at":"2025-04-08T09:48:16.895Z","repository":{"id":248375340,"uuid":"828522335","full_name":"ElegantEngineeringTech/laravel-cookies-consent","owner":"ElegantEngineeringTech","description":"Extremly flexible Cookies consent manager for Laravel","archived":false,"fork":false,"pushed_at":"2025-03-10T02:57:38.000Z","size":97,"stargazers_count":10,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T15:48:20.229Z","etag":null,"topics":["cookie-consent","cookieconsent","cookies-consent","gdpr","laravel","laravel-package","php","tall-stack"],"latest_commit_sha":null,"homepage":"https://elegantengineering.tech/laravel-cookies-consent","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ElegantEngineeringTech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ElegantEngineeringTech"}},"created_at":"2024-07-14T12:09:26.000Z","updated_at":"2025-03-21T19:41:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"22ed4560-17a3-4580-aa43-a9806f05b852","html_url":"https://github.com/ElegantEngineeringTech/laravel-cookies-consent","commit_stats":null,"previous_names":["elegantengineeringtech/laravel-cookies-consent"],"tags_count":14,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-cookies-consent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-cookies-consent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-cookies-consent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ElegantEngineeringTech%2Flaravel-cookies-consent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ElegantEngineeringTech","download_url":"https://codeload.github.com/ElegantEngineeringTech/laravel-cookies-consent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247818653,"owners_count":21001383,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cookie-consent","cookieconsent","cookies-consent","gdpr","laravel","laravel-package","php","tall-stack"],"created_at":"2025-04-08T09:48:16.286Z","updated_at":"2025-04-08T09:48:16.882Z","avatar_url":"https://github.com/ElegantEngineeringTech.png","language":"PHP","funding_links":["https://github.com/sponsors/ElegantEngineeringTech"],"categories":[],"sub_categories":[],"readme":"# Laravel Cookies Consent Manager\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/elegantly/laravel-cookies-consent.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-cookies-consent)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-cookies-consent/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ElegantEngineeringTech/laravel-cookies-consent/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/ElegantEngineeringTech/laravel-cookies-consent/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/elegantly/laravel-cookies-consent.svg?style=flat-square)](https://packagist.org/packages/elegantly/laravel-cookies-consent)\n\n![laravel-cookies-consent](https://repository-images.githubusercontent.com/828522335/9fa1ca96-5277-4d84-ac2d-85d0fee19c50)\n\nThis package provides a simple yet extremely flexible way to manage cookie consent in your Laravel application.\n\nThe default cookie banner design requires Tailwind CSS and Alpine.js, but you can publish the component and customize it with your own stack.\n\n[Demo here](https://elegantengineering.tech/laravel-cookies-consent)\n\n## Requirements\n\n### Backend\n\n-   Laravel\n\n### Frontend\n\nThe default cookie consent banner included in this package requires:\n\n-   Blade components\n-   [Alpine.js](https://alpinejs.dev/)\n-   [Tailwind CSS](https://tailwindcss.com/)\n-   [js-cookie](https://github.com/js-cookie/js-cookie)\n\n## Installation\n\nYou can install the package via Composer:\n\n```bash\ncomposer require elegantly/laravel-cookies-consent\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"cookies-consent-config\"\n```\n\nThis is the content of the published config file:\n\n```php\nreturn [\n\n    /*\n    |--------------------------------------------------------------------------\n    | URL Configuration\n    |--------------------------------------------------------------------------\n    |\n    | These values determine the package's API route URLs. Both values are\n    | nullable and represent the same concepts as Laravel's routing parameters.\n    |\n    */\n\n    'url' =\u003e [\n        'domain' =\u003e null,\n        'prefix' =\u003e 'cookiesconsent',\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | Consent Cookie Configuration\n    |--------------------------------------------------------------------------\n    |\n    | To keep track of the user's preferences, this package stores\n    | an anonymized cookie. You do not need to register this cookie in the\n    | package's cookie manager as it is done automatically (under \"essentials\").\n    |\n    | The duration parameter represents the cookie's lifetime in minutes.\n    |\n    | The domain parameter, when defined, determines the cookie's activity domain.\n    | For multiple sub-domains, prefix your domain with \".\" (e.g., \".mydomain.com\").\n    |\n    */\n\n    'cookie' =\u003e [\n        'name' =\u003e Str::slug(env('APP_NAME', 'laravel'), '_').'_cookiesconsent',\n        'lifetime' =\u003e 60 * 24 * 365,\n        'domain' =\u003e null,\n    ],\n\n    /*\n    |--------------------------------------------------------------------------\n    | Legal Page Configuration\n    |--------------------------------------------------------------------------\n    |\n    | Most cookie notices display a link to a dedicated page explaining\n    | the extended cookies usage policy. If your application has such a page,\n    | you can add its route name here.\n    |\n    */\n\n    'policy' =\u003e null,\n\n];\n```\n\n## Usage\n\nThis package covers both backend and frontend cookie consent management.\n\nYou can choose to use the package only for backend capabilities or for both.\n\n## Backend Usage\n\nIn the backend, you will register the cookies and a callback associated with each of them.\nThis callback will be a JavaScript script to run when the consent is granted.\n\n### Register Your Cookies\n\nFirst, you should register all the cookies requiring user consent.\n\nTo manage cookies, the package provides a service accessible via the Facade: `Elegantly\\CookiesConsent\\Facades\\CookiesConsent`.\n\nCookie registration should be done in middleware to access the app and request context. This also allows you to choose the routes relying on those cookies.\n\nTo register your cookies, create a new middleware `App\\Http\\Middleware\\RegisterCookiesConsent`.\nIn this middleware, call `CookiesConsent::register` to register groups of cookies.\n\n-   Cookies are always registered in groups.\n-   A cookie is defined by its `name`, `lifetime`, and an optional `description`.\n-   A cookie group can be defined as `required`. Such cookies cannot be rejected by the user, which is useful for essential cookies like the session cookie.\n\nFor example, all cookies related to \"Marketing\" can be registered together:\n\n```php\nnamespace App\\Http\\Middleware;\n\nuse Carbon\\CarbonInterval;\nuse Closure;\nuse Elegantly\\CookiesConsent\\CookieDefinition;\nuse Elegantly\\CookiesConsent\\CookieGroupDefinition;\nuse Elegantly\\CookiesConsent\\Facades\\CookiesConsent;\nuse Illuminate\\Http\\Request;\nuse Symfony\\Component\\HttpFoundation\\Response;\n\nclass RegisterCookiesConsent\n{\n    public function handle(Request $request, Closure $next): Response\n    {\n        // Register cookies related to the Facebook pixel\n        CookiesConsent::register(new CookieGroupDefinition(\n            key: 'marketing',\n            name: __('cookies-consent::cookies.marketing.name'),\n            description: __('cookies-consent::cookies.marketing.description'),\n            items: [\n                new CookieDefinition(\n                    name: '_fbc',\n                    lifetime: CarbonInterval::years(2),\n                    description: __('cookies-consent::cookies._fbc.description')\n                ),\n                new CookieDefinition(\n                    name: '_fbp',\n                    lifetime: CarbonInterval::years(3),\n                    description: __('cookies-consent::cookies._fbp.description')\n                ),\n            ],\n            onAccepted: function () {\n                return \u003c\u003c\u003c'JS'\n                    if (typeof fbq === 'function') {\n                        fbq('consent', 'grant');\n                    }\n                JS;\n            },\n        ));\n\n        return $next($request);\n    }\n}\n```\n\n### Registering Essential Cookies\n\nThe package provides a preset for essential cookies. Essential cookies are those that cannot be removed without compromising the application.\nBy default, Laravel includes 2 essential cookies:\n\n-   `XSRF-TOKEN`\n-   Session cookie\n\nThis package adds a third one:\n\n-   Consents (a cookie to store consents).\n\nYou can automatically register these three essential cookies using:\n\n```php\nuse Elegantly\\CookiesConsent\\Facades\\CookiesConsent;\n\nCookiesConsent::registerEssentials()\n    -\u003eregister(\n        // ... custom cookie definition\n    )\n```\n\n### Registering Cookie Callbacks\n\nUsing the `onAccepted` parameter, you can define the JavaScript code to execute when consent is granted to a specific cookie group.\n\nIn the previous example, we grant consent using the Facebook pixel.\n\n```php\nuse Elegantly\\CookiesConsent\\Facades\\CookiesConsent;\n\nCookiesConsent::register(new CookieGroupDefinition(\n    // ...\n    onAccepted: function () {\n        return \u003c\u003c\u003c'JS'\n            // This JavaScript code will be executed when consent is granted\n            if (typeof fbq === 'function') {\n                fbq('consent', 'grant');\n            }\n        JS;\n    },\n));\n```\n\n## Frontend Usage\n\n### Using the Default Cookie Banner\n\nYou can use the default cookie banner included with this package. It requires js-cookie, Alpine and tailwindcss.\n\n#### js-cookie Requirement\n\nThe default banner implementation requires the [js-cookie](https://github.com/js-cookie/js-cookie) library to parse cookies in the browser.\n\nAdd it to your project using the CDN:\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/js-cookie@3/dist/js.cookie.min.js\"\u003e\u003c/script\u003e\n```\n\nOr see [their documentation](https://github.com/js-cookie/js-cookie) to install it via npm.\n\n#### Alpine.js Requirement\n\nThe default banner implementation requires Alpine.js for reactivity. Ensure it is included in your page.\n\nSimply put the banner component `\u003cx-cookies-consent::banner /\u003e` at the end of your HTML page, and you are ready to go!\n\n```php\n    \u003c!-- ... --\u003e\n    \u003cx-cookies-consent::banner /\u003e\n\u003c/body\u003e\n```\n\n#### Tailwindcss Requirement\n\nThe default banner is based on `elegantly/blade-kit` which is styled with tailwindcss.\nYou must then add the following paths to your tailwind config file:\n\n```js\nexport default {\n    content: [\n        // ...\n        \"./vendor/elegantly/laravel-cookies-consent/resources/views/**/*.blade.php\",\n        \"./vendor/elegantly/blade-kit/resources/views/**/*.blade.php\",\n        \"./vendor/elegantly/blade-kit/src/Ui/**/*.php\",\n    ],\n    // ...\n};\n```\n\n### Customizing the Default Component\n\nYou can customize the default component by publishing the views:\n\n```bash\nphp artisan vendor:publish --tag=\"cookies-consent-views\"\n```\n\n### Using a Custom Component\n\nYou can design your own frontend cookie banner.\n\nTo retrieve all the cookie definitions, simply call:\n\n```php\nuse Elegantly\\CookiesConsent\\Facades\\CookiesConsent;\n\nCookiesConsent::getDefinition();\n```\n\n## Facebook Pixel Cookie Consent\n\nThe Facebook Pixel tracks users and conversions on the client side. [Documentation available here](https://developers.facebook.com/docs/meta-pixel).\n\nThis is the historic way to track conversions. Facebook \u0026 Meta now also provide a way to track your conversions directly from your backend. It is called \"API conversions\" and [the documentation is available here](https://developers.facebook.com/docs/marketing-api/conversions-api/).\n\nThis example will only cover the Facebook Pixel as the \"API conversions\" do not need cookie consent.\n\n### Example\n\nThe Pixel provides a built-in manager for consent. This example relies on this.\n\n#### 1. Revoke consent on load\n\nBefore calling `fbq('init', ...)` and immediately after the Pixel script, **revoke** the consent:\n\n```html\n\u003c!-- Facebook Pixel Code --\u003e\n\u003c!-- prettier-ignore --\u003e\n\u003cscript\u003e\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s)}(window, document,'script',\n  'https://connect.facebook.net/en_US/fbevents.js');\n\n  // Revoke consent before init\n  fbq(\"consent\", \"revoke\"); \n  \n  // Then call your logic as usual\n  fbq('init', '{your-pixel-id-goes-here}');\n  fbq('track', 'PageView');\n\u003c/script\u003e\n\u003c!-- End Facebook Pixel Code --\u003e\n```\n\n#### 2. Grant consent\n\nIn your middleware, register a cookie group and call `fbq('consent', 'grant')` in the `onAccepted` callback.\nEvery call to `fbq` done before the consent will be triggered after `fbq('consent', 'grant')` is called.\n\n```php\nuse Elegantly\\CookiesConsent\\Facades\\CookiesConsent;\nuse Elegantly\\CookiesConsent\\CookieGroupDefinition;\nuse Elegantly\\CookiesConsent\\CookieDefinition;\nuse Carbon\\CarbonInterval;\n\nCookiesConsent::register(new CookieGroupDefinition(\n    key: 'marketing', // customize this value if you want\n    name: __('cookies-consent::cookies.marketing.name'), // customize this value if you want\n    description: __('cookies-consent::cookies.marketing.description'), // customize this value if you want\n    items: [\n        new CookieDefinition(\n            name: '_fbc',\n            lifetime: CarbonInterval::years(2),\n            description: __('cookies-consent::cookies._fbc.description')\n        ),\n        new CookieDefinition(\n            name: '_fbp',\n            lifetime: CarbonInterval::years(3),\n            description: __('cookies-consent::cookies._fbp.description')\n        ),\n    ],\n    onAccepted: function () {\n        return \u003c\u003c\u003c'JS'\n                if(typeof fbq === 'function'){\n                    fbq('consent', 'grant');\n                }\n            JS;\n    },\n));\n```\n\n## References\n\n[Facebook Guide: General Data Protection Regulation](https://developers.facebook.com/docs/meta-pixel/implementation/gdpr)\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n-   [Quentin Gabriele](https://github.com/QuentinGab)\n-   [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felegantengineeringtech%2Flaravel-cookies-consent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felegantengineeringtech%2Flaravel-cookies-consent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felegantengineeringtech%2Flaravel-cookies-consent/lists"}