Quiz
Page actions
Couldn’t copy. Open the Markdown to copy it manually.
Control a quiz with JavaScript
Add, open and close a quiz, and show the shopper’s quiz answers, from your theme’s own code.
When the Brandye app embed is on, every page of your theme gets a window.Brandye JavaScript API. Your theme developer can use it to place a quiz where the theme needs it, open or close it from custom code, and show the shopper’s answers on another page.
To open a floating quiz from a button or menu link, use a link instead. It needs no code.
Before you start
- Turn on the Brandye app embed. It loads the Brandye quiz script on every page.
- Run your code once the script is ready:
<script>
customElements.whenDefined("brandye-quiz").then(() => {
// window.Brandye is ready here.
});
</script>
Add a quiz to an element: mount
Brandye.mount(options) puts a quiz inside an element of your page. Calling it again for the same element updates that quiz instead of adding a second one.
| Option | What it does |
|---|---|
target |
The element, or a CSS selector for it. Required. |
mode |
"embedded" for a quiz inside the page (the default), "popup" for a floating widget quiz, or "full-page". |
questionnaire |
The quiz handle. It shows under the quiz’s name in its editor, after handle:. |
placement |
A placement handle, to show one exact placement. Find it on the placement’s card in Placement. |
open |
true starts the quiz right away. The default is true for "full-page" and false otherwise. |
The quiz shows only when it has an enabled placement of that type that can show on this page.
<div id="gift-quiz"></div>
<button type="button" onclick="window.Brandye.open('#gift-quiz')">Find my gift</button>
<script>
customElements.whenDefined("brandye-quiz").then(() => {
window.Brandye.mount({ target: "#gift-quiz", mode: "popup", questionnaire: "gift-quiz" });
});
</script>
Open or close a quiz: open and close
Brandye.open(target) and Brandye.close(target) take the element you passed to mount, its selector, or a <brandye-quiz> element on the page.
- On a floating widget quiz,
openopens the popup andclosecloses it. - On an embedded quiz,
openstarts the quiz andcloseshows its introduction again.
If no quiz is mounted on the target, both throw an error. Call mount first.
Show the shopper’s answers: getQuizAnswers
Brandye.getQuizAnswers(quizHandle) returns the quiz’s questions in quiz order, with the shopper’s answers from their last submission. Use it on a redirect page or any page on the same storefront domain.
Find a question by its handle and show its answer:
<p>Made for <span id="skin-type"></span> skin</p>
<script>
customElements.whenDefined("brandye-quiz").then(async () => {
const questions = await window.Brandye.getQuizAnswers("skin-quiz");
const skinType = questions.find((question) => question.handle === "skin-type");
document.getElementById("skin-type").textContent = skinType?.answers[0]?.label ?? "";
});
</script>
For a question with several answers, show them all with skinType.answers.map((answer) => answer.label).join(", ").
| Field | What it holds |
|---|---|
id |
The question ID. |
handle |
The question handle, set under Question & Answer Handles in the quiz editor. null when it has none. |
label |
The question, in the page’s language. |
options |
Every choice, each with id, handle and label. Choices are listed even when the shopper skipped the question. |
answers |
The shopper’s answers, each with value and label. For a choice, value is the option ID. For a text answer, both hold the shopper’s text. |
lead |
Only on contact detail questions: the firstName, lastName, email and phone the shopper gave. A field is listed only when the question asks for it and the shopper filled it in. |
answers is empty until the shopper submits the quiz. It is also empty for questions they didn’t answer, and for questions the quiz logic skipped on their path. A contact detail question has no options or answers, and its lead is empty until the shopper submits. An unknown quiz handle returns an empty list.
To greet the shopper by name:
const contact = questions.find(({ lead }) => lead);
greeting.textContent = contact?.lead.firstName ? `Hi ${contact.lead.firstName}` : "Hi";
Answers expire after 24 hours. With Redirect only, they are available only in the browser tab where the shopper took the quiz. Set answers with textContent, as in the example, because text answers are typed by shoppers.
Common troubleshooting
window.Brandye is undefined
Check that the Brandye app embed is on, and run your code inside customElements.whenDefined("brandye-quiz").
mount adds nothing to the page
Check that the quiz has an enabled placement of the mode you passed, and that it can show on this page. See Fix a quiz that does not show on your store.
Every answers list is empty
The shopper hasn’t submitted the quiz in this browser in the last 24 hours. With Redirect only, check the page in the tab where they took the quiz.