Download the PHP package leobsst/laravel-cookie-consent without Composer

On this page you can find all versions of the php package leobsst/laravel-cookie-consent. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-cookie-consent

Laravel Logo

Version Laravel PHP

Downloads License

Laravel Cookie Consent

A lightweight Laravel package for handling cookie consent with Google Tag Manager and Google AdSense integration. No Livewire required — the banner is a pure Blade component with vanilla JavaScript, making it usable in any Laravel project.

Features

Requirements

Installation

Step 1: Install the Package

Step 2: Publish Assets (Required)

This publishes public/vendor/cookie-consent/cookie-consent.js, the JavaScript file that handles cookie writing and Google Tag Manager consent updates.

Step 3: Publish Config File (Recommended)

This creates config/cookie-consent.php where you can customize:

Alternative: Use the Install Command

This will publish the config file and assets, then ask you to star the repository on GitHub.

Configuration

Tailwind CSS Integration

[!IMPORTANT]

Add the following to your app.css so Tailwind processes the banner's utility classes:

Usage

Basic Usage

  1. Add your Google Tag Manager ID to .env:

  2. Add the scripts directive and the banner component to your layout:

The banner automatically appears for users who have not yet set their cookie preferences.

Complete Example

How It Works

  1. @cookieConsentScripts (or <x-cookie-consent::scripts />) injects the GTM snippet with gtag('consent', 'default', ...). The initial state depends on the cookie_consent cookie already being set (e.g. returning visitors).

  2. <x-cookie-consent::cookie-banner /> renders the banner only when GTM is configured and the user has not yet made a choice. It also injects a small window.CookieConsent config object (cookie name, duration, SameSite, secure flag) and the banner's JavaScript inline.

  3. When the user clicks Accept or Refuse, the bundled cookie-consent.js script:
    • Writes the cookie_consent cookie directly in the browser (non-HttpOnly so it remains readable by JS on subsequent page loads)
    • Calls gtag('consent', 'update', ...) to update consent in the current GTM session
    • Hides the banner instantly without a page reload

Checking User Consent

The HandleCookieConsent middleware shares $cookieConsentStatus with all views:

You can also read the cookie directly via the $_COOKIE superglobal:

[!NOTE] Do not use request()->cookie('cookie_consent') — Laravel's EncryptCookies middleware will try to decrypt the value and return null, because the cookie is written as plain text by the browser-side JavaScript. Reading it via $_COOKIE bypasses decryption and returns the raw value correctly.

Customizing the Banner

Publish the views to modify them:

This copies the views to resources/views/vendor/cookie-consent/. You can also point to a completely custom view via the config:

Your custom view receives these variables from CookieBanner:

Variable Type Description
$loadScript bool Whether GTM is configured
$learnMoreLink ?string Resolved URL for the "Learn more" link
$accentColor ?string CSS color value
$cookieDuration int Cookie lifetime in seconds
$sameSite string SameSite attribute value

Translations

The banner uses the locale set in config('app.locale'). Available languages: English (en), French (fr), Italian (it), Spanish (es), German (de).

To publish and customize translation files:

Files are published to resources/lang/vendor/cookie-consent/.

Translation keys:

Key Default (EN)
cookie-consent::translations.description Our website uses cookies...
cookie-consent::translations.question Do you accept the use of these cookies?
cookie-consent::translations.learn_more Learn more
cookie-consent::translations.accept Accept
cookie-consent::translations.refuse Deny

Google Tag Manager Consent Mode

The package implements Google Consent Mode v2.

Default state (user has not yet decided):

When user accepts:

When user refuses:

Note: On a first-time accept, if gtag is not yet loaded (GTM script hasn't initialized), the page will reload once so that GTM boots with the correct consent state.

Google AdSense Integration

If you use Google AdSense without Google Tag Manager, the package can inject the AdSense snippet with Consent Mode v2 directly.

Setup

  1. Set your publisher ID in .env:

  2. The package automatically:
    • Adds <meta name="google-adsense-account"> to prevent duplicate consent banners
    • Injects the AdSense gtag.js snippet with Consent Mode v2 defaults based on the user's current consent status
    • Serves an auto-generated /ads.txt file at your site's root

Note: If GOOGLE_TAG_MANAGER_ID is also set, the AdSense snippet is skipped — GTM handles consent signaling instead.

Auto-generated ads.txt

When GOOGLE_ADSENSE_CLIENT_ID is configured and ENABLE_AUTOGENERATED_ADS_FILE is true (default), the package registers a /ads.txt route that returns a plain-text file containing your publisher ID alongside common authorized ad network declarations.

To disable this endpoint:

PostHog Integration

The package can inject the PostHog analytics snippet directly when POSTHOG_PROJECT_TOKEN is set.

Setup

  1. Set your project token in .env:

  2. Optionally override the host (defaults to EU Cloud):

  3. PostHog is initialized unconditionally on every page load. It uses cookie persistence so it respects the user's cookie consent state.

Note: PostHog can be combined with GTM or AdSense — all three integrations are independent and stack freely.

Advanced Configuration

Environment Variables

Resetting a User's Consent

FAQ

Does this package require Livewire?

No. As of v2, Livewire is not required. The banner is a Blade component and all consent logic runs in vanilla JavaScript.

Can I use this without Google Tag Manager?

Yes. Simply don't set GOOGLE_TAG_MANAGER_ID. If you set GOOGLE_ADSENSE_CLIENT_ID instead, the package will inject AdSense with Consent Mode v2. If neither is configured, the banner will not appear, but the cookie_consent cookie will still be available for your own scripts.

How do I style the banner to match my design?

  1. Use ACCENT_COLOR in the config to change the button/link color.
  2. Publish the views and edit the Tailwind classes.
  3. Create a completely custom view and point CONSENT_BANNER_VIEW to it.

Testing

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you find a security vulnerability, please email [email protected] instead of using the issue tracker.

Credits

License

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


All versions of laravel-cookie-consent with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.9|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package leobsst/laravel-cookie-consent contains the following files

Loading the files please wait ...