# JavaScript embed and feedback widget

## What this is

Two one-liners:

1. **Hosted form embed** ([`index.html`](index.html)): one script tag and one `div`. ContactUsIQ renders the form with your workspace theme, loads Turnstile, submits by `fetch` and shows an inline thank-you. Fields and labels come from the form definition, so edits in the dashboard appear on the site without a deploy.
2. **Floating feedback widget** ([`widget.html`](widget.html)): one script tag with data attributes. Adds a corner icon that opens a panel where visitors can ask a question, give feedback, report a problem or suggest an idea, with page context captured automatically.

This sample uses the placeholder form id `au_xxxxxxx`. Replace it. No Turnstile key is needed here; the scripts read it from the form definition.

## Prerequisites

- A ContactUsIQ workspace with a verified domain.
- For the embed: a published contact form on that domain.
- For the widget: a form whose kind is "feedback widget" (Dashboard > Forms > New > Feedback widget). Its intents, position, colours and identity settings are configured there.

## Steps

**Embed**

1. Copy the `div` and `script` from `index.html` into your page where the form should appear.
2. Replace `au_xxxxxxx` in the script `src` with your form id.
3. Keep `data-target="#contactusiq-form"` pointing at the `div` (or omit it to render the form where the script tag sits).
4. Deploy on the verified domain.

**Widget**

1. Copy the `script` tag from `widget.html` into your app's layout so it is on every page (or only the pages you want).
2. Replace `au_xxxxxxx` in `src` and `data-form` with your feedback form id.
3. Adjust `data-position`, `data-intents` and `data-app-version`. Any attribute you leave out falls back to the setting on the form.
4. For signed-in users, add `data-user-name`, `data-user-email` and `data-user-id` from your session so the visitor does not retype them.

## The code

- `index.html`: `<div id="contactusiq-form"></div>` then `<script src="https://api.contactusiq.com/f/au_xxxxxxx/embed.js" data-target="#contactusiq-form" async defer>`.
- `widget.html`: `<script src="https://api.contactusiq.com/f/au_xxxxxxx/widget.js" data-form="au_xxxxxxx" data-position="bottom-right" data-intents="question,feedback,problem" data-app-version="2026.09.1" async defer>`.

Both scripts are cached at the edge, load deferred, set no cookies of their own and never block the host page if they fail to load. The widget queues a submission in local storage when the intake API is unreachable and retries when the page is next online.

## How to test

1. Open the page on the verified domain. The form (or the corner icon) should render within a second.
2. Submit a test message. The scripts post to `https://api.contactusiq.com/s/au_xxxxxxx` with `Accept: application/json`; the Network tab shows `200` and a body `{"ok":true,"id":"sub_..."}`, and the inline thank-you appears with the reference id.
3. Widget submissions show in the inbox with an intent badge, the rating if given, and the page URL.

## Troubleshooting

- **The div stays empty.** The script URL is wrong (check the form id, `404` in the Network tab), or the form is unpublished, or a Content Security Policy blocks `api.contactusiq.com`. Allow `script-src https://api.contactusiq.com https://challenges.cloudflare.com` and `connect-src https://api.contactusiq.com`.
- **Submit shows a generic error but returns 200.** Origin mismatch: the page is not on a verified domain of the form. Test on the real domain or add the staging domain to the workspace.
- **Turnstile fails inside the embed.** The Turnstile widget in Cloudflare does not list this domain. Add it, or check that the key on the form matches.
- **429.** Rate limited: 5 per IP per form per 10 minutes. The embed shows the wait time from `Retry-After`.

## For AI agents

To add a ContactUsIQ form to a page, insert `<div id="contactusiq-form"></div>` followed by `<script src="https://api.contactusiq.com/f/au_xxxxxxx/embed.js" data-target="#contactusiq-form" async defer></script>`, replacing `au_xxxxxxx` with the real form id. To add the floating feedback widget to an app, insert one script tag loading `https://api.contactusiq.com/f/au_xxxxxxx/widget.js` with `data-position`, `data-intents` and `data-app-version` attributes into the shared layout. Nothing else is required; no API key, no build step, no server code.

```html
<!-- Form -->
<div id="contactusiq-form"></div>
<script src="https://api.contactusiq.com/f/au_xxxxxxx/embed.js" data-target="#contactusiq-form" async defer></script>

<!-- Feedback widget -->
<script src="https://api.contactusiq.com/f/au_xxxxxxx/widget.js"
  data-form="au_xxxxxxx" data-position="bottom-right"
  data-intents="question,feedback,problem" data-app-version="1.0.0" async defer></script>
```
