> For the complete documentation index, see [llms.txt](https://doc.dastra.eu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.dastra.eu/en/features/cookies/integration/blocking.md).

# Blocking cookies

This guide explains how to actually block cookies.

To block cookies effectively, there are several methods: deleting cookies, blocking a snippet, custom JavaScript or Google Tag Manager.

## Deleting cookies

This method is the fastest to set up, but also the least reliable. In the **Services** section of the banner, if you enter the name of the cookies associated with each service (**Linked cookies** tab of the service), the SDK automatically deletes these cookies when a refusal is recorded for the service.

<figure><img src="https://2697025545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LvBxs22wUMicv9uWp6C-1972196547%2Fuploads%2Fgit-blob-15bab1623478cb092d9135de0eca5c2232f40678%2Fcookies-blocking-1.png?alt=media" alt="The Linked cookies tab of a service with the list of its cookies"><figcaption><p>The cookies linked to a service</p></figcaption></figure>

This method works in some cases, but it may disrupt the reliability of third-party tools, in particular web analytics tools. Use it alongside one of the other methods below.

## Blocking a code snippet in the page

This method completely disables a tracking code snippet.

In the HTML code of your page, replace the following snippet:

```html
<script>
  alert("hello, I'm a tracking javascript tag");
</script>
```

With:

```html
<script data-consent="{your-service-slug}" type="dastra/script">
  alert("hello, I'm a tracking javascript tag");
</script>
```

Replace `{your-service-slug}` with the identifier of the service, which you find in the **Variable name** field of the service form:

<figure><img src="https://2697025545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LvBxs22wUMicv9uWp6C-1972196547%2Fuploads%2Fgit-blob-5adb83ba27507ebb59ce2a9b659f69e3145757cb%2Fcookies-blocking-2.png?alt=media" alt="The Variable name field of a service form"><figcaption><p>The identifier of the service</p></figcaption></figure>

If the visitor accepted the service, the content of the script is executed automatically.

To load an external script, add the `src` attribute: the browser downloads it only after consent.

```html
<script data-consent="{your-service-slug}" type="dastra/script" src="https://example.com/tracker.js"></script>
```

{% hint style="info" %}
This method has side effects: the syntax highlighting of most code editors does not recognize the `dastra/script` type. The script never runs if the Dastra banner fails to load.
{% endhint %}

### Use the snippets generated by the application

For each service, the **Cookie consents integration** section of the banner generates ready-to-copy snippets and a checklist. Three tabs are available: **Html integration** (the snippet above), **Custom javascript** (the plain JavaScript blocking below) and **Google tag manager** (the events to connect in Google Tag Manager).

## Blocking in plain JavaScript

In JavaScript, you can listen to the events fired on `window` to collect consent and handle a specific behavior depending on whether cookies are accepted or refused. This method gives you more flexibility:

```html
<script>
  (function () {

    /*
     * Trigger a custom service tag with the expected cookies
     */
    function customTagsTrigger() {
      /* If the vendor provides a specific function to make the service work without cookies, call it here.
       * Otherwise, copy the default code snippet provided by the vendor. */
    }

    /*
     * Handle the global scope consent event.
     * If the user consented to the vendor's cookies, this event is fired on each page load
     * where the cookie consent banner is installed.
     */
    window.addEventListener('dastra:consent:{your-service-slug}', function () {
      /* The visitor opted in to the vendor's cookies */
      customTagsTrigger();
    });

    /*
     * Handle the global scope refused event (optional)
     */
    window.addEventListener('dastra:refused:{your-service-slug}', function () {
      /* The vendor's cookies are refused */
    });

  })();
</script>
```

## Google Tag Manager

See the following page:

{% content-ref url="/pages/-M5n29FF-y8wBMLdGgjt" %}
[Google Tag Manager](https://doc.dastra.eu/en/features/cookies/integration/blocking/google-tag-manager.md)
{% endcontent-ref %}

## Reminder on the consent lifecycle

<figure><img src="https://2697025545-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LvBxs22wUMicv9uWp6C-1972196547%2Fuploads%2Fgit-blob-e6d1ddf819e463c60044733ee6decc6928e04b01%2Fcookies-lifecycle.drawio.png?alt=media" alt="Diagram of the consent lifecycle, from page load to saving the consent-eu cookie"><figcaption><p>Consent lifecycle</p></figcaption></figure>

## JavaScript events

The banner fires several events on the `window` element of the page. The consent events are fired on every page load, when the banner applies the choices already saved, and then each time a choice is saved.

| Event name                      | Remarks                                                                                                                                                                                                         |
| ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `dastra:consent:<service slug>` | Fires when the service is accepted, by the visitor or by default (strictly necessary services are not concerned). If the service is set to "consented by default", the event fires from the first page load.    |
| `dastra:refused:<service slug>` | Fires when the service is not accepted: by default if no consent is given, or after an explicit refusal.                                                                                                        |
| `dastra:consents:ready`         | Fires when the stored consent (the `consent-eu` cookie by default) has been read and the first events have been fired.                                                                                          |
| `dastra:consents:updated`       | Fires on every page load and each time choices are saved (accepted, refused or customized). The event detail contains the list of consents.                                                                     |
| `dastra:consents:any_refused`   | Fires when at least one stored consent is a refusal, on every page load and each time choices are saved.                                                                                                        |
| `dastra:consents:all_accepted`  | Fires when no stored consent is a refusal, on every page load and each time choices are saved.                                                                                                                  |
| `dastra:consents:closed`        | Fires when the visitor closes the banner with the close cross.                                                                                                                                                  |
| `dastra:gpc:applied`            | Fires when the banner automatically refuses non-essential cookies because of a Global Privacy Control or Do Not Track signal. See [US Privacy](https://doc.dastra.eu/en/features/cookies/banner/us-privacy.md). |

{% hint style="info" %}
Because the `dastra:consent:*` and `dastra:consents:*` events are also fired on page load, register your reload listeners inside `dastra:consents:ready`. See [Google Tag Manager](https://doc.dastra.eu/en/features/cookies/integration/blocking/google-tag-manager.md).
{% endhint %}

To react to each consent update, you can also use `dastra.addConsentDastraListener(callback)`: the function is called on every `dastra:consents:updated` event.

***

## FAQ

**Can I block a script by category instead of by service? (e.g. `data-consent="analytics"`)**

No. The `data-consent` attribute expects a **service slug**, not a category slug. Writing `<script data-consent="analytics" type="dastra/script">` to block all services in a category in one directive is not supported.

The reason: a user may accept some services within a category while refusing others in the same category. The banner manages consent at the service level, not the category level.

To load a script depending on whether an entire category has been accepted, use the programmatic API:

```javascript
window.addEventListener('dastra:consents:updated', function () {
  if (window.dastra.cookieConsent.consent.getPurposeConsent('Analytical')) {
    // load your analytics scripts here
  }
});
```

{% hint style="warning" %}
`getPurposeConsent` returns `true` only if **all** the services of the category are accepted. If the banner has no service in that category, the call throws an error: guard it, for example with a `try ... catch`.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://doc.dastra.eu/en/features/cookies/integration/blocking.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
