# Embedding scripts on your website

Install the inFlow toolkit script, track visits with analytics, add the lead form, AI chat and showcase widgets, and check that it works.

## Overview

The inFlow toolkit is one `<script>` tag. It loads the lead form, AI chat and showcase widgets into any `<div data-tks-form|chat|showcase>` on the page, and it starts analytics (pageviews, form steps, clicks) as soon as it loads with a key. Pages that already have the form or chat snippet are already tracked. There is no client-side opt-out; turn analytics off per environment in the dashboard, or remove the script.

Existing pages published through inFlow already include analytics. This page is for a website you host yourself.

Agents: this page is https://inflow.build/docs/embed-scripts (also at https://inflow.build/docs/embed-scripts.md). The same steps are in https://inflow.build/skill.md and the endpoint reference is https://inflow.build/EMBED_API.md. _(Developer only)_

## The snippet

```html
<script
  src="https://api.inflow.build/toolkit.v2.js"
  data-tks-key="pk_your_public_key"
  data-tks-environment="your_environment_id"
  async
></script>
```

| Attribute | What it does |
| --- | --- |
| `src` | The toolkit. `toolkit.js` is the same file, kept for old installs; use `toolkit.v2.js` for new ones. |
| `data-tks-key` | Required. Your public key (`pk_...`). Without it the script logs a console warning and stops. |
| `data-tks-environment` | The environment ID. Required for analytics on a site hosted outside inFlow: without it, events return 200 but are dropped unless your business has a published inFlow Site. |
| `data-tks-site` | Optional. Only for tracking a specific inFlow Site. The dashboard snippet for an environment does not include it. |
| `async` | Keep it. The script does not block the page. |

Your key and environment ID are already filled in on your environment page: **Settings → Connections → Domains & environments → your environment → Script Tag**. Copy the tag from there. If the environment has forms, chat or showcases linked to it, the matching `<div>` elements appear below the tag.

## Where to put it

Add the tag once, anywhere in `<head>` or just before `</body>`, on every page you want tracked. Before `</body>` matches the dashboard. A tag added to a template that is not on every page tracks only the pages it is on.

> **Warning:** It must be a normal classic script tag. Not `type="module"`, and not injected through code that evaluates the script text (some tag managers do this). In those cases the script exits silently, with no console message and no request.

## Register your domains

Your public key (`pk_...`) is designed for browser HTML and identifies the business. Public keys do not grant dashboard access. Keep temporary integration secrets in local environment files, never in the page.

Add every domain you run the script on under **Domains & environments**, including `localhost` if you test locally. The server accepts localhost without registration only for forms and chat, and only when inFlow's own server is not in production, so on api.inflow.build it is never automatic. For analytics, localhost must always be registered. The port is ignored, so one `localhost` entry covers every port. A disallowed origin gets HTTP 403, and the widget stays blank with no visible error.

The full origin model is in [Public key authentication](/docs/api-public-auth). _(Developer only)_

## Lead Form Widget

Opens a multi-step form on your page. Submissions create leads in your dashboard. Put the toolkit tag on the page, then a `<div>` where the form should appear. Connect on the form fills in the form ID for you.

```html
<script src="https://api.inflow.build/toolkit.v2.js" data-tks-key="pk_your_public_key" data-tks-environment="your_environment_id" async></script>

<!-- Place where you want the form to appear -->
<div data-tks-form="FORM_ID_HERE"></div>
```

> **Tip:** Set `data-page` (for example `/contact`) to show the widget only when `window.location.pathname` matches. Omit it to show on every page.

## AI Chat Widget

Adds an AI chat bubble. Visitors ask questions and the AI answers from your business info. It can also capture leads mid-conversation.

```html
<script src="https://api.inflow.build/toolkit.v2.js" data-tks-key="pk_your_public_key" data-tks-environment="your_environment_id" async></script>

<!-- Place where you want the chat to appear -->
<div data-tks-chat="AGENT_ID_HERE"></div>
```

## Analytics

Analytics tracks `pageview`, `form_scroll`, `form_focus`, `form_submit` and `click` events. It needs nothing beyond the snippet above. Single-page apps are covered: navigation through the History API counts as a pageview. Results appear in the Analytics card on the environment page.

The older `analytics.js` snippet with `data-tracking-id` is legacy. Outside sites should use the toolkit snippet on this page.

## Next.js _(Developer only)_

In the App Router, load the toolkit with `next/script` in the root layout: _(Developer only)_

```tsx
// app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://api.inflow.build/toolkit.v2.js"
          data-tks-key="pk_your_public_key"
          data-tks-environment="your_environment_id"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}
```

_(Developer only)_

Alternatively, mount the `InflowAnalytics` component once in the root layout, with your `publicKey` and `environmentId`. Use one, not both. The Next.js tab on the environment page generates the form, showcase and chat components but not `InflowAnalytics`, so mount it yourself or use the script tag. _(Developer only)_

## Verify without dashboard access

1. Load a page that has the tag, in a browser, on a registered domain. Open devtools → Network and filter for `analytics`.
2. Wait about 5 seconds. Events flush every 5 seconds; clicks, form submits and leaving the tab flush immediately.
3. Look for `POST https://api.inflow.build/api/analytics/event?key=pk_...` with a JSON body like `{"events":[{"type":"pageview","path":"/",...}],"environmentId":"..."}`.
4. A working install returns `200` with body `{"ok":true}`.

| Response | Meaning |
| --- | --- |
| 401 | The public key is missing or invalid. |
| 403 `Origin not allowed` | The domain is not registered under Domains & environments (this includes localhost). |
| 403 `Analytics disabled for this environment` | Analytics is switched off for that environment. |
| 404 `Environment not found` | The `data-tks-environment` ID is wrong or belongs to another business. |
| 400 | Invalid environment ID or malformed body. |
| 429 | More than 60 requests a minute from one IP. |

If no request appears at all, the script is `type="module"` or evaluated by a tag manager, the tag has no `data-tks-key`, or it did not load.

> **Warning:** A `200 {"ok":true}` alone does not prove the event was stored. The same response is returned when events are dropped because there is no environment or site. Check that `data-tks-environment` is present in the request body. An OPTIONS preflight cannot test the domain allowlist, because it accepts every origin; only the POST does.

Without a browser, `curl -X POST "https://api.inflow.build/api/analytics/event?key=pk_..." -H "Content-Type: application/json" -d '{"events":[{"type":"pageview","path":"/","visitorId":"v_test","sessionId":"s_test"}],"environmentId":"..."}'` returns `{"ok":true}`. It writes a real test event and does not test the domain allowlist, since curl sends no Origin header. _(Developer only)_

## Privacy

Analytics uses no cookies and stores no IP address or user agent with each event, but it does store two identifiers in the visitor's browser: a persistent visitor ID in localStorage (`_ia_vid`) and a per-tab session ID in sessionStorage (`_ia_sid`), plus the landing page's campaign parameters in sessionStorage (`_ia_campaign`). Under EU ePrivacy rules, reading or writing identifiers on a visitor's device needs the same consent as cookies, so show a consent prompt before loading the script for EU visitors. Standard server access logs may still record IP and user agent.
