Overview

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'.

Using NPM packages

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.

Methods

Set visitor attributes

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.


Retarget

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.


Get visitor ID

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.


Reset visitor

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.


Show survey

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.

options

PropertyTypeDescription
forceDisplaybooleanIf 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.
displayMethodstringUse this option in order to overwrite the current displaying configuration. Possible values: ['immediately', 'delayed', 'exitIntent', 'onScroll'].
displayOptionsobjectSee below for available options. Applicable and required only for the displayMethod values 'delayed' and 'onScroll'.

displayOptions

PropertyTypeDescription
delayintegerDelay in seconds. Applicable and required only for displayMethod = 'delayed'.
scrolledPercentageintegerPercentage of the page that was already scrolled. Applicable and required only for displayMethod = 'onScroll'.

Disabling automatic targeting

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.