Modules / Visibility & conditions

Condition: cookie

Show or hide an element by a cookie in the visitor's browser: whether it exists, what it equals, or what it contains. Use it for consent banners, returning visitors or A/B buckets.

#28 of 39 modules by votes

  • On by default
  • Needs Bricks 2.4 or newer
  • Tested with Bricks 2.4.3
Recording: someone gives a banner the condition Cookie exists woocommerce_items_in_cart, adds a mug to the cart in the shop, and on the next page load the banner appears.Recorded in Bricks 2.4.3

How to use it

  1. Select the element and open its Conditions.
  2. Add the condition Cookie from the Kiln group.
  3. Choose how to compare: ==, !=, contains, does not contain, exists or does not exist.
  4. In the value field, type the cookie as name=value, or only name.

Examples:

  • cookie_consent=yes with ==: the element shows when the cookie cookie_consent equals yes.
  • returning_visitor with exists: the element shows when that cookie is present.
  • cookie_consent alone with !=: the element shows when the cookie is absent.

The name and the value accept dynamic data. Kiln renders it before it splits the text at the first =.

Where it appears in Bricks

Element conditions: Cookie, in the Kiln group.

How it works with page caches

A cookie differs for every visitor, so a page that checks one cannot come from a shared cache. When the page evaluates a Cookie condition, Kiln tells page-cache plugins not to cache it (the DONOTCACHEPAGE constant, the LiteSpeed Cache no-cache call, and no-cache headers when they have not been sent yet).

A cache at the host or CDN level, such as nginx, Varnish or Cloudflare "cache everything", only sees the response headers. Those are usually sent before Bricks renders the page. Exclude the page from that cache, or make the cache vary by the cookie, or one visitor's version reaches everyone.

Limits

  • Matching is exact and case-sensitive. PHP URL-decodes cookie values before Kiln reads them, so compare with the decoded value: a b, not a%20b.
  • PHP also turns dots and spaces in a cookie's name into underscores. For a cookie named my.pref, write my_pref.
  • With no value, == and contains mean the cookie exists. != and does not contain mean it does not.
  • With exists or does not exist, Kiln ignores any value.
  • An empty cookie name makes the condition false.
  • Kiln reads the cookies the browser sent with the request. A cookie your script sets in the browser shows up from the next page load.

Settings

None. Turn the module on in Bricks > Kiln (it is on by default).

Requirements

Bricks 2.4 or newer.