You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

lfs-cookie-consent

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lfs-cookie-consent

Cookie Consent App for LFS/Django

1.0.2
pipPyPI
Maintainers
1

GitHub Repository PyPI Python Versions Django Versions Test

A reusable Django app for GDPR-compliant cookie consent management, featuring integration with Google Tag Manager (GTM) and Google Consent Mode V2. Originally developed for Lightning Fast Shop, it is designed for use in any Django project.

Note: LFS Cookie Consent currently supports only Google Analytics cookies. Additional cookie types will be supported in upcoming releases.

Features

  • Cookie banner with options to accept, decline, or customize consent
  • Granular control over necessary and analytics cookies
  • Google Tag Manager (GTM) integration
  • Google Consent Mode V2 support (currently only for analytics_storage)

Installation

  • Add the app to your Django project:

    • Install lfs_cookie_consent by running: pip install lfs-cookie-consent.
    • Add 'lfs_cookie_consent' to your INSTALLED_APPS in settings.py.
  • Configure your GTM ID in settings.py:

    GTM_ID = "GTM-XXXXXXX"  # Replace with your GTM container ID
    
  • Collect static files:

    python manage.py collectstatic
    

Usage

  • Include the template tags and the provided CSS and JavaScript in your base template:

    {% load static %}
    {% load lfs_cookie_consent_tags %}
    <head>
        <link rel="stylesheet" href="{% static 'lfs_cookie_consent/lfs_cookie_consent.css' %}">
        <script src="{% static 'lfs_cookie_consent/lfs_cookie_consent.js' %}"></script>
        {% gtm_script %}
    </head>
    <body>
        {% gtm_noscript %}
        {% cookie_banner %}
        {% cookie_modal %}
    </body>
    
  • Add a link to open the modal anywhere:

    <a href="#" onclick="window.showCookieSettings(); return false;">Open cookie settings</a>
    

How it works

  • On the first visit, a cookie banner is displayed.
  • Users can accept all, decline all, or customize their preferences (currently only analytics cookies).
  • Consent is stored in a cookie and applied via Google Consent Mode V2 (analytics_storage).
  • GTM and Google Analytics tags will only fire if consent is granted.

Google Tag Manager Setup

  • Create a GTM container and use the ID in your Django settings.
  • Add a Google Analytics 4 tag in GTM.
  • Add a Page View trigger to your tag
  • Don't forget to publish your changes
  • Consent Mode is automatically handled by this app (no extra GTM configuration needed).
  • Test with Tag Assistant or GTM Preview to ensure tags fire only after consent.

Test View and Temporary URL Inclusion

This package includes a simple test view that lets you quickly try out the cookie banner and modal. The view renders a sample page with all relevant components.

How to use the test view:

  • Temporarily add the test view URL to your project: Open your main urls.py (e.g., in your main project or test project) and add:

    from lfs_cookie_consent.views import test_cookie_banner
    from django.urls import path
    
    urlpatterns = [
        # ... your other URLs ...
        path("", test_cookie_banner, name="test_cookie_banner"),
    ]
    
  • Open your project's start page in the browser. You should see the cookie banner and modal as intended.

  • Open your browser's developer tools, go to the cookies section, and observe how cookies are set or deleted based on your consent choices.

  • Remove the temporary URL once you have finished testing the functionality.

License

MIT License

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.