Skip to content

Widget

JavaScript API

window.AskMerra lets your storefront open the assistant, send messages, switch language and react to what shoppers do in the chat.

When is it available?

window.AskMerra exists as soon as the widget script has executed. Calls made before the widget finished loading are queued and run in order. The script loads async, so to be safe hook in from onReady:

<script>
  window.AskMerraSettings = {
    onReady: function () {
      AskMerra.on('add_to_cart', addToMyCart);
    },
  };
</script>
<script src="https://cdn.askmerra.com/v1/widget.js" data-site-key="pk_live_…" async></script>

Methods

MethodDescription
AskMerra.open()Opens the chat panel.
AskMerra.close()Closes it.
AskMerra.toggle()Opens or closes.
AskMerra.sendMessage(text)Opens the panel and sends text as the visitor (max 1000 characters).
AskMerra.setLocale(locale)Switches the interface language: en ro it fr de es.
AskMerra.identify({ email, name })Pre-fills the contact form. If visitor identification is enabled in Settings, also attaches the identity to the visitor once they chat.
AskMerra.forget()Deletes this visitor’s conversations, activity and identity from AskMerra (GDPR right to erasure), clears the chat and removes the ids the widget stored in the browser. If the shopper chats again, they get a new, unrelated anonymous id. Returns a promise that resolves once the data is deleted. Wire it to a “Delete my chat data” button on your privacy page.
AskMerra.trackPurchase(order)Sales tracking: reports a purchase on your order confirmation page, in GA4 shape. Not needed if the page already pushes a GA4 purchase event to the dataLayer. See Sales tracking.
AskMerra.setConsent({ analytics })Tells the widget whether the shopper accepted analytics in your cookie banner, when the banner doesn’t use Google Consent Mode.
AskMerra.on(event, handler)Subscribes to an event; returns an unsubscribe function.
AskMerra.versionWidget version string.

Call open() and sendMessage() from a shopper action

The widget stores nothing in the shopper’s browser until the shopper uses the chat. open(), toggle() and sendMessage() count as the shopper using it, so call them in response to a shopper action, for example a click on your own chat button. To open the chat automatically, use data-open or the Open on load setting instead: an automatically opened chat stores nothing until the shopper interacts with it. See Storage & privacy.

Examplesjs
// Your own "Ask an expert" button on product pages
document.querySelector('#ask-expert').addEventListener('click', () => {
  AskMerra.sendMessage('Is this product right for sensitive skin?');
});

// Follow your site's language switcher
languageSwitcher.onChange((lang) => AskMerra.setLocale(lang));

// Logged-in customer: pre-fill the contact form
AskMerra.identify({ email: customer.email, name: customer.firstName });

// Privacy page: let shoppers erase their chat data
document.querySelector('#delete-chat-data').addEventListener('click', async () => {
  await AskMerra.forget();
  alert('Your chat data was deleted.');
});

Events

EventPayloadWhen
readynoneThe widget is mounted. Handlers registered later are called immediately.
open / closenoneThe panel opened or closed.
message{ role, text, products? }The visitor sent a message, or an assistant answer finished streaming.
product_click{ externalId, sku, url }A product card was clicked.
add_to_cart{ externalId, sku, url }The “Add to cart” button on a product card was clicked.

Add to cart

Enable Add to cart in the widget behavior settings, then handle the event with your platform’s cart API. If no handler is registered, the button simply opens the product page.

WooCommerce (AJAX add to cart)js
AskMerra.on('add_to_cart', async ({ externalId }) => {
  // externalId = WooCommerce product id when your catalog comes from WooCommerce
  const body = new URLSearchParams({ product_id: externalId, quantity: '1' });
  await fetch('/?wc-ajax=add_to_cart', { method: 'POST', body });
  document.body.dispatchEvent(new Event('wc_fragment_refresh'));
});
Shopify (Ajax Cart API)js
AskMerra.on('add_to_cart', async ({ externalId }) => {
  // externalId = variant id when your catalog comes from Shopify
  await fetch('/cart/add.js', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ items: [{ id: Number(externalId), quantity: 1 }] }),
  });
});
The handler receives the same identifiers you imported (external_id and sku), so you can map them to whatever your cart expects.

Sales tracking

The dashboard shows the orders placed within 7 days after a chat (Sales through the assistant) and which lines the assistant suggested or added to cart. The widget reads the purchase on your order confirmation page and links it to the shopper’s chat:

  • Only shoppers who chatted. The purchase is sent only if this browser has a chat id. Shoppers who never used the chat are never sent, and nothing is stored for them.
  • Only with analytics consent. The widget follows Google Consent Mode (analytics_storage) from your cookie banner. Without a consent signal, nothing is sent.
  • No personal data. Order number, amount, currency and products (id, name, price, quantity). Names, emails and addresses are never read.

The widget script must be on the confirmation page too. If your path rules hide the chat there (for example /checkout*), tracking still works. Turn it off under Widget → Behavior → Analytics & sales.

Google Analytics 4 already set up

Nothing to do. The widget reads the standard GA4 purchase event from the dataLayer (Google Tag Manager, gtag(), or the GA4 apps for Shopify, WooCommerce and Magento), and the older Universal Analytics enhanced ecommerce format.

No GA4: push the purchase yourself

Order confirmation pagehtml
<script>
  window.dataLayer = window.dataLayer || [];
  dataLayer.push({ ecommerce: null });
  dataLayer.push({
    event: 'purchase',
    ecommerce: {
      transaction_id: 'ORDER-1001',       // required, unique per order
      value: 64.90,                       // order total
      currency: 'EUR',
      items: [
        // item_id: the product id or SKU from your catalog import
        { item_id: 'SKU-123', item_name: 'Vitamin C serum', price: 29.90, quantity: 1 },
      ],
    },
  });
</script>

Or call AskMerra.trackPurchase() with the same object ({ transaction_id, value, currency, items }). Reloading the page doesn’t count an order twice. If your dataLayer has another name, set AskMerraSettings.dataLayer.

Consent

With Google Consent Mode, the widget sends the purchase as soon as analytics_storage is granted, even if the shopper accepts on the confirmation page itself. If your cookie banner doesn’t use Consent Mode, tell the widget yourself:

// When the shopper accepts analytics in your cookie banner
AskMerra.setConsent({ analytics: true });

// or before the widget loads
window.AskMerraSettings = { consent: { analytics: true } };

Google Tag Manager events

When your page has a dataLayer, the widget pushes these events into it (switch them off under Widget → Behavior → Analytics & sales). Your Tag Manager decides what to forward, under your own consent settings. The text of messages is never pushed.

EventData
askmerra_openaskmerra_source: launcher, api or proactive
askmerra_message_sentnone
askmerra_product_clickGA4 ecommerce object with the product (item_id, item_name, price)
askmerra_add_to_cartGA4 ecommerce object with the product, currency and value
In Tag Manager, a custom event trigger on askmerra_add_to_cart with a GA4 event tag (event name add_to_cart, Send ecommerce data) shows assistant add-to-carts in your GA4 reports.