Skip to content

Load the chat after visitors accept cookies

Start the widget from your consent banner, and remove it and its stored data if consent is withdrawn.

Last updated

The widget sets no cookies. It does keep a few things in the visitor's browser storage: a random visitor id, their current conversation, unsent text and their preferences. It also sends anonymous usage events, such as the chat opening, without any message text. If your consent rules cover these, start the widget only after the visitor agrees.

1. Load the script without starting the widget#

Leave the deployment's attributes off the script tag. The script loads, but no widget starts:

html
<script src="https://cdn.help.ai/sdk/js/elements/1.12.0/elements.js" defer></script>

2. Start it when the visitor agrees#

Call init from your consent banner, with the public key and deployment ID from the deployment's Install tab. The first line lets the call run before the widget's script has loaded:

js
window.HelpAI = window.HelpAI || function () { (HelpAI.q = HelpAI.q || []).push(arguments); };

function onConsentGiven() {
  HelpAI('init', {
    publicKey: 'agent_pk_...',
    aiAgentDeploymentId: '...',
  });
}

Call it on every page once consent is known, including when your consent tool loads a choice the visitor made earlier.

js
function onConsentWithdrawn() {
  HelpAI('destroy');
  for (const store of [localStorage, sessionStorage]) {
    for (const key of Object.keys(store)) {
      if (key.startsWith('helpai.')) store.removeItem(key);
    }
  }
}

destroy removes every widget from the page. The loop deletes what the widget stored in this browser: all its keys start with helpai..

Offer the chat without storing anything#

To let visitors chat before they agree, give the widget memory-only storage. Its visitor id, conversation and preferences then live only in the page, and are gone when it reloads. Each page load counts as a new visitor.

js
const memory = new Map();

HelpAI('init', {
  publicKey: 'agent_pk_...',
  aiAgentDeploymentId: '...',
  storage: {
    get: (key) => memory.get(key) ?? null,
    set: (key, value) => memory.set(key, value),
    remove: (key) => memory.delete(key),
  },
});

Usage events#

Browsers that send Global Privacy Control or Do Not Track turn the widget's usage events off on their own. To turn them off for everyone, add tracking: { enabled: false } to init.

Test both paths in a private window: before consent, your browser's storage should have no helpai. keys.

Was this helpful?

Still need help?

Ask the community