Intuify

Intuify

Documentation

Logic & flow2 min read

Custom JS examples

Copy-paste recipes for the most common custom JS patterns.

On this page (10 sections)

Copy-paste recipes for common patterns. Every snippet expects to run on the respondent page (live or preview) after the page has finished rendering — open the browser console while a question is on screen and try them out.

Inspect the current page#

js
const api = window.Intuify.survey;
console.log({
  screen: api.getScreenType(),
  progress: api.getProgress(),
  questions: api.getCurrentQuestions().map((q) => q.id),
});

Read a question's choices#

js
const api = window.Intuify.survey;
api.getChoices("q_color").forEach((c) => console.log(c.id, c.text));

For matrix questions, use getRows / getColumns instead.

Pre-fill an answer from a URL parameter#

js
const api = window.Intuify.survey;
const { urlParams } = api.getEmbeddedData();
if (urlParams.color) {
  api.setResponse("q_color", urlParams.color);
}

Set a survey variable#

js
window.Intuify.survey.setVariable("source", "tag_manager");
// or, equivalently:
window.Intuify.setVariable("source", "tag_manager");

Variables persist across pages for the active session and feed back into piping (${e://Field/source}) and conditional logic.

Resolve piped text in JS#

js
const api = window.Intuify.survey;
const greeting = api.resolveText("Hello ${e://Field/name}, welcome back!");
document.title = greeting;

Read the loop iteration#

When the current section is inside a loop-and-merge block, getMetadata() shows the active navPosition and getResponseKey returns the loop-scoped key:

js
const api = window.Intuify.survey;
console.log("key", api.getResponseKey("q_satisfaction"));
// → "item_pasta::q_satisfaction" inside the Pasta iteration

Inspect a plugin question payload#

Useful when porting Qualtrics widgets:

js
const api = window.Intuify.survey;
const payload = api.getQuestionPayload("q_grid_choice");
console.log(payload.pluginConfig, payload.answers, payload.columns);

payload is the exact same object the standalone widget iframe receives via postMessage (initQuestion).

Listen for the next page#

The runtime API recomputes per page, so the simplest way to react to the respondent advancing is to compare the previous and current session metadata in a small polling loop or via a MutationObserver on the survey container. The dedicated onLoad / onSubmit hooks will remove this boilerplate — track it via the Custom JS overview.

Common pitfalls#

  • Question id vs. qid. The runtime API uses the engine question id (the value stored in the survey JSON's id field, e.g. q_color). The shorter qid you see in the response export (Q1, Q2, …) is not accepted.
  • Calling getters too early. If you run a script before the page hydrates, window.Intuify may be undefined. Wait for the first render (or rely on onLoad once it ships).
  • Mutating returned arrays. getChoices, getRows, etc. return fresh arrays. Mutating them does not change the survey — call setResponse or update the survey definition instead.
  • setResponse does not navigate. Use it to seed answers; Next still has to be clicked (or trigger an auto-advance rule).

Was this page helpful?