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

# Analytics consent

> Pass your visitors’ statistics consent from your consent management platform (CMP) to homie with setConsent.

The chat works without any consent call. To recognize returning visitors in your analytics, homie needs your visitors' consent for statistics. Your consent management platform (CMP) collects that consent, and you pass it to homie with `window.homieBot.setConsent()`.

| Situation | What homie does |
| - | - |
| No `setConsent` call on the page | No visitor ID. The chat works as usual. |
| `setConsent({ analytics: true })` | homie stores a pseudonymous visitor ID in the visitor's browser and uses it for analytics. After 12 months it is replaced by a new one. |
| `setConsent({ analytics: false })` | homie stops using the visitor ID and deletes it the next time the chat loads (right away if it is already loaded). |

## Report consent on every page view

homie does not remember the consent. Report it on every page view, as soon as the Client API is ready (`homiebot:api-ready`), and again whenever the visitor changes their choice in your CMP. Until your call arrives, homie treats the visitor as not consenting.

Each example below does two things:

1. When the Client API is ready, it reads the current consent from your CMP and passes it to homie.
2. It listens for the CMP's own consent event and passes every change to homie.

> [!WARNING]
>
> **Do not block the homie script in your CMP.** Many CMPs can block scripts automatically by category. If you put the
> homie script under "Statistics", the whole chat disappears for every visitor who declines statistics. Classify the
> script as strictly necessary ("Essential" or "Necessary" in most CMPs), and control analytics only through
> `setConsent`. In most CMPs, the "Functional" or "Preferences" category also requires consent.

## Examples for common CMPs

All examples use this helper, which runs your code once the Client API is ready, whether the embed script has already loaded or not:

```js theme={null}
function whenHomieReady(callback) {
  if (window.homieBot) {
    callback();
  } else {
    window.addEventListener('homiebot:api-ready', callback, { once: true });
  }
}
```

> [tab] Usercentrics
>
> Add homie as a service in your Usercentrics settings and use that service's ID.
>
> ```js usercentrics.js theme={null}
> const HOMIE_SERVICE_ID = 'YOUR-SERVICE-ID';
>
> function hasHomieConsent(services) {
>     return services?.[HOMIE_SERVICE_ID]?.consent?.given === true;
> }
>
> // Current consent, once the Client API is ready.
> whenHomieReady(async () => {
>     const details = await window.__ucCmp?.getConsentDetails();
>     window.homieBot.setConsent({ analytics: hasHomieConsent(details?.services) });
> });
>
> // Every later change.
> window.addEventListener('UC_CONSENT', (event) => {
>     window.homieBot?.setConsent({ analytics: hasHomieConsent(event.detail.services) });
> });
> ```

> [tab] Cookiebot
>
> Cookiebot works with categories. homie analytics belongs to **Statistics**.
>
> ```js cookiebot.js theme={null}
> function reportHomieConsent() {
>     window.homieBot?.setConsent({ analytics: window.Cookiebot?.consent?.statistics === true });
> }
>
> // Current consent, once the Client API is ready.
> whenHomieReady(reportHomieConsent);
>
> // Consent loaded or changed.
> window.addEventListener('CookiebotOnConsentReady', reportHomieConsent);
> ```

> [tab] Borlabs Cookie
>
> Add homie as a service in Borlabs Cookie and use that service's ID.
>
> ```js borlabs-cookie.js theme={null}
> const HOMIE_SERVICE_ID = 'homie';
>
> function reportHomieConsent() {
>     const analytics = window.BorlabsCookie?.Consents?.hasConsent(HOMIE_SERVICE_ID) === true;
>     window.homieBot?.setConsent({ analytics });
> }
>
> // Current consent, once the Client API is ready.
> whenHomieReady(reportHomieConsent);
>
> // Borlabs Cookie initialized, and every later change.
> window.addEventListener('borlabs-cookie-after-init', reportHomieConsent);
> window.addEventListener('borlabs-cookie-consent-saved', reportHomieConsent);
> ```

> [!TIP]
>
> Accept statistics in your CMP and reload the page. In your browser's developer tools, the chat's local storage
> contains `homie.visitor` after you open the chat. Withdraw consent and open the chat again: the entry is gone.

## If your CMP already loads the homie script after consent

Some shops load the whole homie script only after the visitor has consented to statistics. In that case, the consent is given whenever the script runs. Report it directly:

```js theme={null}
whenHomieReady(() => {
  window.homieBot.setConsent({ analytics: true });
});
```

> [!NOTE]
>
> With this setup, visitors without consent see no chat at all. To offer the chat to every visitor, load the script as
> strictly necessary ("Essential" or "Necessary") and use one of the examples above.

## Reference

### setConsent(consent)

Passes the visitor's statistics consent to homie. Returns `void`.

| Field | Type | Description |
| - | - | - |
| `analytics` | boolean | `true` if the visitor consented to statistics, otherwise `false`. |

Any other value is ignored and logged as a warning in the browser console. Calling `setConsent` again with a new value replaces the previous one.


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