serge-pro-dev

consent

by serge-pro-dev
payload-plugins
Other
v4
^4.0.0-beta.12
MIT

Overview

Summary generated from the README

Adds a Consent Settings global, a cookie banner with a settings dialog, and script blocking until the visitor agrees. It supports Off, Notice and Opt-in modes, plus Google Consent Mode v2. It also adds a Personal data consent field type to the form builder, validated on the server. Requires Payload 4 and Next.js, and must be listed after the form builder plugin.

Features

  • Consent Settings global with Off, Notice and Opt-in modes
  • Banner with settings dialog, compact pill mode and optional reject and manage buttons
  • Holds scripts inert by category until analytics or marketing consent is given
  • Google Consent Mode v2 defaults and a browser API with events and dataLayer updates
  • Personal data consent form field with server-side check and stored consent record
  • Themable through CSS variables, with English and Russian admin labels

Use cases

  • Block analytics and marketing scripts until visitors accept cookies on a Next.js site
  • Collect and record personal data consent in form builder submissions
  • Run a cookie notice or opt-in banner that follows the site theme

From the author

Cookie banner and consent control for Payload CMS 4 and Next.js: Consent Settings global, banner, Google Consent Mode v2, personal data consent in forms.

Installation

npm i @serge-pro-dev/payload-plugin-consent

0

Stars

0

Forks

0

Open Issues

Yesterday

Last Updated

441

Weekly Downloads

v0.1.0-beta.2

npm Version

123.6 KB

Unpacked Size

1

Dependencies

Repository stars

Trends appear once the first and last snapshots are 7 days apart. Collecting since Nov 29 — 7 daily snapshots so far.

Shared by 6 plugins in serge-pro-dev/payload-plugins.

npm weekly downloads

Trends appear after 7 days of data. Collecting since Dec 4 — 2 daily snapshots so far.

Each point is npm's rolling 7-day download count.

Health Score

76/100Excellent

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1479182369-consent.svg)](https://payloaddirectory.dev/plugins/1479182369-consent)
HTML
<a href="https://payloaddirectory.dev/plugins/1479182369-consent"><img src="https://payloaddirectory.dev/badge/1479182369-consent.svg" alt="Payload Directory"></a>
Updated Dec 4, 2026Created Dec 1, 2026Published Dec 4, 2026

README

Русская версия

Cookie banner and consent control for a Next.js + Payload 4 site. It adds the Consent Settings global (menu group Analytics), a banner with a settings dialog, and blocks analytics scripts until the visitor agrees. Built for RU/BY sites first (Notice mode); Opt-in mode and Google Consent Mode v2 cover EU/US.

The default texts and the modes are mechanics, not legal advice. Have a lawyer review the texts and your policy.

Consent Settings

Install

pnpm add @serge-pro-dev/payload-plugin-consent
// payload.config.ts: after formBuilderPlugin
import { consentPlugin } from '@serge-pro-dev/payload-plugin-consent'

plugins: [formBuilderPlugin(/* ... */), consentPlugin({ defaultLanguage: 'en' })]
// src/app/(frontend)/layout.tsx
import config from '@payload-config'
import { Consent, ConsentHead } from '@serge-pro-dev/payload-plugin-consent/rsc'

<head>
  {/* Google Consent Mode defaults, before any Google tag */}
  <ConsentHead config={config} />
</head>
<body>
  ...
  {/* banner and runtime */}
  <Consent config={config} />
</body>

Then run pnpm payload generate:importmap and pnpm payload generate:types, open Analytics → Consent Settings, choose a mode and save. Default is Off: nothing changes until you switch it on.

Modes

ModeBefore a choiceBanner
Offeverything runsnone
Noticeeverything runs, Reject stops itshown until the visitor chooses
Opt-inoptional scripts do not runshown until the visitor chooses

Categories: necessary (always on), analytics, marketing. Raise Consent version to ask everybody again.

Banner options

  • Compact mode: a minimal one-line pill ("We use cookies for analytics and ads. [Reject] [Manage] [Accept]") instead of the full card. It has its own texts in the Texts section (names start with "Compact banner"): text with a {link} marker (replaced by the policy link), link text and the accept button. Needs the policy URL for the link; without it the link text is shown as plain text.
  • Show reject button / Show manage button: hide the buttons you do not want. Without Manage, visitors can still open the settings from <ConsentSettingsLink> in the footer. If both are hidden only Accept is left, which is not suitable for Opt-in on EU/US sites.

Scripts of the analytics plugin (Google tag, custom JavaScript, UTM writer) have a category and are rendered inert (type="text/plain" data-consent-category="analytics") while the mode is Notice or Opt-in; the browser runtime starts them once their category is allowed. Any script of your own can use the same markup:

<script type="text/plain" data-consent-category="marketing" data-src="https://example.com/pixel.js"></script>
  • <noscript> pixels cannot wait for consent, so they should not be output while the mode is Notice or Opt-in.
  • If the visitor withdraws analytics consent, the utm_first_touch and utm_last_touch cookies are removed. Cookies of third-party services cannot be removed from the page; scripts that already ran keep running until reload.

Browser API

window.__consent.get()        // { analytics, marketing, decided }
window.__consent.set({ analytics: true })
window.__consent.acceptAll() / rejectAll() / open()
window.__consent.subscribe((state) => ...)
// events: 'consent:change' (detail = state), 'consent:open'
// dataLayer: { event: 'consent_update', consent_analytics, consent_marketing }

<ConsentSettingsLink>Cookie settings</ConsentSettingsLink> (client component, @serge-pro-dev/payload-plugin-consent/client) reopens the dialog from the footer.

The plugin adds a field type to the form builder: Personal data consent (a required checkbox with a link). A form has it only if an editor adds it, so it is off by default.

  1. Register the component in the project's field map (one line, like any custom form-builder field):
    import { PersonalDataConsentField } from '@serge-pro-dev/payload-plugin-consent/client'
    
    export const fields = { /* ... */ personalDataConsent: PersonalDataConsentField }
    
  2. The plugin must be listed after formBuilderPlugin.
  3. In a form: Add field → Personal data consent; set the text, link text and URL. Defaults come from consentPlugin({ personalData: { text, linkLabel, linkUrl, errorMessage } }).

The visitor cannot send the form without ticking the box. The server checks it again and refuses a submission without consent (message from the field). Every accepted submission gets a read-only Personal data consent record, built on the server from the form itself: consent given, server time, a short hash of the wording (text + link) and a copy of the text and link. Whatever the browser sends for that record is discarded. No IP address is stored.

The plugin provides the mechanics, not the legal documents: the consent text and the page it links to must be prepared for your jurisdiction.

consent = encodeURIComponent(JSON) of { v: 1, analytics, marketing, ts, version }, SameSite=Lax, lifetime from the settings. Readers must treat it as untrusted input: use readConsentFromHeader() from @serge-pro-dev/payload-plugins-shared/consent.

Styling

Plain CSS, imported by the banner itself; colors come from --cc-* variables that fall back to shadcn-style tokens (--popover, --foreground, --primary, ...), so the banner follows the site theme. Tokens must be complete colors (oklch(), hsl(), hex). Override any --cc-* variable to restyle it.

The visitor's choice is read in the browser after hydration, so pages stay statically rendered; the banner appears just after load.

Admin language

Labels and descriptions in the admin are available in English and Russian. Enable both languages in the project: i18n: { supportedLanguages: { en, ru } } (from @payloadcms/translations/languages). The texts shown to visitors are separate (the texts of the global).

Database

New global consent and the personal_data_consent_* columns of form submissions. Development: schema push. Production: pnpm payload migrate:create and migrate.

Not yet

A site-wide consent log, localized banner texts, external CMPs (Klaro, vanilla-cookieconsent) through the browser API.

MIT

Similar plugins