Skip to main content
Ask your AI

CookieConsent

Passes the visitor's CookieConsent (v3, by orestbida) decision to walkerOS, so destinations gated with config.consent send only what the visitor agreed to. The source reads only a decision the visitor made: accepted categories are granted, rejected ones are denied.

Installation​

npm install @walkeros/web-source-cmp-cookieconsent
import { startFlow } from '@walkeros/collector';
import { sourceCookieConsent } from '@walkeros/web-source-cmp-cookieconsent';

await startFlow({
sources: {
consent: {
code: sourceCookieConsent,
},
},
});

Configuration​

This source uses the standard source config wrapper (consent, data, env, id, ...). For the shared fields see source configuration. Package-specific fields live under config.settings and are listed below.

Settings​

PropertyTypeDescriptionMore
categoryMapRecord<string, string>Map CookieConsent's category names (keys) to walkerOS consent groups (values). Categories without an entry keep their own name. Default: { ads: 'marketing' }.

Mapping​

This package does not define custom rule-level settings. For the standard rule fields (consent, condition, data, batch, name, policy) see mapping.

Examples

Accept all

The visitor clicks Accept all. CookieConsent fires cc:onConsent and the source grants necessary, analytics and marketing.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "all",
    "acceptedCategories": [
      "necessary",
      "analytics",
      "ads"
    ],
    "rejectedCategories": []
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "analytics": true,
  "marketing": true
})

Category map override

A custom categoryMap reports the analytics category as a statistics group.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "custom",
    "acceptedCategories": [
      "necessary",
      "analytics"
    ],
    "rejectedCategories": [
      "ads"
    ]
  }
}
Mapping
{
  "settings": {
    "categoryMap": {
      "analytics": "statistics"
    }
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "statistics": true,
  "marketing": false
})

Custom category

A site category without a map entry, here functionality, is reported under its own name.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "custom",
    "acceptedCategories": [
      "necessary",
      "functionality"
    ],
    "rejectedCategories": [
      "analytics",
      "ads"
    ]
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "functionality": true,
  "analytics": false,
  "marketing": false
})

First visit

The banner is open and CookieConsent holds no valid choice, so the source sends nothing until the visitor decides.

Event
{
  "valid": false,
  "preferences": {
    "acceptedCategories": [],
    "rejectedCategories": []
  }
}
Out
// no output

Opt-out defaults

In opt-out mode CookieConsent enables default categories before a choice. That is no decision, so the source sends nothing until the visitor decides.

Event
{
  "valid": false,
  "preferences": {
    "acceptedCategories": [
      "necessary",
      "analytics",
      "ads"
    ],
    "rejectedCategories": []
  }
}
Out
// no output

Preferences change

The visitor withdraws analytics and ads in the preferences. CookieConsent fires cc:onChange and the source sends both groups as false.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "necessary",
    "acceptedCategories": [
      "necessary"
    ],
    "rejectedCategories": [
      "analytics",
      "ads"
    ]
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "analytics": false,
  "marketing": false
})

Reject all

The visitor clicks Reject all. Only the read-only necessary category stays accepted, and the source sends analytics and marketing as false.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "necessary",
    "acceptedCategories": [
      "necessary"
    ],
    "rejectedCategories": [
      "analytics",
      "ads"
    ]
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "analytics": false,
  "marketing": false
})

Returning visitor

CookieConsent restored a valid choice before the source started, so the static read at init publishes it.

Event
{
  "valid": true,
  "preferences": {
    "acceptType": "custom",
    "acceptedCategories": [
      "necessary",
      "analytics"
    ],
    "rejectedCategories": [
      "ads"
    ]
  }
}
Out
elb("walker consent", {
  "necessary": true,
  "analytics": true,
  "marketing": false
})

How it works​

  1. Stored decision: If window.CookieConsent already holds a valid decision when the source starts, the source publishes it at once.

  2. Decision events: The source listens for CookieConsent's cc:onConsent and cc:onChange window events. CookieConsent fires cc:onConsent after a first decision and on every page load with a stored one, and cc:onChange when the visitor changes it. Each time, the source reads the decision from CookieConsent.getUserPreferences().

  3. Mapping: Accepted categories are published as true, rejected ones as false, through categoryMap and elb('walker consent', state).

  4. One command per decision: A stored decision is reported both by the read at start and by cc:onConsent on page load. The source sends a state only when it differs from the last one it sent.

The ESM build​

The script build defines window.CookieConsent. If you import vanilla-cookieconsent as a module, assign it before CookieConsent.run: window.CookieConsent = CookieConsent;. Without it the source cannot read the decision and logs an ERROR.

import * as CookieConsent from 'vanilla-cookieconsent';

window.CookieConsent = CookieConsent;

Default category mapping​

The categories are the ones your CookieConsent.run configuration names. For the categories the CookieConsent docs use:

CookieConsent categorywalkerOS consent group
necessarynecessary: keeps its name and grants no group
analyticsanalytics
adsmarketing
any other categoryits own name

CookieConsent always accepts the read-only necessary category, so the default mapping never turns it into a group a destination waits for. Category names are compared case-insensitively.

When several categories map to the same group, the group is true only if every one of them is accepted: any deny denies.

Custom entries are merged with the default mapping. Specify only the categories you want to override. All other defaults remain active.

Custom mapping example​

await startFlow({
sources: {
consent: {
code: sourceCookieConsent,
config: {
settings: {
categoryMap: {
functionality: 'functional', // Your functionality category
advertisement: 'marketing', // CookieConsent's Google Consent Mode guide names it so
},
},
},
},
},
});

When the source sends nothing​

The source publishes a decision only while CookieConsent.validConsent() is true, so it sends nothing:

  • before the visitor makes a choice;
  • after you raise revision in your configuration, until the visitor decides again;
  • in mode: 'opt-out' before a choice, although CookieConsent runs the scripts of your enabled categories;
  • with respectGpc for a visitor whose browser sends Global Privacy Control, until that visitor opens the preferences and decides;
  • in a bot or automated browser (Playwright, Selenium, Puppeteer) under CookieConsent's default hideFromBots, so a test setup sets hideFromBots: false in CookieConsent.run.

Destinations gated with config.consent keep that visitor's events queued (see how CMP sources work).

To act on your opt-out defaults before a choice, set them yourself with a walker consent command or the collector's consent setting.

Services​

The source maps categories, not services: it publishes the categories CookieConsent reports as accepted or rejected.

The source sends nothing to Google itself. The gtag destination sets Google Consent Mode with its como setting from the analytics and marketing groups.

If your CookieConsent callbacks already call gtag('consent', ...) (CookieConsent's Google Consent Mode guide), keep one of them: remove those calls, or set "como": false on the gtag destination.

Timing considerations​

The source may start before or after CookieConsent.run. Both orders report the stored decision once:

  • CookieConsent runs before the source: the read at start publishes the stored decision.
  • Source starts before CookieConsent: the source publishes the decision when CookieConsent fires cc:onConsent.

Reference​

💡 Need implementation support?
elbwalker offers hands-on support: setup review, measurement planning, destination mapping, and live troubleshooting. Book a 2-hour session (€399)