Skip to documentation content

Consent cookie schema

Field-by-field reference for the CookieBeam consent cookie, covering both the V1 baseline and V2 extended schema.

Last updated:

CookieBeam persists each visitor's consent state in a first-party cookie (default name: cb_cookie). The cookie value is URL-encoded JSON. Two schema versions exist: V1 (the original) and V2 (the current default). Every new record is V2 with schemaVersion: 2. Records without a schemaVersion field are V1 and are auto-upgraded to V2 when parsed.

You normally don't need to read the cookie directly. Use the Browser API instead. This page is the field-level reference for when you're debugging, building a server-side integration, or writing a custom reader.

For the conceptual overview of what consent records mean and how they're used, see The consent record.

V1 fields

These fields form the baseline. They're present on every consent record, V1 or V2.

FieldTypeRequiredDescription
categoriesstring[]YesAccepted category IDs. At most 50 entries, each 1–100 characters.
revisionnumberYesConfiguration revision the choice was made against. Range: 0–1,000,000. A mismatch with the banner's current revision triggers re-consent.
dataunknownYesCustom data stored via setCookieData(). Default: null.
consentIdstringYesWhen the banner's consent ID setting is on and the visitor's choice qualifies, a per-consent identifier (cb1_ prefix + 32 hex characters). When the setting is off (the default), absent, or the choice does not qualify, the literal string 'anonymous'. A changed choice creates a new identifier; only repeating the same choice keeps the existing one. A returning visitor's existing cookie may still carry a per-visitor UUID from core versions before 0.3.87, and banners pinned to an older core version continue to write one, until the banner is rebuilt on 0.3.87 or later.
consentTimestampstring (ISO 8601)YesRewritten on every save. Anchors the expiry check. See Timestamp semantics.
lastConsentTimestampstring (ISO 8601)YesWhen the most recent save happened. Falls back to consentTimestamp on older records that predate the field.
languageCodestringYesBanner language when consent was given. 1–20 characters.
servicesRecord<string, string[]>NoMap of category IDs to accepted service IDs within each category.

V2 fields

V2 extends V1 with these fields. All are backward-compatible: a V1 reader that ignores them still gets a working consent record.

FieldTypeRequiredDescription
schemaVersion2YesMust be 2. Records without it are treated as V1.
acceptType'all' | 'custom' | 'necessary'YesHow the visitor chose: accepted everything, made a custom selection, or accepted only necessary.
jurisdictionsstring[]NoJurisdiction tags recorded with this consent.
gpcRespectedbooleanNotrue when a Global Privacy Control signal shaped this record. Stays as a historical marker even if the visitor later changes their choice through the banner.
usOptOutChoicesUsOptOutChoicesNoDurable US opt-out choices. See below.
isPendingbooleanNoMarks a pending record. For opt-in defaults (collectedVia: 'default_optin'), validConsent() returns false. For deferred machine defaults (deferMachineDefaults on, with isDefaultState: true and collectedVia of default_optout, default_notice, or hidden_autogrant), validConsent() returns true: the granted categories are in effect, but the GTM template skips the snapshot.
isDefaultStatebooleanNoMachine-written record (opt-out default, notice default, hidden auto-grant). Does not invalidate consent (scripts for granted categories still run), but the banner keeps auto-showing until the visitor makes an explicit choice.
collectedViaCollectionMethodNoHow the record was collected.
firstConsentTimestampstring (ISO 8601)NoWhen the visitor's first-ever consent was recorded. Absent means consentTimestamp is the first save. See Timestamp semantics.
consentModeConsentModeSnapshotNoConsent Mode state snapshot for the GTM template. Written only on explicit choices.
purposeConsentobjectNoPurpose-level consent evidence. Present only on banners with the purpose consent feature enabled.

UsOptOutChoices

Persisted US opt-out panel choices. Each field is a boolean where true means the visitor has opted out of that right.

FieldMeaning
saleSharingCCPA/CPRA sale and sharing of personal information.
targetedAdvertisingState-law targeted advertising opt-out (Colorado, Virginia, Connecticut, and others).
sensitiveDataCPRA limit-use-of-sensitive-personal-information.
profilingProfiling opt-out (Colorado, Virginia, Connecticut).

The polarity is inverted from the UI. In the opt-out panel, a checked toggle means the right is allowed. In the cookie, true means it's blocked. The conversion happens once, in the confirm handler. A field left undefined means the visitor never made a deliberate choice for that right; treat it as fail-safe (keep blocking), not as consent.

collectedVia values

ValueMeaning
bannerThe visitor clicked a button on the consent banner.
preferencesThe visitor saved choices in the preferences modal.
apiConsent was set through the JavaScript API (acceptCategory()).
default_optinMachine-written default in opt-in mode (always isPending: true).
default_optoutMachine-written default in opt-out mode.
default_noticeMachine-written default in notice mode.
hidden_autograntMachine-written auto-grant from a regional rule that hides the banner. Grants defaultCategories (or read-only categories when no defaults are configured).

Machine-written records (default_* and hidden_autogrant) always have isDefaultState: true. Distinguishing them by collectedVia tells you which consent model produced the default.

Written on explicit choices. When Consent Mode is enabled and the consent cookie is the storage backend, the runtime also refreshes the snapshot on page load for returning visitors whose stored choice is still valid. The GTM Consent Mode template reads this snapshot at Consent Initialization, before the full CookieBeam runtime loads.

FieldTypeDescription
vnumberContract version of the snapshot format. Currently 1. The GTM template skips snapshots whose v is present and not 1. Absent on records written by core 0.3.62 and earlier (treated as version 1).
revisionnumberThe config.revision the record was written under.
expiresAtnumberEpoch milliseconds when the record expires. Integer because the GTM sandbox has no Date parser.
stateConsentModeV2StateThe 7 Consent Mode v2 keys, each 'granted' or 'denied'.

The 7 keys: ad_storage, ad_user_data, ad_personalization, analytics_storage, functionality_storage, personalization_storage, security_storage.

The snapshot is written on explicit visitor choices. When Consent Mode is enabled and the banner uses cookie storage (not localStorage only), the runtime refreshes the snapshot silently on page load for returning visitors with a valid stored choice (same revision, same timestamps, no new event). The refresh is skipped when the record has no existing snapshot and the banner's consent lifetimes differ across regions, or when another tab has changed the cookie since this page loaded. It's never written on a machine-written default. The GTM template skips records that lack it. See Consent Mode v2 for what each key controls.

Timestamp semantics

Three timestamp fields serve different purposes:

FieldUpdated whenPurpose
consentTimestampEvery consent save (not setCookieData)Expiry anchor. The runtime compares it against expiresAfterDays (or the regional consentExpiryDays) to decide whether the record is stale. setCookieData() does not update this field.
lastConsentTimestampEvery save (including setCookieData)Calendar time of the latest write. Falls back to consentTimestamp on records that predate the field.
firstConsentTimestampFirst save onlyWhen the visitor's first consent was recorded. Absent on the very first save (where consentTimestamp already is the first time). Carried forward on every later save.

In the lifecycle callbacks, these surface as:

  • cookie.firstConsentAt: resolved from firstConsentTimestamp ?? consentTimestamp
  • cookie.lastConsentAt: resolved from lastConsentTimestamp ?? consentTimestamp

The expiry check always uses consentTimestamp, not firstConsentTimestamp. A visitor who consented a year ago but changed their choice yesterday has a fresh expiry window from yesterday's save.

Validity rules

The runtime considers a stored record valid when all of these hold:

  1. categories is a non-empty array of strings.
  2. consentId is a non-empty string.
  3. consentTimestamp is a valid ISO 8601 timestamp.
  4. The timestamp is not in the future.
  5. The record hasn't expired: consentTimestamp is within the configured expiresAfterDays (or the regional consentExpiryDays override).
  6. revision matches the current configuration revision.
  7. isPending is not true for opt-in defaults. When deferMachineDefaults is configured, a pending record with isDefaultState: true and a granting collectedVia (default_optout, default_notice, hidden_autogrant) is valid: validConsent() returns true and the granted categories are in effect.
  8. Every readOnly category in the current configuration is present in categories. A record missing a read-only category is treated as stale.

A record that fails any check is treated as stale. The banner auto-shows (when autoShow is on), and the visitor's current categories revert to the pre-consent state for their consent model.

Through the API

// Full parsed record
var record = CookieBeam.getCookie();

// Individual fields
var cats = CookieBeam.getCookie('categories');
// getCookie('consentId') returns the cb1_ identifier when one
// exists and the banner's consent ID setting still allows it;
// otherwise undefined. The raw cookie field holds 'anonymous'
// when there is no identifier.
var type = CookieBeam.getCookie('acceptType');

// Validity check
var isValid = CookieBeam.validConsent();

See Browser API for the full method reference.

If you need to read consent server-side or outside the runtime:

// Browser
var raw = document.cookie
  .split('; ')
  .find(function (c) { return c.startsWith('cb_cookie='); });
if (raw) {
  var decoded = decodeURIComponent(raw.split('=').slice(1).join('='));
  var record = JSON.parse(decoded);
  console.log(record.categories);
}
// Node.js (Express)
var cookieValue = req.cookies['cb_cookie'];
if (cookieValue) {
  var record = JSON.parse(decodeURIComponent(cookieValue));
}

The cookie name defaults to cb_cookie but is configurable via cookie.name in the configuration. Use the same name your banner is published with.

localStorage fallback

When cookie.useLocalStorage is true, the consent record lives in localStorage under the key cb_consent_{bannerId} instead of a browser cookie. When storage.enabled is true, the runtime writes to both the cookie and localStorage (cookie first, localStorage as a fallback that the runtime reads if the cookie is missing on a later visit). The localStorage value is the same JSON (not URL-encoded). The API methods work the same regardless of the storage backend, but server-side code can't read localStorage.

The cookie itself (not its JSON payload) is written with these HTTP attributes, all configurable via the cookie configuration section:

AttributeDefaultNotes
Max-Age182 days (~6 months)Set by expiresAfterDays. Regional rules can override via consentExpiryDays.
Path/Set by path.
Domain(current host)Empty by default (host-only cookie). Set to example.com for subdomain sharing. See Cross-domain consent.
SecuretrueSet by secure.
SameSiteLaxSet by sameSite.

Example record

A V2 record after a visitor accepts analytics and preferences through the preferences modal:

{
  "schemaVersion": 2,
  "categories": ["necessary", "analytics", "preferences"],
  "revision": 3,
  "data": null,
  "consentId": "cb1_8a3f29b7c6d1e0f45a2b7c9d3e6f1a2b",
  "consentTimestamp": "2026-11-12T14:30:00.000Z",
  "lastConsentTimestamp": "2026-11-12T14:30:00.000Z",
  "languageCode": "en",
  "acceptType": "custom",
  "collectedVia": "preferences",
  "consentMode": {
    "v": 1,
    "revision": 3,
    "expiresAt": 1763020200000,
    "state": {
      "ad_storage": "denied",
      "ad_user_data": "denied",
      "ad_personalization": "denied",
      "analytics_storage": "granted",
      "functionality_storage": "granted",
      "personalization_storage": "granted",
      "security_storage": "granted"
    }
  }
}

The consentMode.state snapshot lets the GTM template restore these signals at Consent Initialization on the next page load, before the full runtime has initialized.

A machine-written opt-out default looks different:

{
  "schemaVersion": 2,
  "categories": ["necessary", "analytics", "preferences", "marketing"],
  "revision": 3,
  "data": null,
  "consentId": "anonymous",
  "consentTimestamp": "2026-11-12T14:30:00.000Z",
  "lastConsentTimestamp": "2026-11-12T14:30:00.000Z",
  "languageCode": "en",
  "acceptType": "all",
  "isDefaultState": true,
  "collectedVia": "default_optout"
}

No consentMode snapshot (machine-written defaults don't get one), all categories granted, and isDefaultState: true tells the banner to keep auto-showing until the visitor makes an explicit choice.