itemvirtual/cookies-popup

Cookie consent management for Laravel projects

Installs: 265

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 1

Open Issues: 0

pkg:composer/itemvirtual/cookies-popup

1.2.5 2025-02-06 10:38 UTC

This package is auto-updated.

Last update: 2025-10-31 11:46:36 UTC


README

Laravel package

This package provides a popup layer to manage cookies consent

Latest Version on Packagist Total Downloads

Installation

You can install the package via composer:

composer require itemvirtual/cookies-popup

Publish config file (with --force option to update)

php artisan vendor:publish --provider="Itemvirtual\CookiesPopup\CookiesPopupServiceProvider" --tag=config

You need to add package cookies name to the EncryptCookies except array

# file app/Http/Middleware/EncryptCookies.php
protected $except = [
    'analytical_cookies',
    'advertising_cookies',
    'recaptcha_cookies',
    'preferences_cookies',
];

In laravel 11 you should add the cookies name to the encryptCookies method in your application's bootstrap/app.php file

->withMiddleware(function (Middleware $middleware) {
    $middleware->encryptCookies(except: [
        'analytical_cookies',
        'advertising_cookies',
        'recaptcha_cookies',
        'preferences_cookies',
    ]);
})

Generate translations

There is a command to create the required translations in labels section

php artisan cookies-popup:generate-labels

If you prefer to create the translations in a file, these are the necessary indexes

cookies-popup-title
cookies-popup-text
cookies-popup-configure
cookies-popup-accept
accept-required-cookies-label
accept-required-cookies-info
accept-preferences-cookies-label
accept-preferences-cookies-info
accept-analytical-cookies-label
accept-analytical-cookies-info
accept-advertising-cookies-label
accept-advertising-cookies-info
accept-recaptcha-cookies-label
accept-recaptcha-cookies-info
cookies-popup-close
cookies-configure-link

Usage

Print popup

To add cookie popup, put this code after footer in your html layout

{!! \Itemvirtual\CookiesPopup\CookiesPopup::addCookiesPopup() !!}

If you set add-inline-styles to false in your config/cookies-popup.php. You should use getHeadStyles() inside your <head> html tag

{!! \Itemvirtual\CookiesPopup\CookiesPopup::getHeadStyles() !!}

To add a link to open the cookie popup, use the id cookies-popup-show parameter

<a href="#" id="cookies-popup-show" rel="noopener">{{ trans('cookies-popup.cookies-configure-link') }}</a>

Check if cookies are allowed

To hide or show content related with the cookies consent, use the allowed methods

@if(\Itemvirtual\CookiesPopup\CookiesPopup::allowedAnalyticalCookies())
@endif
@if(\Itemvirtual\CookiesPopup\CookiesPopup::allowedAdvertisingCookies())
@endif
@if(\Itemvirtual\CookiesPopup\CookiesPopup::allowedRecaptchaCookies())
@endif
@if(\Itemvirtual\CookiesPopup\CookiesPopup::allowedPreferencesCookies())
@endif

Add Google Analytics scripts

To add the scripts in your layout (gtag.js or analytics.js), use the method you need ()

{!! \Itemvirtual\CookiesPopup\CookiesPopup::getGtagJs() !!}
{!! \Itemvirtual\CookiesPopup\CookiesPopup::getAnalyticsJs() !!}

You need to add an env variable GA_MEASUREMENT_ID it can be a comma separated array

GA_MEASUREMENT_ID="UA-XXXXXXXX-X"
GA_MEASUREMENT_ID="G-XXXXXXXXXX, AW-XXXXXXXXXX"

The analytics.js script can be normal or async script more info

analytics.js
Universal Analytics (gtag.js)

Styling

To styling your pop-up window, you have two options.
The custom_styles or custom_classes config parameters.

With custom_styles you can change some CSS styles. If you need a deeper design change, use custom_classes, which will concatenate your class to the generic one

Documentation links

Google Consent Mode

Cómo configurar el modo de consentimiento en sitios web
Cómo solucionar problemas del modo de consentimiento con Tag Assistant
Tag Manager consent mode support
Google Tag Assistant

Google Consent Mode

Consent Mode Categoría Descripción
ad_storage Publicitarias Es para almacenamiento relacionado con publicidad.
ad_user_data Publicitarias Recoge datos del usuario para personalización de anuncios.
ad_personalization Publicitarias Controla si los anuncios se personalizan.
analytics_storage Analíticas Para el uso de herramientas como Google Analytics.
functionality_storage Funcionales Almacena configuraciones relacionadas con la funcionalidad del sitio.
personalization_storage Funcionales Relacionado con la personalización no publicitaria.
security_storage Necesarias Para almacenamiento relacionado con seguridad y prevención de fraude.

Function not defined

If you don't add the analytics code getGtagJs() or getAnalyticsJs() and you get the message Uncaught ReferenceError: setAnalyticalConsent/setAdvertisingConsent is not defined, you'll need to add this script.

<script>
    if (typeof setAnalyticalConsent === "undefined") {
    function setAnalyticalConsent(value) {}
}
    if (typeof setAdvertisingConsent === "undefined") {
    function setAdvertisingConsent(value) {}
}
</script>

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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