---
title: "SDK reference: methods, options and events"
description: "Every method on window.userloop, every init and show option, every event and error code, and the display modes, positions and triggers."
---

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

# SDK reference: methods, options and events

Everything `window.userloop` offers, in the order the SDK's own type definitions give it.

## Loading

| Name | What it is |
| --- | --- |
| `https://cdn.userloop.io/sdk-3/userloop.js` | The SDK. It loads the file that draws surveys from the same place the first time a survey opens. |
| `window.userloop` | The SDK's only global, lowercase. Before the SDK loads, the snippet makes it a stand-in that queues calls to every method except `getVisitorId` and `version`. The SDK replays them in order when it loads. |
| `UserLoopReady` | A `CustomEvent` on `window`, fired once when the SDK has loaded and replayed the queued calls. `event.detail.version` is the SDK's version. |
| `data-site-key` | On the plain `<script>` tag: the SDK calls `init({ siteKey })` itself, unless an `init` call is already queued, which then wins. |

A second copy of the snippet or the SDK on the same page does nothing. The SDK never defines `window.UserLoop`, which belongs to UserLoop's older script.

## Methods

Every method returns nothing, unless the table says otherwise, and never throws into your page. Calls made before `init` wait for it, up to 100 of them.

| Method | Parameters | Returns | What it does |
| --- | --- | --- | --- |
| `init(options)` | `options`: [init options](#init-options) | nothing | Starts the SDK. The snippet calls it for you. Only the first call on a page counts. In a browser without `fetch` or Shadow DOM, it does nothing. |
| `identify(userId, traits?)` | `userId`: string, not empty. `traits`: [attributes](#attribute-values) | nothing | Sets the user ID, kept in the browser and sent with each answer. Trait `email` becomes the address the answers belong to, kept in memory for this page only. Other traits are set as attributes. |
| `setAttributes(attributes)` | `attributes`: [attributes](#attribute-values) | nothing | Merges attributes into the visitor's: a new value replaces the old, `null` removes a key. Kept in the browser and sent with each answer. Re-checks every survey's rules. |
| `track(event, properties?)` | `event`: string. `properties`: attributes, not used | nothing | Opens surveys whose trigger is **On an event** with this exact name, if their rules allow it now. Sends nothing anywhere. Before the site's surveys have arrived, up to 50 events wait for them. |
| `page()` | none | nothing | Counts a page view the SDK couldn't see: restarts **After a wait** and **On scroll**, and re-checks every survey's rules against the current address. |
| `show(surveyId, options?)` | `surveyId`: the survey's ID. `options`: [show options](#show-options) | nothing | Opens a live website survey of the site. Skips its pages, **Who** rules, **Share of visitors**, trigger and **One survey per visit**; honours the site's addresses, **How often** (unless `force`) and one survey at a time. Waits for the site's surveys if they haven't arrived. |
| `close(surveyId?)` | `surveyId`: optional | nothing | Without an ID, closes the survey that is open. With one, removes every copy of that survey, including a feedback button's tab or a minimised popup. Fires no event. |
| `reset()` | none | nothing | For sign-out: a new visitor ID; forgets the user ID, email, attributes, which surveys this browser has seen and answered, and the visit. |
| `setLanguage(language)` | `language`: a language code such as `fr` or `fr-CA` (the base code is used) | nothing | Fetches the site's surveys again in that language. An open survey changes language and keeps its answers. |
| `on(event, handler)` | `event`: an [event name](#events). `handler`: function given the payload | a function that unsubscribes, once the SDK has loaded; nothing through the stand-in | Listens for an event. An error thrown by your handler stays in your handler. |
| `off(event, handler)` | the same `event` and `handler` given to `on` | nothing | Stops listening. |
| `getVisitorId()` | none | string, or `null` before `init` | The visitor ID: a UUID, sent with every answer. Only once the SDK has loaded. |
| `version` | property | string | The SDK's version, such as `3.0.0+1a2b3c4`. Only once the SDK has loaded. |

### Attribute values

An attributes object maps a key of 1 to 64 characters to a string, a number, `true` or `false`, `null`, or a list of strings. A visitor has at most 50 keys; strings are cut to 500 characters and lists to 50 strings. Any other value is ignored. The key `email` is never an attribute: it becomes the address the answers belong to.

## Init options

Pass them in the object at the end of the snippet, such as `{"siteKey":"SITE_KEY","debug":true}`.

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `siteKey` | string | none | The site's key from **Settings → Website**. With it, the SDK fetches the site's live website surveys and shows them by their rules. A value that isn't a key (8 to 64 letters, digits, `_` or `-`) is ignored, with a debug message. |
| `storage` | `"local"`, `"session"` or `"memory"` | `"local"` | Where the SDK keeps the visitor, their attributes and survey history: `localStorage`, `sessionStorage`, or nowhere. See [Identify visitors](https://docs.userloop.io/developers/sdk/identify-visitors/#choose-where-it-stores-things). |
| `language` | string | the page's `<html lang>`, then the browser's language | The language whose translations of the surveys are fetched. The base code is used: `en-GB` is `en`. |
| `nonce` | string | the nonce of the SDK's `<script>` tag, which the snippet copies from its own | The Content-Security-Policy nonce put on the script that draws surveys, and on its `<style>` tag in browsers without constructable stylesheets. |
| `autoPageViews` | boolean | `true` | Watches `pushState`, `replaceState`, `popstate` and `hashchange` for route changes. Set `false` and call `page()` yourself. |
| `debug` | boolean | `false` | Logs `[UserLoop]` warnings to the console. They never contain an answer, an email or an address with its query. |
| `endpoints` | object | UserLoop's own addresses | For UserLoop's own testing. Leave it out. |

## Show options

| Option | Type | Default | What it does |
| --- | --- | --- | --- |
| `mode` | a [display mode](#display-modes-and-positions) | the survey's own, from its **Design** stage | How the survey appears, for this call. |
| `position` | a [position](#display-modes-and-positions) | the survey's own | Where a popup or slide-in sits. |
| `target` | CSS selector or `Element` | none | Where a survey shown with `mode: "inline"` renders. If nothing matches, the survey doesn't show and an `error` event with code `render` fires. |
| `force` | boolean | `false` | Also skips **How often**, so a survey this browser has already seen or answered opens. |
| `respondent` | `{ email?: string }` | none | `email` is the address this response belongs to, sent with each answer. The object also takes `customerId` and `rewardToken`, which only UserLoop's own survey pages use. |
| `firstAnswer` | string | none | Answers the first question and opens the survey on the next: one of that question's answer IDs, or a whole number for a score question. Anything else is ignored. |
| `collectEmail` | boolean | `false` | Asks for the visitor's email in the survey, unless it is already known. |
| `language` | string | the `init` language | The language the survey is marked as, and its answers recorded in. Doesn't fetch a translation: use `setLanguage()`. |
| `preview` | boolean | `false` | Shows the survey without sending answers, counting a view or recording it for **How often**, and with its question rules ignored. It replaces any survey already open. The editor's preview passes an object here. |
| `document`, `documentUrl`, `surface` | | none | Used by UserLoop's own survey pages and the editor's preview. Leave them out. |

## Events

Listen with `userloop.on(name, handler)`. Each handler gets one payload object.

| Event | When | Payload |
| --- | --- | --- |
| `shown` | The survey's first question is visible, once per showing. For a feedback button or a popup that starts minimised, when the visitor first opens it. | `surveyId`, `responseId`, `mode` |
| `answered` | The visitor gives an answer and moves on, and again if they change it. | `surveyId`, `responseId`, `questionId`, `questionType`, `answerIds?`, `text?`, `score?`, `final` |
| `completed` | The last answer has been given. It can fire again for the same submission if the visitor retries a send that failed. | `surveyId`, `responseId` |
| `dismissed` | The visitor closed the survey before finishing. Not fired by `close()`. | `surveyId`, `responseId`, `answeredCount` |
| `error` | Something failed. See [error codes](#error-codes). | `code`, `surveyId?` |

| Field | Type | What it is |
| --- | --- | --- |
| `surveyId` | string | The survey's ID, as `show()` takes it. |
| `responseId` | string | A UUID for this submission: one showing of the survey, from the first question to the end. |
| `mode` | string | The display mode it was shown in. |
| `questionId` | string | The question answered, follow-up questions included. |
| `questionType` | string | `Single Select`, `Multi Select`, `Open Ended`, `Email`, `Phone`, `Date`, `Number Score` or `CSAT`. |
| `answerIds` | string[] | The chosen answers' IDs, for a choice or CSAT question. |
| `text` | string | What the visitor typed, for a question they type into. |
| `score` | number | The number chosen on a score question. |
| `final` | boolean | `true` on the last question the visitor will see. |
| `answeredCount` | number | How many questions the visitor answered before closing. |
| `code` | string | An [error code](#error-codes). |

## Error codes

| Code | Cause |
| --- | --- |
| `site_fetch` | The site's surveys couldn't be fetched, after one retry 10 seconds later for a network or server error. No survey shows on this page. |
| `document_fetch` | A `documentUrl` given to `show()` didn't answer `200`. |
| `invalid_document` | A survey's data was incomplete or malformed. That survey is skipped; the others still show. |
| `storage` | The browser refused storage. The SDK keeps working in memory for the rest of the page. |
| `submit` | An answer was dropped after the retry limits: a network error, `408`, `429` or `5xx` is retried after 1, 4, 15 and 60 seconds, then once per page view, up to 10 attempts or 24 hours; any other `4xx` is dropped at once. |
| `render` | A survey shown inside a page had no element to render in. Nothing showed. |
| `renderer_load` | The file that draws surveys didn't load or didn't start within 10 seconds. Nothing showed, nothing was counted, and the next survey to open tries again. |

[Test and debug an install](https://docs.userloop.io/developers/sdk/test-and-debug/#an-error-event-or-error-code) says what to do about each.

## Display modes and positions

| `mode` | In the app (**Design** stage) | Positions |
| --- | --- | --- |
| `popup` | **Popup** | `bottom_right`, `bottom_left`, `top_right`, `top_left`, `center` |
| `slide_in` | **Slide-in** | `bottom_right`, `bottom_left` |
| `modal` | **Modal** | none |
| `feedback_button` | **Feedback button** | none; its side is set on the **Design** stage |
| `inline` | **Inside a page** | none; it renders in `target`, or the element in **Where on the page** |

A new website survey is a popup in the bottom right. A popup given a position it doesn't know goes in the bottom right; a slide-in takes the side, left or right, that the position names; the other modes ignore `position`. UserLoop's own survey pages use one more mode, `fullpage`.

## Triggers

How a survey opens, set on the **Targeting** stage under **When**. A feedback button and a survey inside a page have no trigger.

| Trigger | In the app | Opens |
| --- | --- | --- |
| `time_on_page` | **After a wait** | That many seconds after the page view started; `0` as the page loads. A page view starts the count again. |
| `scroll` | **On scroll** | Once the visitor has scrolled that percentage of the page in this page view. A page that can't scroll counts as 100%. |
| `exit_intent` | **On exit** | When the pointer leaves through the top of the window. Never on a phone or tablet. |
| `click` | **On a click** | With a selector: a click on an element matching it, or inside one. With text: a click on an element whose text is that text, ignoring capitals and extra spaces. With both: the element matching the selector must also have that text. |
| `event` | **On an event** | When your code calls `userloop.track()` with exactly that name. |
| `manual` | **From code** | Never by itself: only `userloop.show()`. |

## Attribute rules

How the **Visitor attributes** rules on the **Targeting** stage compare the attributes your code sets. All of a survey's rules must hold.

| Rule | In the app | Holds when |
| --- | --- | --- |
| `equals` | **is** | The value equals the rule's: as numbers when both are numbers, else ignoring capitals and surrounding spaces. |
| `not_equals` | **is not** | `equals` doesn't hold, including when the attribute isn't set. |
| `contains` | **contains** | The value contains the rule's text, ignoring capitals. |
| `exists` | **is set** | The attribute has a value that isn't empty. |
| `not_exists` | **isn’t set** | The attribute is missing, `null` or empty. |

`true` and `false` compare as text. On a list, `equals` and `contains` hold when any item matches.

## Related

- [Install the SDK](https://docs.userloop.io/developers/sdk/install/)
- [Show a survey from code](https://docs.userloop.io/developers/sdk/show-a-survey-from-code/)
- [Identify visitors and set attributes](https://docs.userloop.io/developers/sdk/identify-visitors/)
- [Test and debug an install](https://docs.userloop.io/developers/sdk/test-and-debug/)

Source: https://docs.userloop.io/developers/sdk/reference/index.mdx
