| [ Web Proxy ] |
| Viewing: https://developers.survicate.com/javascript/methods/#display-options | [Back] [Original] |
The Survicate JavaScript API allows you to trigger and adjust the behaviour of websites and web-app surveys.
JavaScript methods used to trigger Survicate surveys, except setVisitorTraits can only be used on some of the paid plans. Check our pricing page for more details. We refer to this feature as 'Advanced Targeting' or 'JavaScript Targeting'.
This section applies only to users, who decided to install Survicate tracking code either via the npm web surveys package, or npm wrapper.
Before calling any of the methods or appending event listeners, you need to initialize the Survicate JavaScript API.
Please note: In Web Surveys wrapper initSurvicate is asynchronous, so the user has to await its resolution. This should be done once, when initSurvicate hass been resolved, user can refer to getSurvicateInstance() as in provided example. Handling asynchronous code depends on the use case.
To set the visitor attributes asynchronously after the script is loaded, you can use the setVisitorTraits method.
Please note: The visitor attributes will be updated locally straight away, but the change will be visible in Survicate as soon as they answer any question.
Response traits are custom attributes tied to the current browser session. They are automatically included in survey answer payloads as part of the visit object and persist throughout the browser session. Response traits are stored in sessionStorage and are automatically cleared when the browser session ends.
For Widget surveys, you can set response traits using the setResponseTraits method. The method accepts an array of objects, where each object contains:
name (required): The name of the attributevalue (required): The value of the attribute (string, number, boolean, or Date)provider (optional): The source or integration that provided the attribute (e.g. "hubspot")You can also initialize response traits for Widget surveys by setting them before the Survicate script loads:
Survicate triggers the targeting script to determine if a survey should be displayed to a user. This happens during events like a page load, the completion of a survey, or a path change in the Single Page Application.
If you'd like to trigger an additional targeting script execution, you can use the retarget method.
This feature can be especially useful for Single Page Applications / Progressive Web Applications or the callbacks of asynchronous methods.
As soon as your user answers any survey's question, we assign a unique ID to them. Feel free to identify your users using that ID with the getVisitorId method
Please note: The output of this method can be null. As mentioned above, the value is assigned only to the visitors that have answered any question.
Using the destroyVisitor method you can remove a user's browser data. This erases data like session history and survey responses from their localStorage and sessionStorage. However, responses already saved in our database remain untouched and can still be viewed in the survey results tab.
Be cautious:
If a user matches a survey's targeting criteria, they might encounter the same survey again.
If you use this method while a survey is in progress, the survey will close and the user won't be able to complete it.
When using the destroyVisitor method, you can include a callback function to execute a specific action afterward. In this example, the callback function triggers the retarget method, which will check if there are surveys that should be shown to this 'new' user.
For the customers that need a custom targeting system, we provide the showSurvey method, that makes it easy to show an arbitrary survey based on the given survey's id.
The method ignores all targeting options, except checking whether this visitor has already answered the survey.
The method returns true if the survey was rendered and false otherwise (e.g. another survey is already displayed or the visitor has answered the desired survey).
Optionally, to change the default behavior of the survey, you can provide the options object as the second parameter.
| Property | Type | Description |
|---|---|---|
| forceDisplay | boolean | If true, currently rendered survey will be closed in favor of the new survey. Survey will be shown regardless of whether the user has already answered this survey. |
| displayMethod | string | Use this option in order to overwrite the current displaying configuration. Possible values: ['immediately', 'delayed', 'exitIntent', 'onScroll']. |
| displayOptions | object | See below for available options. Applicable and required only for the displayMethod values 'delayed' and 'onScroll'. |
| Property | Type | Description |
|---|---|---|
| delay | integer | Delay in seconds. Applicable and required only for displayMethod = 'delayed'. |
| scrolledPercentage | integer | Percentage of the page that was already scrolled. Applicable and required only for displayMethod = 'onScroll'. |
Use the closeSurvey method to programmatically close the currently displayed survey (widget or feedback button). This triggers the same close flow as when the user clicks the close button, including the survey_closed event and any configured integrations.
Optionally, pass a survey ID to close only a specific survey. This is useful when multiple widgets are displayed on the same page.
| Parameter | Type | Description |
|---|---|---|
| surveyId | string | Optional. If provided, only the survey with that ID is closed. If omitted, all open surveys are closed. |
The targeting script is automatically executed to determine if a user fits the specified criteria. We recognize that some users might prefer a different approach, such as using custom targeting with the showSurvey method. If you'd like to turn off this automatic feature, use the disableTargeting property.
If you need to display a survey after your site's visitor performs a specific action, use our event-based targeting. You can consider any action from your users' behavior as an event. For example, you might be interested in triggering a survey for those users who've just left the cart page without finishing the purchase. Or for those users who've just downgraded their subscription, etc.
In order to show surveys based on triggered events you can use invokeEvent method.
If only event name was provided in panel, pass it's name as an argument to invokeEvent method.
If also event properties were provided in panel, pass them as an object to invokeEvent method as second argument.
Important to note:
In advanced scenarios, you may want to programmatically submit answers to Survicate surveys without showing the survey widget to the user. This is possible using the hiddenSurveys, getSurveyPointsMetadata and submitAnswer method.
This feature works for Text, Single, Rating, Numerical, CSAT and NPS.
In order to accept multiple responses in a short time, you need to set your survey frequency to: "If the user has responded or closed the survey - Let the user take the survey multiple times on a recurring basis - Every time a respondent matches required criteria"
To prevent specific surveys from being displayed, pass their IDs to the Survicate tracking code using the hiddenSurveys property. This ensures the surveys are hidden from the user interface, but remain accessible via the API.
You can verify which surveys are hidden by checking:
Note: Hidden surveys will not appear on the page, but you can still interact with them programmatically.
Before submitting an answer, you need to know the available questions (points) and possible answers for a given survey. Use the getSurveyPointsMetadata method:
This returns an array of objects, each describing a survey point (question) and its possible answers:
pointId: The unique ID of the question.answerType: The type of answer expected (e.g., text, rating, single choice).answers: Optional, an array of possible answer IDs (for choice-based questions).Once you have the point and answer IDs, use the submitAnswer method to send a response:
Parameters:
params: Object containing surveyId, pointId, and either answerId (for choice-based questions) or answer (for text/NPS questions)responseUuid (optional): UUID string to group multiple submissions as part of the same response. If omitted, each submission creates a new response.Tip: For NPS questions, provide the score as the
answer(a number between 0 and 10).
You can group multiple submissions together as part of the same response by using a response UUID. This is useful when you want to submit multiple answers to the same survey and would like to see them in a single record in the Analyze tab.
Important: All submissions connected with the same response UUID must be from the same survey.
Use the getResponseUuid method to either:
Parameters:
surveyType: The survey type ('WidgetSurvey' or 'FeedbackButton')connectResponse (optional, default is false):
true: Returns the existing response UUID from an active, shown survey (requires at least one question to be answered)false or omitted: Generates and returns a new unique UUIDGenerate a UUID once and reuse it for multiple submissions:
Link programmatic submissions to a user's active survey session:
The API provides informative console warnings if something goes wrong, such as:
Survey with ID "7d9d103a77b389ss25" not foundPoint with ID "1319371113" not found in survey "7d9d103a77b38925"Answer ID "2418281111" not found in point "1319375" of survey "7d9d103a77b38925"Response UUID "a584d128-f331-401b-808d-61bbf8d9ca90" is not a valid UUIDGetResponseUuid: invalid-survey-type is not supported. Please use one of the following survey types: WidgetSurvey, FeedbackButton,Check the browser console for these messages during development and debugging.
All answers submitted via the API will be visible in the Survicate Analyze tab for your workspace.
hiddenSurveys only for surveys you intend to control programmatically.Use the setSurveyLanguage method to dynamically change the survey display language after the SDK has been initialized. This is useful when the user changes their language preference in your application.
Parameters:
| Parameter | Type | Description |
|---|---|---|
| languageTag | string | A valid ISO 639-1 language code (e.g., en, de, fr, es, pt). |
Important to note:
forcedLanguage configuration option instead.<html lang> attribute up to date (typical for localized SPAs), you can enable the useHtmlLangAttribute option instead of calling this method on every language change Survicate then follows the attribute automatically.Use the setThemeMode method to control whether surveys use light or dark theme. This is useful when you want to match your website's theme.
The default theme mode is 'auto'. Meaning that the survey will use the browser's preference for the theme.
For the dark theme to be applied, the survey's theme must have dark mode variant configured in the Survicate panel.
To set the theme during initialization, use the themeMode configuration option
Parameters:
| Parameter | Type | Description |
|---|---|---|
| mode | string | Theme mode: "light" or "dark" (case-insensitive). |
Important to note:
"light", "Light", "LIGHT" all work the same way)."light" and "dark" modes are available via this method.| Web Proxy Viewer | New URL | Original Page |