# How to add the code

This page shows where the code of a form goes. Copy your code from the app. It contains the tracking ID of your domain and the token of your form.

## Before you start [#before-you-start]

- Add the form with **With a code snippet**. See [Add a form](/form-monitoring/add-a-form).
- To see the code again, open the form, then open the **Setup** tab. The **Tracking method** card shows it. Only owners and admins see the code.
- On this page, `TRACKING_ID` and `TOKEN` are placeholders. The code in the app has the real values.
- Code that you copied before, with `app.vitalsentinel.com/v1/analytics/events`, continues to count. You do not have to replace it.

<Callout type="warn">
  Never place the code on a page that people can open by themselves, for example a thank-you page. Each visit to that page counts as a submission. Search engines and links can bring visitors to such a page. Put the code in the success message, or run it on the success event.
</Callout>

## The four snippets [#the-four-snippets]

### Tracking pixel [#tracking-pixel]

Place this in the success message, so that it loads only after a real success. It runs outside the Forms tag, so load it under the consent rules of your site.

```html
<img src="https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&amp;k=TOKEN" width="1" height="1" alt="" referrerpolicy="no-referrer">
```

The **Fail pixel (optional)** goes in your error message:

```html
<img src="https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&amp;k=TOKEN&amp;outcome=fail" width="1" height="1" alt="" referrerpolicy="no-referrer">
```

### Page attribute [#page-attribute]

Add this attribute to the element that shows your success message. Do not add it to the `<form>` element. The [Forms tag](/form-monitoring/overview#add-the-forms-tag) must be on the page.

```html
<div data-vs-form="success:TOKEN">Thank you. We received your message.</div>
```

The **Fail attribute (optional)** goes on the element that shows your error message:

```html
<div data-vs-form="fail:TOKEN">We could not send your message.</div>
```

Use the attribute only on a message that shows after a submission. The script does not read the text of a marked message.

If the form reloads the page, the attribute counts only if the browser reports the page as the referrer. A `Referrer-Policy` of `no-referrer`, `origin` or `strict-origin` stops this.

### JavaScript [#javascript]

Run this line after your own success code, when the submission went through. It runs outside the Forms tag, so load it under the consent rules of your site.

```javascript
fetch('https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&k=TOKEN', { mode: 'no-cors', keepalive: true, credentials: 'omit', referrerPolicy: 'no-referrer' })
```

For the **Fail line (optional)**, add `&outcome=fail` to the end of the URL. Run it where your code handles a failed submission.

### Webhook [#webhook]

Paste this URL into the webhook settings of your form tool. A server, or a tool such as Zapier or Make, can call it with a GET request.

```text
https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&k=TOKEN
```

The **Fail URL (optional)** is the same URL with `&outcome=fail` at the end.

The Webhook takes GET requests only. It does not depend on the browser of the visitor.

<Callout type="info">
  A GET call answers with a 1x1 GIF image, also when the token is wrong or VitalSentinel does not count the call. So a response proves nothing. To check your code, use the [self-test](/form-monitoring/reading-the-numbers#the-self-test).
</Callout>

## Where to put the code [#where-to-put-the-code]

Each tool is different. Put the code where your tool shows the success message, or where it runs something after a successful submission.

- **A success message that accepts HTML.** Paste the **Success pixel** into the message.
- **Code that runs after a success.** Paste the **Success line** of the **JavaScript** tab there. This can be your own code or a tag manager, for example Google Tag Manager.
- **A webhook or a URL call after a submission.** Point it at the **Success URL** of the **Webhook** tab. Use a GET request.

The code must run only after a successful submission. Do not run it on a page view. Do not run it on a click of the submit button, because a click does not prove a success.

- Do not paste the code into a field that runs on every page, such as a header or footer field. Each page view then counts as a submission.
- Some tools remove HTML from a success message. After you save, make sure that the code is still there.
- A POST request does not count. If your tool can send only POST, let a tool such as Zapier or Make call the URL with GET.
- If your tool can report a failed submission, add the fail version of the code there.
- If a cache serves your pages, clear the cache after you add the code.

After you add the code, submit the form once, and watch the [self-test](/form-monitoring/reading-the-numbers#the-self-test).

## Forms in iframes [#forms-in-iframes]

A form that is embedded from another site is in an iframe. The Forms tag cannot see inside it. If VitalSentinel does not recognize the platform of the form, the app shows a note when you track the form or change its method. The note says "We can't see inside this form because it's embedded from another site, so use a code snippet."

- Use the **Webhook** tab if the form tool has webhooks.
- Use the **Tracking pixel** tab if the form tool lets you add HTML to its success message.
- Use the **JavaScript** tab if the form tool can run your code after a success.
- **Page attribute** is not available for these forms, because the message is inside the iframe.

HubSpot and Jotform embeds report by themselves. For these forms, **Automatically** is available in the setup.

For other embedded forms, VitalSentinel counts **Shown** only if it recognizes the form tool that hosts the iframe.

## Forms that redirect [#forms-that-redirect]

Some forms send the visitor to another page after the submission. With **Automatically**, a plain redirect shows **Shown** but no **Submitted**. The full page load ends the watch of the script.

Such a form needs one of these methods:

- **Webhook.** Call it from your backend or your form tool, after the submission went through. This method works for every form that redirects.
- **JavaScript.** Use it where your own code sends the form and then moves on to the next page. The `keepalive` option lets the request finish after the page changes.

A **Page attribute** does not help, because the script stops when the page changes. A **Tracking pixel** does not help, because no success message shows on the page of the form.

<Callout type="warn">
  Do not place the code on the page that the form redirects to. People can open this page without a submission, and each visit counts as a submission.
</Callout>

A single page app that changes the route without a page load is different. The watch of the script continues after the route change, until 60 seconds after the submit. So **Automatically** can count a success message on the new route.

## If your site has a Content Security Policy [#if-your-site-has-a-content-security-policy]

The **Tracking pixel** needs `img-src` and the **JavaScript** line needs `connect-src`. Both use `https://data.vitalsentinel.com`. See [Which CSP directives do I add for VitalSentinel?](/connect/tracking-scripts#which-csp-directives-do-i-add-for-vitalsentinel)

## Related [#related]

- [How to count](/form-monitoring/how-to-count) - compare the methods
- [Reading the numbers](/form-monitoring/reading-the-numbers#the-self-test) - run the self-test
- [Troubleshooting](/form-monitoring/troubleshooting) - common problems
- [Privacy](/form-monitoring/privacy) - consent and the code snippets
