# Start a survey programmatically

Start a survey from your code, for example behind a feedback button, with hidden fields, a forced language or a specific distribution.

Most surveys start on their own, when the Screeb targeting engine finds a match. Start a survey from your code when the trigger is a user action the targeting rules cannot see, such as a click on a "Give feedback" button.

The survey id is in the survey editor URL, after `/surveys/`. To keep the survey from also starting on its own, set its trigger to manual in the admin: see [Survey targeting](https://help.screeb.app/ask/survey-targeting/).

```ts
import { Component } from "@angular/core";
import { Screeb } from "@screeb/sdk-angular";

@Component({
  selector: "app-feedback-button",
  template: `<button (click)="giveFeedback()">Give feedback</button>`,
})
export class FeedbackButtonComponent {
  constructor(private screeb: Screeb) {}

  async giveFeedback() {
    await this.screeb.surveyStart("<SURVEY-ID>");
  }
}
```

## Prevent multiple displays

By default, a survey started from your code shows again even if the user already answered it. Turn off multiple responses to show it only to users who have not answered yet.

```ts
await this.screeb.surveyStart("<SURVEY-ID>", undefined, false);
```

## Add context with hidden fields

Hidden fields attach values to the response. They show next to the answers in the Screeb admin and in exports, so you know where the survey started.

```ts
await this.screeb.surveyStart("<SURVEY-ID>", undefined, true, {
  source: "checkout",
  cart_size: 3,
});
```

## Force the language

Pass an ISO 639-1 code, such as `"en"` or `"fr"`, to show the survey in that language. The language used is the first one set among: this parameter, the language passed at initialization, the user's language property, then the browser or device language.

```ts
await this.screeb.surveyStart("<SURVEY-ID>", undefined, true, {}, undefined, "en");
```

## Start a specific distribution

A survey can have several distributions. Pass a distribution id to start the survey with the settings of that distribution.

```ts
await this.screeb.surveyStart("<SURVEY-ID>", "<DISTRIBUTION-ID>");
```

## Attach the survey to an element

Pass one CSS selector, or a list tried in order, to display the survey next to an element of the page.

```ts
await this.screeb.surveyStart("<SURVEY-ID>", undefined, true, {}, undefined, undefined, [
  "#feedback-button",
  ".help-menu",
]);
```

## React to the survey

Pass hooks when you start the survey to run your code when it shows, gets an answer or closes. See [Hooks](https://developers.screeb.app/sdk-angular/hooks) for the list and the payloads.

## Close a survey

Close the surveys on screen, for example when the user leaves the page the survey is about.

```ts
// Closes every running survey
await this.screeb.surveyClose();
```
