Base URLhttps://ottla.ai/api/v1

Guides

JavaScript events and consent

Window events, and a handful of functions you write yourself. There’s no exposed JavaScript API beyond that.

Updated 14 min read

On this page

The widget talks to your page in two ways. You dispatch events on window to open it or hand it a question, and you define a few functions on window that it calls when it needs something from you: the product on the page, the basket, a way to add to it, and the shopper’s consent. That is the whole contract. It is small on purpose, so a theme change or a new widget release does not break your code.

Events you send

Events the widget listens for
Event Detail What it does
ottla:open None Opens the assistant. An inline widget is always open, so it ignores this. It works even before the widget has finished loading, as long as the head tag is on the page.
ottla:ask {query, source} Puts a question in the message box, up to 500 characters, for the shopper to send or edit. It is never sent for them, and never during a live chat with your team. source is optional: one of search, autocomplete, listing, pdp_questions, social_proof, alert, trigger. Dispatch ottla:open as well, so the shopper sees it.

A typical use is an “Ask about this product” button next to the product description:

Ask about this product
<button type="button" id="ask-about-tent">Ask about this tent</button>

<script>
  document.getElementById('ask-about-tent').addEventListener('click', function () {
    window.dispatchEvent(new CustomEvent('ottla:ask', {
      detail: {
        query: 'Is the Trailhead 2 big enough for two people and their packs?',
        source: 'pdp_questions'
      }
    }));
    window.dispatchEvent(new CustomEvent('ottla:open'));
  });
</script>
An example chat on Kestrel & Fell’s store, on the page for Trailhead 2 tent. The assistant answers: “You’re chatting with an AI assistant. I can help you find products and answer questions about this store. Just ask, or request a human at any time.”. The shopper asks: “Is the Trailhead 2 big enough for two people and their packs?”. The assistant answers: “Yes. It sleeps two, and there is a porch on each side big enough for a 50-litre pack, so your kit stays dry outside the inner tent.”.

The mock above shows the result: the shopper taps Ask about this tent, the chat opens with the question ready, and they send it.

Hand over a search that found nothing

Your own search results page is another good place for ottla:ask. When a search for “tent for a wet festival” finds nothing, offer the assistant with the shopper’s words already in the box. The assistant can ask a clarifying question or suggest something close, which an empty results page cannot.

Zero results
// On your zero-results page
document.getElementById('ask-instead').addEventListener('click', function () {
  window.dispatchEvent(new CustomEvent('ottla:ask', {
    detail: { query: searchTerms, source: 'search' } // the words the shopper searched for
  }));
  window.dispatchEvent(new CustomEvent('ottla:open'));
});

Events the widget sends

Events the widget dispatches
Event Detail When
ottla:cart-updated {v: 1, ok, source, items: [{product_id, sku, qty}]} After an add to basket the widget makes through the store’s own add-to-basket route, which is how the Magento module works. If you define OttlaCartAdd (below), your function makes the add, so refresh your mini basket there.
Refresh your mini basket
window.addEventListener('ottla:cart-updated', function (event) {
  // detail: { v: 1, ok, source, items: [{ product_id, sku, qty }] }
  if (event.detail && event.detail.ok) {
    refreshMiniBasket(); // your own function
  }
});

Functions you can define

Hooks the widget calls
Function Returns What it is for
window.OttlaPageContext() {page_context, basket}, or just a page context Read before each question, so answers know which product or page the shopper is looking at.
window.OttlaCart() {items, item_count, subtotal, grand_total, currency, coupon_code, checkout_url} Lets answers take what is already in the basket into account. At most 50 lines are read.
window.OttlaCartAdd(items, opts) A promise of {ok, added, failed, redirect_url} Lets the chat’s Add to basket button use your platform’s basket, up to 20 items at a time. Without it, product cards in the chat link to the product page instead.
window.OttlaPageContext
window.OttlaPageContext = function () {
  return {
    page_context: {
      product: {
        sku: 'TENT-2P',
        name: 'Trailhead 2 tent',
        url: location.href
      }
    }
  };
};
window.OttlaCart
window.OttlaCart = function () {
  var cart = myStore.getBasket(); // your platform's basket, read synchronously
  return {
    items: cart.lines.map(function (line) {
      return { sku: line.sku, product_id: line.productId, name: line.name, qty: line.quantity };
    }),
    item_count: cart.itemCount,
    subtotal: cart.subtotal,
    grand_total: cart.total,
    currency: 'GBP',
    coupon_code: cart.couponCode || null,
    checkout_url: 'https://shop.example/checkout'
  };
};
window.OttlaCartAdd
window.OttlaCartAdd = function (items, opts) {
  // items: [{ product_id, sku, qty }], at most 20; opts.source says where the add came from
  return myStore.addLines(items).then(function (result) {
    refreshMiniBasket();
    return {
      ok: result.failed.length === 0,
      added: result.added.map(function (line) { return line.sku; }),
      failed: result.failed.map(function (line) {
        return { product_id: line.productId, url: line.productUrl }; // where the shopper can finish
      }),
      redirect_url: null
    };
  });
};

In OttlaCartAdd, added lists what went in, each failed entry can carry a url where the shopper can finish (to choose a size, say), and redirect_url sends the shopper somewhere first, such as your sign-in page. Define all three functions before the widget’s tags, and keep OttlaCart synchronous.

The assistant switched on

The widget and the Magento module follow one privacy mode for each shopper, based on the shopper’s choice and your store’s privacy rules (UK, EU and EEA, Germany or other). With the Magento module, the rules follow where your store is established, as set in the module’s settings. On a site without the module, the widget takes the rules from the country confirmed for each storefront under Reviews > Disclosures, or from Privacy rules in the widget’s settings: a storefront confirmed as US follows Other rules unless it is set to a currency other than US dollars, and one with no confirmed country follows EU rules. After a yes, they set the visitor cookie ottla_vid and the conversation continues between visits. Without a yes, on a UK or EU store, they set no visitor cookie, and the conversation lasts only while that browser tab stays open. On a store under Other rules they behave as after a yes until the shopper says no.

OttlaConsent should take a purpose and return true, false, or nothing while the shopper has not yet chosen. Set OttlaConsent.purposes = ['measurement', 'statistics'] to tell Ottla your function handles both. Ottla asks for 'measurement'. With the Magento module it also asks for 'statistics', but only on UK stores and on websites with a UK store view. On a site without the module, the widget asks for 'statistics' on any store whose function lists it, and a false there turns Ottla off even after a yes to 'measurement'. On such a site, once My privacy notice includes Ottla’s text is on in the Ottla console, list 'statistics' too and return false for it when the shopper objects: that is how a shopper opts out of Ottla’s counting there. Leave it out only while that setting is off, and only if you do not want a no to statistics to turn Ottla off. A function without purposes is asked once, with no purpose, and its false counts as a no once your consent tool records the shopper’s answer, once your page sends ottla:consent (unless a consent tool whose record is on the device is still loading), or, with no consent tool Ottla recognises, once the page has finished loading. Until a tool or ottla:consent confirms it, a no known only from the page having loaded sets the mode for that page but deletes and forgets nothing. With Cookiebot, OneTrust or Usercentrics, Ottla reads the tool itself, and on Automatic it reads your own function too: a no from either counts. On Magento, to let shoppers stop Ottla’s measuring from your own page, link to /ottla/privacy or call window.OttlaPrivacy.object().

<script>
  // Before the Ottla tags: answer from your consent manager
  window.OttlaConsent = function (purpose) {
    if (!myCmp.hasChosen()) { return undefined; } // nothing until the shopper chooses
    if (purpose === 'statistics') { return myCmp.allows('statistics'); } // UK: on unless turned off
    return myCmp.allows('measurement');
  };
  window.OttlaConsent.purposes = ['measurement', 'statistics'];
  // After any change in your consent manager:
  // document.dispatchEvent(new Event('ottla:consent'));
  // On Magento, from your own page: window.OttlaPrivacy.object() stops Ottla measuring
  // the shopper's visits, and window.OttlaPrivacy.allow() undoes it.
</script>

What changes with consent

With and without consent
Area Without consent With consent
The conversation Carries on across pages in the same browser tab, and ends when the tab closes Follows the shopper across pages, and earlier chats can be reopened on the same device
Measurement cookies None ottla_vid, a first-party cookie that lasts 13 months from when it is set and is never renewed, SameSite=Lax
Browser storage Only the conversation id and a few markers, in that tab’s session storage, which clears when the tab closes. No message text. Used to keep the conversation and whether the chat window is open
Order attribution Clicks and adds in Ottla’s blocks and search are only counted, with no identifier, and no basket line is marked. Marks and chats kept with the basket before the shopper said no are deleted and not sent with the order. Ottla deletes the ottla_vid cookie, your store stops reading it at once, and Ottla deletes what it had linked to it under the shopper’s yes; links made under the store’s privacy notice before the no stay. For signed-in shoppers, chats are not linked to orders Linked to the visitor id, so Analytics can show which Ottla features shoppers used prior to an order
Product views and recently viewed (Magento module) Not sent; recently viewed records nothing Sent and recorded

Our cookie policy and the data and privacy page describe this for shoppers and for your privacy notice.

On Magento

The Magento module defines OttlaPageContext, OttlaCart and OttlaCartAdd for you from Magento’s own data, so do not define your own. For consent, the module reads window.OttlaConsent if your store defines it, any of Cookiebot, OneTrust or Usercentrics it finds, Magento’s own cookie notice once you add Ottla’s Decline button to it, and Google Consent Mode if you switch it on, and it applies your settings under Ottla > Settings > Shopper privacy. The module no longer defines OttlaConsent itself.

Every Ottla script adapts to the shopper’s privacy mode, worked out from your consent tool and your store’s privacy rules. After a yes (on a store under Other rules, until the shopper says no), clicks and adds in Ottla’s blocks and search carry the visitor id and, for a signed-in shopper, their account link, product views are sent, and an add to basket from an Ottla feature sends the X-Ottla-Surface header, which marks the basket line with that feature. Without a yes, on a UK or EU store, clicks and adds are only counted, with no identifier, once you have confirmed your privacy notice in the module’s settings and accepted the updated data processing agreement; no basket line is marked and product views wait. With the widget added directly to another platform, Ottla counts shoppers on UK or EU stores who have not said yes in the same way, with no identifier, only once you have turned on My privacy notice includes Ottla’s text in the website’s settings in the Ottla console and accepted the updated data processing agreement. On a UK store, a no that does not say whether the shopper objects to statistics stops Ottla’s counting as well. After any no, Ottla deletes the ottla_vid cookie.

The module’s storefront blocks add a few more events and objects you can use in theme code:

Extra events and objects on Magento
Name Kind What it is for
ottla:project-quantity Event you send Drives the quantities in a “What you’ll need” kit from your own calculator. Detail {v: 1, product_id, qty, unit, source}.
ottla:kit-ready Event Fires once per kit block, so a calculator that announced too early can announce again.
ottla:listing-updated Event After an in-place listing update, with total, offset and badges.
ottla:search-impression, ottla:search-click, ottla:search-redirect Events From the search box, for your own analytics.
ottla:recently-viewed-ready Event window.OttlaRecentlyViewed is ready to use.
window.OttlaBadges Object render(), build() and scan() for badges in your own markup.
window.OttlaRecentlyViewed Object ids(), record(id) and clear().
window.OttlaTrack(event, fields) Function Block measurement, loaded with “Measure Ottla blocks”.
window.OttlaListing.navigate(url) Function Moves an Ottla listing to another page or filter in place.

FAQ

Frequently asked questions

How do I open the assistant from my own button?

Dispatch ottla:open on window. To fill in a question too, dispatch ottla:ask with {query} first.

Which cookies do the widget and the module set, and when?

The widget sets one cookie, ottla_vid, a first-party cookie kept for 13 months from when it is set, and only when the shopper’s privacy mode allows it: after a yes, or on a store outside the UK and the EU until the shopper says no. On Magento, the module also sets cookies that hold no identifier: ottla_choice, ottla_optout and ottla_decline store the shopper’s choice, and ottla_vid_sites records which of the store’s websites use ottla_vid. When a shopper votes on a review or an answer, it sets ottla_vote_id, so the vote is counted only once, and it limits repeated or fake reports, questions and reviews. The widget and the module also keep some data in the browser’s storage, such as the chat. The module’s README lists every cookie and storage key, with how long each is kept.

What if I do not define OttlaConsent?

Ottla reads Cookiebot, OneTrust or Usercentrics if it finds one. With no consent tool at all, a shopper on a UK or EU store is treated as not having said yes: the widget keeps the conversation to that browser tab only and sets no visitor cookie. On a store under Other rules it works as after a yes until the shopper says no. On a site without the Magento module, a storefront confirmed as US under Reviews > Disclosures follows Other rules unless it is set to a currency other than US dollars, and one with no confirmed country follows EU rules.

Can the chat add to my headless basket?

Yes. Define window.OttlaCartAdd and the chat’s Add to basket button calls it. Without it, product cards link to the product page.

How do I pass the basket so answers know it?

Define window.OttlaCart() to return the basket lines and totals. The widget reads it before each question.

Next

Try Ottla

Give every shopper a good answer.

Start your free trial on your own store, or book a demo and we’ll show you Ottla on a store like yours.

14 days free. No card needed.