Advanced widget functionality
Note that for any of the window.duet
functions to work, you must wait until the primary <script>
is loaded. Consult the intro guide to see how to do that.
Passing in Metadata
If you want to send arbitrary metadata (e.g. other details about the user), you may call window.duet.setMetadata({...})
. Anything sent as metadata will be passed to Decagon servers and will be visible in the admin console for each conversation. For example:
If you are setting user email and id via metadata, please use the fields email
and userId
respectively.
Note that you may pass in user tokens with the special token
field, which will be redacted when stored in the database.
You may call setMetadata
multiple times if you'd like but know that each time you call it, the metadata will be set to exactly what you pass in. So if you drop fields in subsequent calls, they will be removed from the metadata. This is to give you control over deleting fields if you want to.
Tracking events
If you want to track events, you may set a listener via window.duet.setEventListener
so that your callback is called every time there is a meaningful user action.
For example:
Here are the events we currently support:
User clicks the icon to open a chatbot window:
{eventName: "DUET_INITIALIZE"}
User closes the chatbot window:
{eventName: "DUET_DEINITIALIZE"}
User begins typing:
{eventName: "DUET_USER_INPUT"}
User sends a message:
{eventName: "DUET_USER_SEND", question: "<The query that the user sent>"}
Bot responds:
{eventName: "DUET_RESPONSE"}
Dynamically opening/closing the widget
This should rarely be needed, but you can dynamically open/close the widget using these functions:
Optionally, you may also specify that when the widget is closed, it should be hidden. Note that this will not affect the widget when it is open. You can toggle the hidden mode like so:
Send a proactive message (or set the welcome message)
Use this to customize the initial message in a new conversation with a user. Set the 2nd argument to try if you want to trigger a proactive message bubble above the chat widget to invite the user to chat. The message will them appear as the welcome message in the chat when the widget is opened.
Pre-populate the initial message for the user
If you want to pre-populate a message in the user's input box (for example, if they are coming from another product flow), you may use the following command.
Setting a Metadata Request Callback
The window.duet.setMetadataRequestListener
function allows you to handle metadata requests dynamically by setting a callback function. This function is called whenever there's a need to fetch and return metadata based on specific criteria or actions within the widget.
API Definition:
window.duet.setMetadataRequestListener(function(callback))
: Registers a callback function that will be invoked with two arguments: payload
and callback
. The payload
object contains the details of the metadata request, and the callback
function is used to return the response to the widget.
Usage:
You can set a callback function that will be executed to handle data requests. The callback receives two arguments: a payload object that contains details about the request, and a callback function to send the response back to the widget.
Example:
Here's how you can set up a listener for metadata requests related to user orders:
This function listens for a subtype
of 'orders' in the metadata request. If detected, it fetches the order details from a specified URL. The fetched data is then returned via the callback function, where the orders
key contains the data retrieved and ok
indicates the success status of the operation.
This allows you to set "metadata requests" so that the AI can make requests through your frontend. In the example, Decagon asks for "orders". Your frontend can then make the request to the relevant endpoint for orders (using your existing auth framework) and then just pass the response back to Decagon. You can create as many subtypes as you want.
Last updated