> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# $wsConsent - Cookies & Services

> Evaluate the customer's cookie and service consent in the frontend to control tracking, third-party scripts, and embedded content in a compliant way.

With the `$wsConsent` module, you read the customer's consent to cookies and services and use it to control which content and external scripts are loaded. This lets you, for example, embed a tracking or marketing service only after the customer has agreed to it.

This page is about reading the consent status. Setting the consent (accepting, declining, saving the selection) is done via actions and is documented separately under [Actions → Consent](/en/frontend/referenz/aktionen/consent). The associated cookies themselves are handled by the [\$wsCookies](/en/frontend/referenz/module/wscookies) module.

***

## Basic concept

The consent is organized in two stages: groups (e.g. "Statistics", "Marketing") bundle individual services (e.g. "Google Analytics"). The customer can either agree to all ("Allow all") or make a selection per group/service. Each entry carries an "allowed" flag that reflects the decision.

`$wsConsent` provides this status in the following ways:

* **Overall status** – [`alreadySet`](#wsconsent-alreadyset) (has the customer made any decision yet?) and [`allAllowed`](#wsconsent-allallowed) (have they agreed to everything?). Use these to control whether the consent layer needs to be displayed at all.
* **Structured list** – [`groups`](#wsconsent-groups) with their `services`, to build a consent layer.
* **Targeted check** – [`checkAllowed(serviceName)`](#wsconsent-checkallowed), to check the consent before loading a specific script. This is the recommended way to embed external scripts.

### Consent decides whether the script is loaded at all

Template code runs when the page is built. If you wrap a script with `{{ if $wsConsent.checkAllowed(...) }}`, the script is not even written into the page when consent is missing — it is not just hidden. This ensures that a declined service really does not load.

***

## Module overview

**Example / excerpt of** `$wsConsent`

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $wsConsent | json }}
```

**JSON output**

```json theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{
  "alreadySet": false,
  "allAllowed": false,
  "groups": [
    {
      "name": "...",
      "label": "...",
      "description": "...",
      "allowed": false,
      "services": [
        { "name": "...", "label": "...", "description": "...", "allowed": false }
      ]
    }
  ],
  "services": [
    { "name": "...", "label": "...", "description": "...", "allowed": false }
  ],
  "checkAllowed": "ƒ()"
}
```

Note: `"ƒ()"` denotes a function.

**Variables overview**

| **Variable** | **Type** | **Description**                                          |
| ------------ | -------- | -------------------------------------------------------- |
| `alreadySet` | bool     | Whether the customer has already given consent.          |
| `allAllowed` | bool     | Whether the customer has agreed to all cookies/services. |
| `groups`     | array    | Configured groups, each with their `services`.           |
| `services`   | array    | Flat list of all configured services.                    |

**Properties of an entry** (applies to `groups[]` as well as `services[]`)

| **Property**  | **Type** | **Description**                             |
| ------------- | -------- | ------------------------------------------- |
| `name`        | string   | Technical name (for `checkAllowed()`).      |
| `label`       | string   | Label visible to the customer.              |
| `description` | string   | Description.                                |
| `allowed`     | bool     | Whether the entry is allowed (accepted).    |
| `services`    | array    | Only for `groups[]`: the assigned services. |

**Methods overview**

| **Method**       | **Return type** | **Description**                                      |
| ---------------- | --------------- | ---------------------------------------------------- |
| `checkAllowed()` | bool            | Checks whether a specific service has been accepted. |

***

## Templates

The consent layer can be called globally and is loaded from the template `consent.htm`.

***

## Variables

### \$wsConsent.alreadySet

Returns whether the customer has already set a cookie/service consent. Use it, for example, to show the consent layer only when no decision has been made yet.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if not $wsConsent.alreadySet }}
  <!-- Show consent layer -->
{{ /if }}
```

### \$wsConsent.allAllowed

Returns whether the customer has agreed to all services ("Allow all" button).

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsConsent.allAllowed }}
  <!-- All services have been agreed to -->
{{ /if }}
```

### \$wsConsent.groups

Returns the configured groups. Each group carries the [properties of an entry](#module-overview) and contains the assigned services under `services`. Use the groups to build a structured consent layer.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ foreach $group in $wsConsent.groups }}
  {{= $group.label }}
  {{ foreach $service in $group.services }}
    - {{= $service.label }}
  {{ /foreach }}
{{ /foreach }}
```

### \$wsConsent.services

Returns all configured services as a list, regardless of group assignment. The entries carry the [properties of an entry](#module-overview).

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ foreach $service in $wsConsent.services }}
  {{= $service.label }}: {{= $service.allowed }}
{{ /foreach }}
```

***

## Methods

### \$wsConsent.checkAllowed()

Checks whether a specific service has been accepted by the customer. This is the recommended way to check consent before loading an external script, because the script is not rendered at all if consent is missing.

**Signature**\
`$wsConsent.checkAllowed(serviceName)`

**Return value**\
`bool` – `true` if the service has been accepted, otherwise `false`.

| **Name**      | **Type** | **Required** | **Description**                                               |
| ------------- | -------- | ------------ | ------------------------------------------------------------- |
| `serviceName` | string   | yes          | Technical name of the service (the `name` field of an entry). |

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsConsent.checkAllowed("googleAnalytics") }}
  <!-- Include Google Analytics tracking code -->
{{ /if }}
```

***

## Actions

Actions for this module (set, change, save consent) are documented separately: [Actions → Consent](/en/frontend/referenz/aktionen/consent).

***

## Examples

### Load an external script depending on consent

Embeds a script only if the corresponding service has been accepted. If consent is missing, the script is not written into the page.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsConsent.checkAllowed("googleAnalytics") }}
  <script src="https://www.googletagmanager.com/gtag/js?id=..."></script>
{{ /if }}
```

**Result** \
The tracking script appears in the source only if the customer has accepted "Google Analytics".

### Build a consent layer from groups and services

Iterates over the groups and, for each group, its services, which reflects the typical structure of a consent layer.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ foreach $group in $wsConsent.groups }}
  <fieldset>
    <legend>{{= $group.label }}</legend>
    <p>{{= $group.description }}</p>
    {{ foreach $service in $group.services }}
      <label>
        <input type="checkbox" name="{{= $service.name }}"{{ if $service.allowed }} checked{{ /if }}>
        {{= $service.label }} – {{= $service.description }}
      </label>
    {{ /foreach }}
  </fieldset>
{{ /foreach }}
```

**Result** \
One block per group with the contained services; services already accepted are checked.

### Show content only with consent

Shows content that requires consent (e.g. an embedded video) only if the service has been accepted; otherwise a notice is displayed.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ if $wsConsent.checkAllowed("youtube") }}
  <!-- e.g. embedded YouTube video -->
{{ else }}
  To see this content, please accept the "Youtube Videos" service.
{{ /if }}
```

**Result** \
With consent, the content is shown; otherwise the notice.

***

## Related links

* [Actions → Consent](/en/frontend/referenz/aktionen/consent) – set and save the consent (this module only reads it).
* [\$wsCookies](/en/frontend/referenz/module/wscookies) – the cookies controlled by the consent.
