How to add the code
Where the Form Monitoring code goes - the four snippets, where to put them in your tool, iframes and redirects.
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
- Add the form with With a code snippet. See 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_IDandTOKENare 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.
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.
The four snippets
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.
<img src="https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&k=TOKEN" width="1" height="1" alt="" referrerpolicy="no-referrer">The Fail pixel (optional) goes in your error message:
<img src="https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&k=TOKEN&outcome=fail" width="1" height="1" alt="" referrerpolicy="no-referrer">Page attribute
Add this attribute to the element that shows your success message. Do not add it to the <form> element. The Forms tag must be on the page.
<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:
<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
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.
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
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.
https://data.vitalsentinel.com/v1/forms/events?tid=TRACKING_ID&k=TOKENThe 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.
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.
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.
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
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
keepaliveoption 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.
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.
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
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?
Related
- How to count - compare the methods
- Reading the numbers - run the self-test
- Troubleshooting - common problems
- Privacy - consent and the code snippets
How to count
Count form submissions automatically or with a code snippet. Compare the Tracking pixel, Page attribute, JavaScript and Webhook methods, and rotate or change them.
Reading the numbers
What Shown, Submitted, Left and Fails mean, what each form status means, and how to use the Overview, Alerts and Setup tabs and the self-test.