---
title: "Show a survey from code"
description: "Open a website survey with userloop.show() or your own event, choose how it appears, embed it inside a page element, and close it."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.userloop.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Show a survey from code

Your code can open a published website survey at the moment that suits your site: when a visitor selects a button, finishes a task or reaches a step of your app. Call `userloop.show()` to open a survey directly, or `userloop.track()` to tell UserLoop an event happened and let the survey's own rules decide.

## Before you start

- **The SDK is on your site.** See [Install the SDK](https://docs.userloop.io/developers/sdk/install/).
- **The survey is a website survey, and published.** The SDK only knows the site's live website surveys. A change on the **Targeting** or **Design** stage reaches the site when it is published, within two minutes.

## Find the survey's ID

`show()` takes the survey's ID. In the survey editor, open the **More** menu (⋯) and select **View responses**. The address of the page that opens ends `?survey=` and the ID, such as `?survey=k17aw2c5x8n0m3b6v9t1r4y7z0h3d6f9`.

You can also list the IDs of every live website survey on the site from the browser console:

```js
fetch("https://s.userloop.io/api/site/SITE_KEY")
  .then((r) => r.json())
  .then((site) => console.table(site.surveys.map((s) => ({ id: s.survey_id, title: s.survey }))));
```

You should see a table of IDs and survey titles. A survey that isn't in it isn't published as a website survey of this site.

The examples on this page use `SURVEY_ID`.

## Open a survey when your code says

Set the survey to open only from code, then call `show()`.

1. **Choose From code**

   In the survey editor, open the **Targeting** stage. Under **When**, select **From code**. The survey then never opens by itself.
2. **Publish the survey**

   Select **Publish**, then **Publish** again in the dialog.
3. **Call show() where it should open**

   For example, on a button:

   ```html
   <button id="give-feedback">Give feedback</button>
   <script>
     document.getElementById("give-feedback").addEventListener("click", function () {
       userloop.show("SURVEY_ID");
     });
   </script>
   ```

   Select the button. The survey opens as its **Design** stage says, for example as a popup in the bottom right.

`show()` skips the survey's pages, its **Who** rules, **Share of visitors**, its trigger and **One survey per visit**. It still honours:

- **Your site's addresses**: on an address that isn't listed, nothing opens.
- **How often**: a visitor who has already seen or answered the survey may not see it again. Pass `force: true` to skip this too.
- **One at a time**: while another survey is open, `show()` does nothing. Close it first with `userloop.close()`.

`show()` works on a survey with any trigger, not only **From code**. The survey's own trigger still runs as well, so use **From code** when only your code should open it.

```js
// Opens even if this visitor has already seen or answered it.
userloop.show("SURVEY_ID", { force: true });
```

`show()` returns nothing. To know whether the survey opened, listen for the `shown` event (below). If you call `show()` before the SDK has fetched the site's surveys, it waits for them.

## Open a survey on your own event

With **On an event**, the survey opens when your code reports an event, and only if its other rules hold for this visitor on this page: pages, **Who**, **Share of visitors** and **How often**.

1. **Choose On an event**

   On the **Targeting** stage, under **When**, select **On an event** and type the **Event name**, such as `signup_completed`. The stage shows the line of code to add.
2. **Publish the survey**

   Select **Publish**, then **Publish** again in the dialog.
3. **Call track() when it happens**

   ```js
   // After the sign-up has succeeded:
   userloop.track("signup_completed");
   ```

   The survey opens straight away, if its rules allow it.

- The name must match the **Event name** exactly, including its capitals.
- `track()` sends nothing anywhere: it only opens surveys waiting for that event. A second argument of properties is accepted, but not used.
- An event that happens before the SDK has fetched the site's surveys waits for them, so you can call `track()` straight after the snippet.
- An event isn't remembered. If the survey can't open at that moment (another survey is open, or this page isn't one of its pages), the event is gone.

Use `track()` when the survey's targeting should still decide, and `show()` when your code has already decided.

## Choose how it appears

Without options, the survey appears as its **Design** stage says. Pass `mode` and `position` to change that for this one call:

```js
userloop.show("SURVEY_ID", { mode: "slide_in", position: "bottom_left" });
```

| `mode` | Shows as | `position` |
| --- | --- | --- |
| `popup` | **Popup**: a card in a corner or the centre | `bottom_right`, `bottom_left`, `top_right`, `top_left` or `center` |
| `slide_in` | **Slide-in**: a panel that slides in at the bottom | `bottom_right` or `bottom_left` |
| `modal` | **Modal**: in the centre, over a dimmed page | none |
| `feedback_button` | **Feedback button**: a tab on the edge of the page that opens the survey | none |
| `inline` | **Inside a page**: in an element of your page, with `target` | none |

A feedback button shows its tab; the survey counts as shown when a visitor opens it.

## Embed a survey inside a page element

There are two ways to put a survey inside your page rather than over it.

**From the Design stage.** Choose **Inside a page** and type a CSS selector in **Where on the page**, such as `#feedback`. The survey then renders in that element as soon as the page loads, on the pages and for the visitors its targeting allows. No code is needed beyond the element.

**From code.** Give `show()` both `mode: "inline"` and a `target`, a CSS selector or an element:

```html
<section>
  <h2>How was this article?</h2>
  <div id="article-feedback"></div>
</section>
<script>
  userloop.show("SURVEY_ID", { mode: "inline", target: "#article-feedback" });
</script>
```

The survey renders inside `#article-feedback`, as wide as the element. Without `mode: "inline"`, a survey designed as a popup stays a popup even with a `target`. If the element isn't on the page when the survey opens, nothing shows and the SDK emits an `error` event with code `render`.

## Pass what you know

| Option | What it does |
| --- | --- |
| `respondent: { email }` | The visitor's email, sent with each of their answers. [`identify()`](https://docs.userloop.io/developers/sdk/identify-visitors/) does the same for every survey. |
| `collectEmail: true` | Asks for the visitor's email in the survey, unless UserLoop already has it. |
| `firstAnswer` | Answers the survey's first question and opens it on the next one: an answer's ID, or a number for a score question. Answer IDs are in the site's survey data, or in an `answered` event's `answerIds`. If it isn't one of the first question's answers, the survey opens from the start. |
| `language` | The language the survey is marked as and its answers are recorded in. It doesn't fetch a translation: use [`setLanguage()`](https://docs.userloop.io/developers/sdk/reference/#methods) for that. |

```js
userloop.show("SURVEY_ID", { respondent: { email: "ada@example.com" }, firstAnswer: "9" });
```

The [SDK reference](https://docs.userloop.io/developers/sdk/reference/#show-options) lists every option.

## Close a survey

```js
userloop.close();            // the survey that is open
userloop.close("SURVEY_ID"); // that survey, also a feedback button's tab or a minimised popup
```

`close()` removes the survey at once. It doesn't count as the visitor dismissing it, so no `dismissed` event fires. Answers already given are kept.

## React to what happens

```js
userloop.on("shown", function (e) {
  console.log("Survey shown", e.surveyId, "as", e.mode);
});
userloop.on("completed", function (e) {
  console.log("Survey completed", e.surveyId, e.responseId);
});
```

Call `on()` straight after the snippet: calls made before the SDK has loaded are queued. To stop listening, pass the same function to `userloop.off()`. Every event and its payload is in the [SDK reference](https://docs.userloop.io/developers/sdk/reference/#events).

## Check it worked

Open a page of your site, select the button (or do what calls `track()`), and the survey opens. The browser console shows `Survey shown …` from the listener above.

| You see | What to do |
| --- | --- |
| Nothing opens, and no message | Turn on debugging with `"debug":true` in the snippet's options. The console then says why `show()` did nothing. See [Test and debug an install](https://docs.userloop.io/developers/sdk/test-and-debug/). |
| `[UserLoop] unknown SURVEY_ID` | The survey isn't one of the site's live website surveys. Publish it, and check it is a website survey and the ID is right. |
| `[UserLoop] frequency SURVEY_ID` | This browser has already seen or answered it. Pass `force: true` while you test. |
| `[UserLoop] busy SURVEY_ID` | Another survey is open. Close it first. |
| `[UserLoop] render SURVEY_ID` | An inline survey's `target` isn't on the page. |

## Related

- [Choose when it appears](https://docs.userloop.io/surveys/website/choose-when-it-appears/), the triggers on the Targeting stage
- [Identify visitors and set attributes](https://docs.userloop.io/developers/sdk/identify-visitors/)
- [SDK reference](https://docs.userloop.io/developers/sdk/reference/)

Source: https://docs.userloop.io/developers/sdk/show-a-survey-from-code/index.mdx
