Skip to main content

Method Signature

constructorio.quizzes.getQuizResults(quizId, parameters, networkParameters)
Retrieves personalized product recommendations and filter expressions based on the complete set of quiz answers. This method should be called after all quiz questions have been answered.

Parameters

quizId
string
required
The unique identifier of the quiz.
parameters
object
required
Parameters for the quiz results request.
networkParameters
object
Parameters relevant to the network request.

Returns

Returns a Promise that resolves to a QuizResultsResponse object.
request
object
The request parameters that were sent.
response
object
The response data containing results and metadata.
result_id
string
Unique identifier for this result set. Used for analytics tracking.
quiz_version_id
string
required
Version identifier for the quiz.
quiz_session_id
string
required
Session identifier for the quiz.
quiz_id
string
required
The identifier of the quiz.
quiz_selected_options
array
Information about the selected options and their matching status.

Examples

Basic Usage

const ConstructorIO = require('@constructor-io/constructorio-client-javascript');

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

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [
    [1, 2],      // First question: selected options 1 and 2
    [3],         // Second question: selected option 3
    ["true"],    // Third question: answered true
    ["seen"],    // Fourth question: acknowledged cover
  ],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
});

console.log(results.response.results);
// Array of recommended products

console.log(results.response.total_num_results);
// Total number of matching products

With Section Filter

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3], ["true"]],
  section: 'Women',
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
});

With Pagination

// Get first page with 20 results
const page1 = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
  page: 1,
  resultsPerPage: 20,
});

console.log(`Showing ${page1.response.results.length} of ${page1.response.total_num_results}`);

// Get second page
const page2 = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
  page: 2,
  resultsPerPage: 20,
});

With Additional Filters

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
  filters: {
    'Brand': ['Nike', 'Adidas'],
    'Color': ['Red', 'Blue'],
    'Price Range': ['$50-$100'],
  },
});

With Hidden Fields

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
  hiddenFields: ['internal_id', 'cost_price', 'supplier'],
});

// Access hidden fields in results
results.response.results.forEach(product => {
  console.log(product.data.internal_id);
  console.log(product.data.cost_price);
});

With Format Options

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
  fmtOptions: {
    hidden_fields: ['internal_id'],
    fields: ['id', 'title', 'price', 'image_url'],
  },
});

Complete Quiz Flow

const ConstructorIO = require('@constructor-io/constructorio-client-javascript');

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

// Step 1: Get first question
const firstQuestion = await constructorio.quizzes.getQuizNextQuestion('product-finder-quiz');
const quizVersionId = firstQuestion.quiz_version_id;
const quizSessionId = firstQuestion.quiz_session_id;

// Store answers as user progresses
const answers = [];

// Step 2: User answers first question
answers.push([1, 2]);

// Get next question
const secondQuestion = await constructorio.quizzes.getQuizNextQuestion('product-finder-quiz', {
  answers,
  quizVersionId,
  quizSessionId,
});

// Step 3: User answers second question
answers.push([3]);

// Get next question
const thirdQuestion = await constructorio.quizzes.getQuizNextQuestion('product-finder-quiz', {
  answers,
  quizVersionId,
  quizSessionId,
});

// Step 4: User answers third question
answers.push(["true"]);

// Step 5: All questions answered, get results
const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers,
  quizVersionId,
  quizSessionId,
  resultsPerPage: 24,
});

console.log(`Found ${results.response.total_num_results} products`);
console.log('Top recommendations:', results.response.results.slice(0, 5));

With Network Timeout

try {
  const results = await constructorio.quizzes.getQuizResults(
    'product-finder-quiz',
    {
      answers: [[1, 2], [3]],
      quizVersionId: 'abc123',
      quizSessionId: 'session456',
    },
    {
      timeout: 5000, // 5 seconds
    }
  );
} catch (error) {
  console.error('Request timed out or failed:', error);
}

Processing Results

const results = await constructorio.quizzes.getQuizResults('product-finder-quiz', {
  answers: [[1, 2], [3]],
  quizVersionId: 'abc123',
  quizSessionId: 'session456',
});

// Display products
results.response.results.forEach(product => {
  console.log('Product:', product.data.id);
  console.log('Name:', product.value);
  console.log('Price:', product.data.price);
  console.log('Matched terms:', product.matched_terms);
});

// Show available facets for further filtering
results.response.facets.forEach(facet => {
  console.log('Filter:', facet.name);
  console.log('Options:', facet.options);
});

// Track which quiz options were matched
results.quiz_selected_options.forEach(option => {
  if (option.is_matched) {
    console.log('Matched option:', option.value);
  }
});

Error Handling

try {
  const results = await constructorio.quizzes.getQuizResults('quiz-id', {
    answers: [[1, 2], [3]],
    quizVersionId: 'abc123',
    quizSessionId: 'session456',
  });
} catch (error) {
  if (error.message === 'quizId is a required parameter of type string') {
    console.error('Quiz ID is required');
  } else if (error.message === 'answers is a required parameter of type array') {
    console.error('Answers array is required');
  } else if (error.message === 'getQuizResults response data is malformed') {
    console.error('Invalid response from server');
  } else {
    console.error('Request failed:', error);
  }
}

Notes

  • The answers parameter is required and must be a non-empty array
  • Always use the quiz_version_id and quiz_session_id from your initial getQuizNextQuestion call
  • The hiddenFields parameter is a convenience that automatically merges into fmtOptions.hidden_fields
  • Use pagination (page and resultsPerPage) for large result sets
  • The result_id should be used for tracking user interactions with results
  • Additional filters can be applied on top of quiz logic using the filters parameter
  • The response will throw an error if quiz_version_id is not present, indicating a malformed response

API Reference

Build docs developers (and LLMs) love