Category: developer
Setting survey options programmatically
Use the options object to set specific options for your surveys.
Available options
There are two options that you can set:
- Custom Fields to attach any custom data to responses.
- URL to set the current URL manually. This is done by the script automatically, but you need to do this manually if you're using a SPA
Two ways to set options: show
and setOptions
If you've set the widget to trigger programmatically, you'll use the show
method. If you have any other trigger, like on page load or if you use the side sticky widget, you can use the setOptions
method.
The setOptions
method
Make sure to pass the data after the Freddy script is loaded. This can be achieved by wrapping the setOptions
method in the load
event:
window.addEventListener('load', function() {
freddyWidget.setOptions({
custom_fields: {
user_id: 42,
email: 'customer@example.com'
},
url: 'https://www.yourapp.com/dashboard/settings'
});
});
The show
method
If you've set your widget to trigger programmatically then you can trigger the widget and pass custom fields in one go using the show
method. Here's an example of triggering the survey on a button click:
document.getElementById('my-button').addEventListener('click', function() {
freddyWidget.show({
custom_fields: {
user_id: 42,
email: 'customer@example.com'
},
url: 'https://www.yourapp.com/dashboard/settings'
});
});
$(window).on("load", function() { ... })
instead of $(document).ready(function() { ... })