Skip to content

Add custom JavaScript to surveys

Use JavaScript in survey content to track events, embed external tools, or connect participants to another application. Your code runs in the participant’s browser.

  1. Add a Page Text question where the integration should run.
  2. Switch the text editor to HTML view and add your markup and <script> tags. Scripts can be inline or loaded from an HTTPS URL.
  3. Save the question and open the Test Previewer to test it. The builder’s inline preview does not execute scripts.

Replace the .example URLs and study labels in the examples with your own values. Keep scripts that should run once per participant in a non-sample questionnaire; a sample questionnaire can run for each sample.

This script sends a form-encoded POST to your tracking endpoint when the page renders:

<script>
navigator.sendBeacon(
"https://tracking.example/events",
new URLSearchParams({
event: "external-task-viewed",
study: "product-evaluation"
})
);
</script>

The receiving endpoint must accept browser requests. Check its logs to confirm receipt. Reloading or returning to the page can send the event again, so account for repeat visits in your tracking logic. For recorded survey outcomes, use webhooks.

Add the provider’s HTML and JavaScript to the Page Text question. This might be a recording widget, an interactive task, or another application with an embeddable interface. For a tool that supports iframe embedding:

<iframe
class="external-task"
src="https://tools.example/task"
title="Product evaluation task"
width="100%"
height="480">
</iframe>

The provider must allow its page to be embedded on your survey domain. Follow its integration instructions for initialization, participant identifiers, and completion callbacks. When initialization needs an element from the question’s HTML, wait until that element is rendered.

Data collected by an embedded tool follows that tool’s storage rules. Saving a result as a RedJade response requires an explicit integration; displaying a widget does not save its output as an answer. Use the provider’s completion signal to confirm a task or upload finished.

For a fixed destination, add a link to the Page Text question:

<a class="external-task-link" href="https://tasks.example/start">
Open task
</a>

Use JavaScript’s window.location.assign(destination) when your integration chooses the destination at runtime. Opening an external page does not complete the questionnaire. Provide a return link to the original survey entry URL, preserving the participant’s external ID when one was supplied. To keep the survey open, use target="_blank" and rel="noopener" on the link.

A minimum-time control delays advancement from the RedJade page; it does not verify that an external task finished. For completion, termination, and quota outcomes, configure survey redirect links.

Use {{participant_external_id}} to match a provider’s participant, {{sample_code}} to display the current sample, or an answer-piping variable to reuse an earlier response. The survey variables reference lists built-in variables, custom fields, and question variables, with text, URL, and JavaScript examples.

Keep API keys and other server credentials out of survey scripts. Browser code and its requests are visible to participants.

Use the Test Previewer for rendering and script checks, then test a participant session through the survey entry URL, including advancing, going back, reloading, and resuming. The previewer does not save responses or reproduce live session resumption.

Scripts can run again when a page is revisited; avoid registering duplicate handlers or creating duplicate widgets. Check the external system for the expected event or result. Test camera or microphone permissions when the integration uses them.