Explore — Events
Run your own code when Explore loads its questions and when the reader picks one.
The Explore workflow emits an event at each step. Below we listen to one, list
the events a page load goes through, and then look at the data event, which
tracks the response itself.
You need a working Explore page first. See Explore — Quick Start.
Listen to an event
Call on on the workflow with the event name and a function. Add the listener
before start(), so it catches the first events:
const off = client.ui.explore.on("done", () => {
console.log("Questions shown");
});
on returns a function. Call it to remove the listener:
off();
The lifecycle events and data also reach client.ui.explores, the context,
with the workflow that emitted them in workflow. Use the context for a page
with more than one Explore unit. select reaches the workflow only.
Lifecycle events
start() sends the request, and the events follow in this order. Each one
fires at most once:
| Event | Fires when |
|---|---|
request | The SDK sends the request to Miso. payload is the request body. |
loading | The request is out, and the SDK waits for the questions. |
ready | The questions show. |
done | The questions show. Explore emits it right after ready. |
interrupt | You called restart() before the questions arrived. |
error | The request failed. |
finally | The request has ended, in any of the three ways above. |
The request always ends with one of done, interrupt and error, and then
with finally. Explore shows all its questions at once, so ready and done
come together. Listen to either one.
Apart from request, these events carry:
| Property | What it holds |
|---|---|
session | The session of the request. |
status | loading, ready or erroneous. |
Select event
select fires when the reader clicks a related question, before the SDK
follows the link:
client.ui.explore.on("select", ({ question }) => {
console.log("Question picked", question.text);
});
question.text is the question. The SDK then sends the reader on through your
useLink function.
The SDK reports the click to Miso by itself. Listen to this event only to send it somewhere else as well, such as to Google Analytics 4.
A question that the reader types in the search box goes to the answers page
with no select event.
The data event
data fires each time the data of the workflow changes: when the workflow
starts, when the request goes out, and when the response arrives.
client.ui.explore.on("data", ({ status, value }) => {
if (status !== "ready") {
return;
}
console.log(value.related_questions.length, "questions");
});
The event carries the data object:
| Property | What it holds |
|---|---|
session | The session of the request. |
status | initial, loading, ready or erroneous. |
request | The request that the SDK sent, with the body in request.payload. |
value | The Related Questions API response. |
error | The error, when status is erroneous. |
Each item in value.related_questions is an object with text and url. The
SDK turns the strings in the response into these objects before anything else
reads them.
For one request, data fires in this order:
status: "initial"when the workflow starts, with norequestyet.status: "loading"when the request goes out.status: "ready"when the response arrives.
If the request fails, the last one has status: "erroneous" instead.
The event shows the data after each
data processor has run, so a
field you changed there arrives changed here. The event cannot change the data.
To change what Explore shows, use useDataProcessor.
If it does not work
| Symptom | Cause |
|---|---|
select never fires | The listener is on client.ui.explores. Set it on client.ui.explore. |
request and loading never fire | The listener is added after start(). Add it before. |
value is undefined in data | The response has not arrived yet. Check status first. |
question.length is undefined | Each question is an object. Read question.text. |
| Nothing fires and an error shows in the console | The function throws. The SDK logs the error and goes on. |
Next
- API Configuration — change the request and the response.
- Explore — Google Analytics 4 — send the pick to GA4.
- Ask — Events — the events on the answers page.
