Back to docs

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:

  1. Custom Fields to attach any custom data to responses.
  2. 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'
  });
});
When using jQuery make sure you use $(window).on("load", function() { ... }) instead of $(document).ready(function() { ... })

Start today

Collect feedback from your most valuable source: your visitors and customers.

Get started for free

Free 30 day trial • No credit card required

Subscribe to Freddy's updates

Get insights on how to best get feedback from your customers, updates on new features and maybe even a joke from Freddy.