定制 radosavleovac/laravel-consent-mode 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

radosavleovac/laravel-consent-mode

Composer 安装命令:

composer require radosavleovac/laravel-consent-mode

包简介

GDPR Compliant Google Consent Mode V2 for Laravel

README 文档

README

GDPR Compliant Google Consent Mode V2 for Laravel

This package provides an easy way to implement Google Consent Mode V2 in Laravel applications while complying with GDPR regulations. It supports TailwindCSS for styling and offers customizable cookie consent management.

🚀 Features

✅ Google Consent Mode V2 compliant ✅ Supports TailwindCSS for styling ✅ Easy integration with Laravel ✅ Customizable cookie categories ✅ Consent-based loading of Google Tag Manager (GTM) ✅ Multi-language support ✅ Publishable assets (JS, CSS, Views, and Config)

📌 Installation

1️⃣ Install the package via Composer

Run the following command in your Laravel project:

composer require radosavleovac/laravel-consent-mode

2️⃣ Publish the configuration file

After installation, publish the configuration file using:

php artisan vendor:publish --tag=consent-config

This will create a configuration file at:

config/consent.php

3️⃣ Publish assets (JS & CSS)

To publish the required JavaScript and CSS files, run:

php artisan vendor:publish --tag=consent-assets

This will place the assets in the public/js and public/css folders.

4️⃣ Publish translations (optional)

To publish language files for customization:

php artisan vendor:publish --tag=cookie-consent-translations

5️⃣ Publish views (optional)

If you want to override the default views:

php artisan vendor:publish --tag=cookie-consent-views

This will copy the views to:

resources/views/vendor/cookie-consent/

📖 Configuration

Edit the config/consent.php file to configure the cookie categories and default settings:

return [
    'gtm_id' => env('GTM_ID', 'GTM-XXXXXXX'),

    'cookie_name' => env('COOKIE_CONSENT_NAME', 'cookie_consent'),

    'cookie_expiration_days' => env('COOKIE_EXPIRATION_DAYS', 365),

    'categories' => [
            'necessary' => [
            'label' => 'necessary', // This key is used in the cookie settings modal
            'description' => 'necessary_description', // This key is used in the cookie settings modal
            'required' => true, // This key is used in the cookie settings modal
            'gtag_key' => 'security_storage', // This key is used in the Google Tag Manager
            'cookies' => ['XSRF-TOKEN', 'laravel_session'], // Cookies that are set when the category is accepted
        ]
    ],

    'default_consent' => [
        'ad_storage' => 'denied',
        'analytics_storage' => 'denied',
        'security_storage' => 'granted',
    ]
];

🚀 Usage

1️⃣ Add the consent banner

Include the following Blade directive in your main layout, near or in footer (e.g., resources/views/layouts/app.blade.php):

@include('cookie-consent::cookie-banner')

2️⃣ Add the Google Tag after head opening

@include('cookie-consent::gtm')

2️⃣ Add the noscript Tag after body opening

@include('cookie-consent::gtm-noscript')

🎯 Example: Handling Consent in JavaScript

When the user accepts all cookies, the following function is executed:

function acceptAllCookies() {
  gtag("consent", "update", {
    ad_storage: "granted",
    analytics_storage: "granted",
    security_storage: "granted",
  });

  document.cookie =
    "cookie_consent=accepted; path=/; max-age=" + 365 * 24 * 60 * 60;
  document.getElementById("cookie-banner").style.display = "none";
}

🔧 Customization

  • Edit views in resources/views/vendor/cookie-consent/
  • Modify translations in resources/lang/vendor/cookie-consent/
  • Adjust CSS styles in public/css/cookie.css

To enable visitors to change settings you can place a button in your code

<!-- Open modal -->
<button onclick="openCookieModal()" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
    Settings
</button>

To display map after user gave permission you can use

@if(isset($_COOKIE['maps_cookies']) && $_COOKIE['maps_cookies'] === 'granted')
    <iframe
        src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d5249.98288205065!2d2.291906376485278!3d48.85837360070764!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x47e66e2964e34e2d%3A0x8ddca9ee380ef7e0!2sEiffelturm!5e0!3m2!1sde!2sat!4v1738785000996!5m2!1sde!2sat"
        width="600" height="450" style="border:0;" allowfullscreen="" loading="lazy"
        referrerpolicy="no-referrer-when-downgrade"></iframe>
    @else
    <div class="bg-gray-200 p-4 text-center">
        <p>{{ __('cookie-consent::messages.enable_map') }}</p>
        <button onclick="openCookieModal()" class="bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600">
            {{ __('cookie-consent::messages.cookie_settings') }}
        </button>
    </div>
@endif

📄 License

This package is licensed under the MIT licence.

To do - tutorial how to setup Google Tag manager

radosavleovac/laravel-consent-mode 适用场景与选型建议

radosavleovac/laravel-consent-mode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 radosavleovac/laravel-consent-mode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 radosavleovac/laravel-consent-mode 我们能提供哪些服务?
定制开发 / 二次开发

基于 radosavleovac/laravel-consent-mode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 55
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-05