Surveys API

Description

This JS API provides you access to surveys. Access methods on this object:

productFruits.api.surveys

Methods

startSurvey(surveyId)

A string ID of the survey. You can get the survey ID from the URL in the Product Fruits administration - the last GUID parameter.

listen(callbackType, callback)

Listens to an event that occurs on surveys. See the next section to get event names. The listen method returns a function. If you want to clear the attached event handler, call that function. For example:

const disposeFn = productFruits.api.surveys.listen('survey-closed', yourCallback);

// ... later

disposeFn();

Events

survey-closed

Arguments:
surveyId - GUID of the survey
reason - either last_screen or close_button
answers - an array of answers, one answer object looks like:
    additText - for choice types of answer, if "Other" is selected, this will get the free text
    answer - the answer value, string|number|Array<string|number>
    questionId - question ID

Triggered when a user closes a survey. It can be triggered either by clicking on the close button or when the last question was answered.

Answer values are different based on the question type. E.g. multiple choices questions will get an array of strings (answer IDs), but the single-choice type will get only the string ID.

 

Tracking Full Answer Data

By default, Product Fruits does not automatically send all survey information, only answersCount is included (number).
To track full answer data (array of question objects including questionId, additText, and answer), along with surveyId, reason, and sessionId, you need to manually track this event using productFruits.api.events.track(...) and include all of the properties you want to track.

This code shows how to manually create a custom event that logs detailed answer data when a user closes (finishes or dismissed, not snoozes) a survey.

The script listens for the built-in survey-closed event - which provides the survey ID, close reason, and answer values, processes the answers into a cleaner format, and then emits a custom "survey_closed" event with that info, including step ID, question ID, selected answer(s), and "Other choice" answer text, if provided โ€” and makes sure it gets forwarded to integrations.

Example script: 

window.addEventListener("productfruits_ready", function () {
        window.productFruits.api.surveys.listen("survey-closed", (event) => {
          const { surveyId, reason, answers } = event;          const formattedAnswers = answers.map((a) => {
            const base = {
              stepId: a.stepId,
              questionId: a.qId,
              answer: a.answer,
            };            if (a.additText) {
              base.otherAnswer = a.additText;
            }            return base;
          });          window.productFruits.api.events.track(
            "survey_closed",
            {
              surveyId,
              reason,
              answers: formattedAnswers,
            },
            {
              forwardToIntegrations: true,
            }
          );
        });
      });

Was this article helpful?