About this project

js-fair-consent is an open-source consent management solution designed for websites that want to respect visitor privacy while still being able to use analytics and marketing tools ethically and legally. The library provides a consent banner (fully customizable via HTML structure and CSS) that asks visitors for permission before loading any analytics scripts. Once consent is given—or denied—the appropriate services are loaded asynchronously and non-blockingly, following modern web performance practices. **Supported analytics providers** - Google Analytics (via gtag.js), with Consent Mode v2 support (`consent-only` or `preload` modes) - Matomo, using its native consent API (`requireConsent`, `rememberConsentGiven`, `forgetConsentGiven`) - Umami, with a no-preload mode to avoid re-renders when consent is granted later - Hotjar, conditionally loaded only when marketing or personalization consent is given **Key features** - Asynchronous, deferred script loading so page rendering is never blocked - Global Privacy Control (GPC) and Do-Not-Track (DNT) signals are automatically respected — when either is detected, the banner hides and all optional tools stay off - Privacy-by-design defaults: no cookies until explicit consent, secure cookie attributes (`SameSite=Lax`, HTTPS-only) - Optional granular consent: visitors can approve or deny individual categories (analytics, marketing, personalization) instead of a single all-or-nothing choice - CSP-safe: supports `nonce` and `integrity` attributes for strict Content Security Policy setups - Built-in i18n: English and Polish translations shipped by default, extensible for other languages - CDN-ready: distributable directly via jsDelivr or GitHub Pages **Configuration** The library is initialized through a simple JavaScript object passed to `ConsentAnalytics.init()`. Users declare which services they want to support, provide their IDs (e.g., GA measurement ID, Umami website ID, Matomo URL and site ID, Hotjar project ID and version), and set options like `debug`, `locale`, and whether to enable `granular` consent. If granular consent is enabled, the stored cookie becomes a JSON object mapping each category to its approval status, and each service checks the relevant category value before loading. **Public API** The library exposes a small set of methods for programmatic access: - `ConsentAnalytics.hasConsent()` — returns a boolean indicating whether any consent has been given - `ConsentAnalytics.readPrefs()` — returns the full consent preference object - `ConsentAnalytics.writePrefs(prefs)` — saves a new preference state - `ConsentAnalytics.revokeConsent()` — clears stored consent and shows the banner again - `ConsentAnalytics.trackPageView()` — manually triggers a pageview (useful for SPAs) The project is MIT-licensed and was created by Rafał Masiarek.