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

# Consent mode

> Store nothing in the browser until the visitor gives consent, and still count page views and events.

By default, the SDK stores a persistent first-party visitor ID. If your site must get consent before analytics storage (for example, for visitors in the EU), start the SDK in **consent mode**.

## How consent mode works

Until you call `setConsent(true)`:

* The SDK reads and writes **no cookies and no `localStorage`**.
* Page views and custom events are still sent, but **without** `anonymous_id`, `session_id` or `user_id`.
* These events count toward page view and event totals. They do **not** count as visitors or sessions, and they do not appear in retention, funnels, user paths or profiles.
* An `identify()` call is held in memory and sent when consent is granted.

`setConsent(true)` creates or restores the persistent visitor ID. `setConsent(false)` clears all identity storage, drops the user ID, removes identity fields from queued events and discards queued `identify` events.

The SDK does not store the consent choice. Call `setConsent()` on each page load, after your consent manager reports the choice.

## Enable consent mode

> [tab] HTML snippet
>
> Add `data-require-consent="true"`:
>
> ```html theme={null}
> <script
>   src="SCRIPT_URL_FROM_THE_SETUP_PAGE"
>   data-publishable-key="YOUR_PUBLISHABLE_KEY"
>   data-endpoint="https://YOUR_COLLECTOR_ENDPOINT"
>   data-require-consent="true"
>   defer
> ></script>
> ```
>
> Then call the SDK from your consent banner:
>
> ```js theme={null}
> window.untitledAnalytics.setConsent(true);  // consent granted
> window.untitledAnalytics.setConsent(false); // consent withdrawn
> ```

> [tab] npm
>
> ```ts theme={null}
> import { createAnalytics } from '@untitled-analytics/web';
>
> const analytics = createAnalytics({
>   publishableKey: 'YOUR_PUBLISHABLE_KEY',
>   endpoint: 'https://YOUR_COLLECTOR_ENDPOINT',
>   requireConsent: true,
> });
>
> consentManager.onChange(granted => analytics.setConsent(granted));
> ```

`setConsent()` also works without consent mode. `setConsent(false)` then turns identity off for that instance.

## Opt out

For a visitor who asks not to be tracked at all:

| Method | Effect |
| - | - |
| `optOut()` | Drops queued events, clears identity, and stores an opt-out flag for one year. Later page loads send nothing. |
| `optIn()` | Removes the flag and creates a new anonymous ID |
| `isOptedOut()` | Returns `true` when the browser is opted out |
| `disabled: true` | Turns the SDK off for one instance. It sends nothing and does not touch browser storage. |

## Test it

1. Load your site in a private window and open the developer tools.
2. Before consent, confirm that no `ua_` cookie or `localStorage` key exists, and that `POST /v0/events` bodies have no `anonymous_id`.
3. Grant consent. Confirm that the `anonymous-id` cookie appears and that later events carry it.
4. Withdraw consent. Confirm that the cookies are removed.

> [!NOTE]
>
> Whether your site needs consent is a decision for you and your legal advisers. Consent mode gives you the technical control.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.