Ask AI Widget

The Ask AI widget adds an Ask AI button to your pages. When a customer clicks it, the Genius Chat Widget opens with a message about the product they're viewing, such as I'd like to talk about Trail Runner 2, so Purchase AI can pick up the conversation.

It can be shown in three ways, set with mode:

  • Tab (the default): docked to the right-hand edge of the page.
  • Floating button: a pill at the bottom centre of the screen.
  • Button: placed wherever you put it on the page, such as in your site header.

All three take your page's background colour, with bold black text and a border that fades from your brand colour on the left to black on the right. When a customer hovers over it, it fills with a pale tint of your brand colour and a highlight sweeps once along the border. See Styling.

For an icon only tab with no text, see the AI Tab widget.

Prerequisites

  • Your DigitalGenius widget ID.
  • Your region: eu or us.
  • The Genius Chat Widget on the same page. The widget opens the chat to send its message. See Integrating Genius Chat Widget.
  • A Purchase AI flow that responds to conversations started from the widget. See Conversation starter.
  • Access to edit the HTML of the page the widget is displayed on.

Installation

Add the following code to your product page, after the Genius Chat Widget. Replace your-widget-id with your widget ID, update env to match your region if required, and replace the product name with the one output by your page template.

<script>
  window.DG_SDK_CONFIG = {
    components: [
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'askAi',
        config: {
          productName: 'Trail Runner 2',
        },
      },
    ],
  };
</script>
<script src="https://chat.digitalgenius.com/init-sdk.js"></script>

The tab is docked to the right-hand edge of the page, so it doesn't need a container. targetDOMNode is only used by the button.

Configuration

The following properties are set on each entry in components.

PropertyTypeRequiredDescription
widgetIdstringYesYour DigitalGenius widget ID.
envstringYesYour region: 'eu', 'us', or 'dev.us'.
typestringYesMust be 'askAi'.
targetDOMNodestringFor the buttonOnly when mode is 'inline'. A CSS selector for the element the button is rendered into.
config.mode'tab' | 'floating' | 'inline'NoHow the widget is displayed. Defaults to 'tab'. See Floating button and Button.
config.productNamestringYes, for the tabThe product name, or a CSS selector for the element containing it when retrievalMethod is 'selector'. Added to the end of lang.message.
config.retrievalMethod'config' | 'selector'NoHow the product name is provided. Defaults to 'config'. See Providing the product name.
config.productNameExcludestringNoOnly when retrievalMethod is 'selector'. A CSS selector for elements inside the productName element whose text is left out, such as screen reader only text.
config.labelIntervalnumberNoFloating button only. How long each label is shown, in milliseconds, before the next one. Defaults to 10000.
config.langobjectNoOverrides the widget's text. See Customising text.
config.style.backgroundColorstringNoThe background colour. Defaults to your page's background colour, or white if your page has no background colour.
config.style.textColorstringNoThe text and icon colour. Defaults to black, or white if the background is dark.
config.style.accentColorstringNoYour brand colour, where the border gradient starts on the left. Defaults to a colour found on your page. See Styling.
config.style.borderColorstringNoA plain border colour, used instead of the gradient.
config.style.topstringNoThe vertical position of the centre of the tab, as any CSS length, for example '40%' or '320px'. Defaults to '50%', the middle of the screen.
config.style.bottomstringNoFloating button only. The distance from the bottom of the screen, as any CSS length. Defaults to '1.5em'.
config.style.radiusstringNoThe corner radius, as any CSS length, for example '0' or '0.5em'. Defaults to 1.5em for the tab, which rounds its ends, and fully rounded for the floating button and button.

Providing the product name

retrievalMethod controls where the product name comes from.

Example: value in the configuration

With retrievalMethod set to 'config' (the default), productName is the product name itself. Use this when your page template can output the product name into the script:

<script>
  window.DG_SDK_CONFIG = {
    components: [
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'askAi',
        config: {
          retrievalMethod: 'config',
          // replace with the product name output by your page template
          productName: 'Trail Runner 2',
        },
      },
    ],
  };
</script>
<script src="https://chat.digitalgenius.com/init-sdk.js"></script>

Example: value read from the page

With retrievalMethod set to 'selector', productName is a CSS selector for an element already on the page, and the widget reads the product name from it. Use this when the product name is displayed on the page but can't easily be added to the script:

<h1 class="product-title">Trail Runner 2</h1>

<script>
  window.DG_SDK_CONFIG = {
    components: [
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'askAi',
        config: {
          retrievalMethod: 'selector',
          productName: '.product-title',
        },
      },
    ],
  };
</script>
<script src="https://chat.digitalgenius.com/init-sdk.js"></script>

The product name is read when the customer clicks the tab, so elements rendered after the widget loads, or changed when a customer picks a different variant, are supported. For form fields (input, select and textarea) the field's value is used; for other elements, their text content is used, with surrounding spaces removed.

Leaving out hidden text

Some product name elements contain extra text for screen readers that isn't shown on screen, for example:

<h1 class="product-name">
  <span class="accessibility-hidden">details about product:</span>
  Pure Linen Polo - Olive
</h1>

The widget reads all the text in the element, so set productNameExclude to a CSS selector for the elements inside it to leave out:

config: {
  retrievalMethod: 'selector',
  productName: 'h1.product-name',
  productNameExclude: '.accessibility-hidden',
},

The product name read is then Pure Linen Polo - Olive. Line breaks and repeated spaces in the element's text are always collapsed into single spaces.

Localisation

The widget is displayed in British English by default. To change the language, set locale in your configuration:

window.DG_SDK_CONFIG = {
  locale: 'fr-FR',
  components: [
    {
      widgetId: 'your-widget-id',
      env: 'eu',
      type: 'askAi',
      config: {
        productName: 'Trail Runner 2',
      },
    },
  ],
};

The following locales are supported: ca-ES, cs-CZ, da-DK, de-DE, en-GB, en-LU, en-US, es-ES, et-EE, fi-FI, fr-FR, it-IT, ja-JP, ko-KR, lt-LT, lv-LV, nl-NL, no-NO, pl-PL, pt-PT, ro-RO, sk-SK, sv-SE, zh-CN and zh-TW. Unsupported locales fall back to British English.

Customising text

The text listed below can be customised using a lang object in the app's config. Only the properties you specify are overridden; all other text uses the default wording for the selected locale.

window.DG_SDK_CONFIG = {
  components: [
    {
      widgetId: 'your-widget-id',
      env: 'eu',
      type: 'askAi',
      config: {
        productName: 'Trail Runner 2',
        lang: {
          buttonLabel: 'Need a hand?',
          message: 'Can you help me decide on the',
        },
      },
    },
  ],
};
PropertyDefault (en-GB)Location
buttonLabelAsk AIThe text on the tab, the button, and the floating button if floatingLabels isn't set.
floatingLabelsNot setA list of texts the floating button cycles through, for example ['Ask AI', 'Shop with AI']. Replaces the default list rather than adding to it.
messageI'd like to talk aboutThe start of the message sent to the chat. The product name is added after it, separated by a space.

For example, with the configuration above, the customer's first message is Can you help me decide on the Trail Runner 2. Your Purchase AI flow doesn't need to recognise this wording. The conversation opens with a conversation_starter chat event with source set to ask_ai and action set to product_enquiry, and the product name in data.productName when one is set, so branch on those instead:

{
  "from": "customer",
  "type": "chat_event",
  "payload": {
    "name": "conversation_starter",
    "source": "ask_ai",
    "action": "product_enquiry",
    "text": "Can you help me decide on the Trail Runner 2",
    "data": { "productName": "Trail Runner 2" }
  }
}

Floating button

Set mode to 'floating' to show the widget as a pill at the bottom centre of the screen instead of a tab.

productName is optional in floating mode, so the button can be used on any page. If it's set, it's added to the message in the same way as the tab.

To switch between several texts, set floatingLabels in config.lang. Each label is shown for labelInterval milliseconds.

window.DG_SDK_CONFIG = {
  components: [
    {
      widgetId: 'your-widget-id',
      env: 'eu',
      type: 'askAi',
      config: {
        mode: 'floating',
        lang: {
          floatingLabels: ['Ask AI', 'Shop with AI'],
        },
        style: {
          bottom: '2em',
        },
      },
    },
  ],
};

Make sure the button doesn't cover the Genius Chat Widget launcher or other fixed elements at the bottom of your page. Use style.bottom to move it up if needed.

Button anywhere on the page

Set mode to 'inline' to render the button into an element on your page, such as your site header, instead of fixing it to the edge or bottom of the screen. Set targetDOMNode to a CSS selector for that element:

<header>
  <a href="/">Your shop</a>
  <nav>…</nav>
  <div id="ask-ai"></div>
</header>

<script>
  window.DG_SDK_CONFIG = {
    components: [
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'askAi',
        targetDOMNode: '#ask-ai',
        config: {
          mode: 'inline',
        },
      },
    ],
  };
</script>
<script src="https://chat.digitalgenius.com/init-sdk.js"></script>

As with the floating button, productName is optional. Set it on product pages to include the product in the message.

Using more than one

You can add more than one to the same page, each as its own entry in components. For example, the AI Tab on the edge of the page and an Ask AI button in your header:

<script>
  window.DG_SDK_CONFIG = {
    components: [
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'aiTab',
        config: {
          productName: 'Trail Runner 2',
        },
      },
      {
        widgetId: 'your-widget-id',
        env: 'eu',
        type: 'askAi',
        targetDOMNode: '#ask-ai',
        config: {
          mode: 'inline',
          productName: 'Trail Runner 2',
        },
      },
    ],
  };
</script>
<script src="https://chat.digitalgenius.com/init-sdk.js"></script>

Avoid using the AI Tab and the Ask AI tab (mode: 'tab') together, as both are docked to the right-hand edge of the page and overlap.

Behaviour

  • Product name: if productName isn't set, the tab isn't shown and a warning is logged to the browser console. The floating button and button are still shown and send message on its own.
  • Loading: the widget appears as soon as it loads. If it's clicked before the Genius Chat Widget has loaded, nothing happens and a warning is logged to the browser console.
  • Clicking: opens the chat widget and sends the message as the customer's first message. While the assistant is replying, the widget is disabled.
  • Position: the tab and floating button stay in place as the customer scrolls, and appear behind the chat widget when it's open. The button scrolls with the element it's in.

Styling

The widget renders inside a shadow DOM, so the styles on your page don't affect it directly. By default it inherits your page's font and background colour, with black text, or white text on a dark background. Set style.textColor to use a different text colour. Sizes are set in em, so it scales with your page's font size.

The border is a subtle gradient running from your brand colour on the left to black on the right (on the tab, from the bottom to the top, in the direction the text reads). The brand colour is the first coloured button background, or button or link text, found on your page. If that isn't the right colour, set it with style.accentColor, or set style.borderColor for a plain border instead. When a customer hovers over the widget, its background turns a pale tint of the brand colour (or of borderColor, if set), and a band of light sweeps once along the border unless their device is set to reduce motion.

config: {
  productName: 'Trail Runner 2',
  style: {
    accentColor: '#1f3d2b',
    top: '40%',
    radius: '0',
  },
},

Troubleshooting

If the tab does not appear, check that:

  • type is set to 'askAi'.
  • productName is set and isn't empty.
  • For the button, mode is 'inline' and targetDOMNode matches an element on the page. An error is logged to the browser console if it doesn't.
  • widgetId is correct and env matches your region.

If clicking the tab or button does nothing, check that the Genius Chat Widget is on the page and loads without errors. A warning is logged to the browser console when it isn't available.

If the message doesn't include the product name, check the browser console. When retrievalMethod is 'selector', an error is logged if the selector doesn't match an element on the page.


Did this page help you?