Skip to main content
The Quizzes module provides methods to build interactive product recommendation quizzes that guide users to relevant products through a series of questions.

Getting Started

The quizzes module is available on your Constructor.io client instance:
const ConstructorioClient = require('@constructor-io/constructorio-client-javascript');

const constructorio = new ConstructorioClient({
  apiKey: 'YOUR_API_KEY',
});

// Access the quizzes module
constructorio.quizzes.getQuizNextQuestion('beauty-quiz');

Quiz Flow

A typical quiz flow consists of:
1

Load First Question

Call getQuizNextQuestion without answers to get the first question
2

Collect Answers

Present questions to users and collect their responses
3

Get Next Questions

Pass collected answers to get subsequent questions
4

Retrieve Results

Call getQuizResults with all answers to get recommended products

Methods

getQuizNextQuestion

Retrieve the next question in a quiz based on previous answers.
// Get the first question (no answers yet)
constructorio.quizzes.getQuizNextQuestion('beauty-quiz', {})
  .then((response) => {
    const { quiz_version_id, quiz_session_id, next_question } = response;
    
    // Store these IDs for subsequent requests
    console.log(next_question);
  });

Parameters

quizId
string
required
The unique identifier of the quiz (configured in your Constructor.io dashboard)
parameters
object
Additional parameters

getQuizResults

Retrieve product recommendations and filter expressions based on quiz answers.
constructorio.quizzes.getQuizResults('beauty-quiz', {
  answers: [[1, 2], [0], ["true"]],
  quizVersionId: 'v1',
  quizSessionId: 'session-123'
})
  .then((response) => {
    console.log(response.response.results);
  });

Parameters

quizId
string
required
The unique identifier of the quiz
parameters
object
required
Parameters including answers and optional refinements

getQuizResultsConfig

Retrieve configuration for the results page of a quiz.
constructorio.quizzes.getQuizResultsConfig('beauty-quiz', {
  quizVersionId: 'v1'
})
  .then((response) => {
    console.log(response);
  });

Parameters

quizId
string
required
The identifier of the quiz
parameters
object

getQuizAllQuestions

Retrieve all questions for a quiz at once (useful for showing quiz progress or building custom flows).
constructorio.quizzes.getQuizAllQuestions('beauty-quiz', {
  quizVersionId: 'v1',
  quizSessionId: 'session-123'
})
  .then((response) => {
    console.log(response.questions);
  });

Parameters

quizId
string
required
The identifier of the quiz
parameters
object

Complete Quiz Example

class QuizManager {
  constructor(quizId) {
    this.quizId = quizId;
    this.answers = [];
    this.versionId = null;
    this.sessionId = null;
  }
  
  async start() {
    // Get the first question
    const response = await constructorio.quizzes.getQuizNextQuestion(this.quizId, {});
    
    this.versionId = response.quiz_version_id;
    this.sessionId = response.quiz_session_id;
    
    return response.next_question;
  }
  
  async answerQuestion(answer) {
    // Store the answer
    this.answers.push(answer);
    
    // Get next question
    const response = await constructorio.quizzes.getQuizNextQuestion(this.quizId, {
      answers: this.answers,
      quizVersionId: this.versionId,
      quizSessionId: this.sessionId
    });
    
    return response.next_question;
  }
  
  async getResults() {
    // Get final recommendations
    const response = await constructorio.quizzes.getQuizResults(this.quizId, {
      answers: this.answers,
      quizVersionId: this.versionId,
      quizSessionId: this.sessionId,
      resultsPerPage: 24
    });
    
    return response.response.results;
  }
}

// Usage
const quiz = new QuizManager('beauty-quiz');

// Start quiz
const firstQuestion = await quiz.start();
displayQuestion(firstQuestion);

// User answers question
const nextQuestion = await quiz.answerQuestion([1, 2]); // Multiple choice
displayQuestion(nextQuestion);

// After all questions
const results = await quiz.getResults();
displayResults(results);

Answer Formats

User can select multiple options:
// User selected options at index 1 and 2
answers: [[1, 2]]

Events

The quizzes module dispatches custom events on the browser window when requests complete:
window.addEventListener('cio.client.quizzes.getQuizNextQuestion.completed', (event) => {
  console.log(event.detail);
}, false);

window.addEventListener('cio.client.quizzes.getQuizResults.completed', (event) => {
  console.log(event.detail);
}, false);

window.addEventListener('cio.client.quizzes.getQuizResultsConfig.completed', (event) => {
  console.log(event.detail);
}, false);

window.addEventListener('cio.client.quizzes.getQuizAllQuestions.completed', (event) => {
  console.log(event.detail);
}, false);

Best Practices

  • Store quiz_version_id and quiz_session_id from the first request and pass them with all subsequent requests
  • Use skipTracking: true when preloading the first question for better UX
  • Track quiz completions and drop-off points to optimize your quiz flow
  • Allow users to skip questions or go back to previous questions
  • Show progress indicators to encourage completion

Build docs developers (and LLMs) love