> ## Documentation Index
> Fetch the complete documentation index at: https://docs.widgetic.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Embedding widgets

> Put a published composition on your website as a box in the page, a pop-up or a floating box, choose when it opens, and listen for events

This page is for **self-serve developers** and partners who show a **published composition** on a website. To show a workflow (a path of compositions), see [Embed a workflow](https://docs.widgetic.com/docs/api/embedding-workflows.md). To put Widgetic's editors in your product, see [UI Components](https://docs.widgetic.com/docs/partners/ui-components). Every option of the **Embed code** screen is listed, with its attribute, in [Embed settings](https://docs.widgetic.com/docs/api/embed-settings.md).

## Embed code

After you publish a composition, the dashboard (or `get_embed_code` in MCP) gives you a tag and script. The supported way is the CDN script plus a `<widgetic>` element placed where the widget should appear. There is nothing to install and no key in the page: showing a widget needs only the composition id.

```html theme={"theme":"css-variables"}
<script
  src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6"
  data-api-url="https://api.widgetic.com"
></script>
<widgetic
  composition-id="COMPOSITION_ID"
  resize="fill-width"
  width="800"
  height="450"
  autoscale="on"
  adaptive="480"
  embed-target="site"
></widgetic>
```

* **Several widgets on one page** work with one script tag.
* **`data-api-url`** is the API the script asks for the composition. Copy it as the dashboard gives it.
* **Every other attribute is an option** of the **Embed code** screen. Together they decide how the widget fits its place ([Size and layout](#size-and-layout)), where it appears ([Choose how the widget appears](#choose-how-the-widget-appears)) and when ([Choose when a pop-up opens](#choose-when-a-pop-up-opens)).
* **Where the widget runs.** The script mounts the widget in the page when it can, and in an iframe of its own when it cannot. Add `render-mode="iframe"` to ask for the iframe from the start.
* **Allowed websites**: if you limited the websites that may show your widgets, the script shows nothing on other websites. Your own machine and Widgetic's own sites are always allowed, and so is a partner's platform website with all of its subdomains. See [Allowed domains](#allowed-domains).

When you cannot load a script, use the frame instead:

```html theme={"theme":"css-variables"}
<iframe
  src="https://api.widgetic.com/embed/compositions/COMPOSITION_ID/frame"
  width="800"
  height="450"
  frameborder="0"
  allowfullscreen
></iframe>
```

### Paste an embed code to set the options

The **Embed code** screen in the dashboard shows your embed code in a box at the top, with the line "Not sure how embedding works? **Learn more**" just below it, and the options under that. You can paste an existing embed code into the box and the options (size, resize mode, overlay position and the rest) change to match it, which is the quickest way to edit an embed that is already on a page:

1. Copy the embed code from your page.
2. Click the code box in the **Embed code** screen.
3. Press `Cmd+V` (Mac) or `Ctrl+V` (Windows, Linux).

The box does not accept typing, only a paste. If the text is not a Widgetic embed code, the screen says so and nothing changes. Options in the pasted code that the screen does not know are listed and left out.

## Choose how the widget appears

The **Embed target** decides how a visitor meets the widget:

| Embed target | In the tag | What a visitor sees |
| - | - | - |
| **Inline** | `embed-target="site"` | The widget sits in the page, where the tag is. |
| **Overlay** | `embed-target="overlay"` | Nothing at first. A pop-up opens when a trigger fires, or when your page asks for it. |
| **Mixed** | `embed-target="mixed"` | The widget sits in the page. When the visitor scrolls past it, it follows them in a floating box, and goes back when they scroll to its place again. |
| **Use iframe** | no `<widgetic>` tag | A plain frame with no script, for places that cannot run one. |

The **Embed code** screen starts on Overlay, so a code you copy without changing it is a pop-up. A `<widgetic>` tag that has no `embed-target` is Inline.

### Inline

The widget is part of the page and takes the space of its tag. This is the code at the top of this page, and [Size and layout](#size-and-layout) says how the box is sized.

### Overlay: a pop-up

A pop-up in the middle of the window on a dimmed background, that opens when the visitor is about to leave, at most twice in 72 hours, and not on phones:

```html theme={"theme":"css-variables"}
<script
  src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6"
  data-api-url="https://api.widgetic.com"
></script>
<widgetic
  composition-id="COMPOSITION_ID"
  resize="fill-width"
  width="480"
  height="360"
  autoscale="on"
  adaptive="480"
  embed-target="overlay"
  overlay="enabled"
  overlay-position="center-center"
  event-trigger="exit-intent"
  overlay-backdrop="true"
  overlay-background-color="#000000"
  overlay-background-opacity="0.6"
  device-mobile-enabled="false"
  device-desktop-enabled="true"
  max-views="2"
  time-period="72"
></widgetic>
```

* **The tag is hidden from the first moment**, so the widget never flashes in the page before the pop-up. A pop-up that never opens never loads the widget.
* **It opens when the trigger fires** ([Choose when a pop-up opens](#choose-when-a-pop-up-opens)). It sits at the **Overlay position** you chose, one of nine cells of the window, 20 pixels from the edges. It is as big as `width` and `height` say, and never larger than 90% of the window.
* **The visitor can always close it**: it has a close button, and `Esc` closes it too.
* **Add background** puts a layer behind the pop-up, in the color and opacity you choose, and the page behind cannot be clicked. Without it the page stays usable while the pop-up is open.
* **View limits** keep a visitor from seeing it over and over: at most **Max # of views** within the **Time period**, counted in the visitor's browser.

### Mixed: a box that follows the visitor

A widget in the page that floats in the bottom-right corner, at 320 by 180 pixels, once its place has scrolled out of view, and never on `/checkout`:

```html theme={"theme":"css-variables"}
<script
  src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6"
  data-api-url="https://api.widgetic.com"
></script>
<widgetic
  composition-id="COMPOSITION_ID"
  resize="fill-width"
  width="800"
  height="450"
  autoscale="on"
  adaptive="480"
  embed-target="mixed"
  overlay="enabled"
  overlay-position="bottom-right"
  exclude-url-patterns="/checkout"
  keep-dimensions="false"
  overlay-resize-mode="fixedSize"
  overlay-width="320"
  overlay-height="180"
></widgetic>
```

* **The widget is in the page from the start**, and the floating box appears when the visitor scrolls it out of view. An empty space the size of the widget stays in the page, so the page does not jump. When the visitor scrolls back to that space, or closes the floating box, the widget returns to it.
* **Keep dimensions** on lets the floating box keep the size the widget has in the page. Off, the **Overlay resize mode**, **Overlay width** and **Overlay height** size it, as in the code above. A floating box is never larger than 90% of the window.
* **Mixed has fewer options than Overlay.** It has no trigger and no rules except **Exclude URLs**, and the device switches of **View limits** apply. The view limits themselves, **Max # of views** and **Time period**, count pop-ups only.

### Use iframe: a frame with no script

With **Use iframe** on, the code is only the frame of the composition. It needs no script, so it works where scripts cannot run, but the widget cannot follow the height of its content or hear events from your page, and only **Width** and **Height** are left to set:

```html theme={"theme":"css-variables"}
<iframe
  src="https://api.widgetic.com/embed/compositions/COMPOSITION_ID/frame"
  width="500"
  height="400"
  frameborder="0"
  allowfullscreen
></iframe>
```

## Size and layout

How the box of an Inline embed is sized comes from `resize`, `width` and `height`:

| `resize` | The box is |
| - | - |
| `fixed` | `width` by `height`, never wider than the space the tag is in. |
| `fill-width` | As wide as the space the tag is in. |
| `responsive` | As wide as the space the tag is in, up to `width`. |
| `custom` | The same box as `fixed`. |

A tag with no `resize`, or with a value that is not in the table, is a fixed box, and a tag that gives no `width` or `height` gets 400 by 300 pixels. `resize` and `adaptive` are for an Inline embed: an Overlay or a Mixed embed is not sized by them, and a pop-up or a floating box is sized as described above.

* **Height of a wide box.** With `fill-width` or `responsive`, the height is `height`. When the tag has no Autoscale and both `width` and `height` are numbers of pixels, the height instead keeps the proportions of that `width` and `height` as the box gets wider or narrower, so a narrow box is shorter, as a picture is.
* **Autoscale** (`autoscale="on"`) makes the box as tall as the widget needs, so the widget never clips and the paragraph after it is never covered. The box starts at `height`, or at the height the last visit ended on so a reload does not jump, and then follows the widget between `autoscale-min-height` (the tag's `height` unless you say otherwise) and `autoscale-max-height` (2000 pixels unless you say otherwise, at most 20000). A widget shown in an iframe is followed by the height it reports. Each change sends `widgetic:autoscale`. Off, the box stays the height of the tag. A tag that says `autoscale="false"` (or `0`, `off` or `no`) is off too.
* **Adaptive** (`adaptive="480"`) is a breakpoint in pixels, a whole number from 1 to 10000. The tag says `data-widgetic-mobile="true"` while the window is that narrow or narrower, and `"false"` above it, and sends `widgetic:adaptive` once the box is made and each time the window crosses it, so your page, or a widget that listens, can switch to a narrow layout. The box itself does not change: a `fixed` tag stays fixed. A `responsive` tag without `adaptive` uses 480.

```css theme={"theme":"css-variables"}
/* Give a narrow window a margin around the widget */
widgetic[data-widgetic-mobile="true"] {
  margin: 0 12px;
}
```

## Choose when a pop-up opens

The **Event triggers** setting decides what opens an Overlay pop-up by itself (`event-trigger` in the tag):

| Trigger | `event-trigger` | The pop-up opens |
| - | - | - |
| **Exit intent** (the default) | `exit-intent` | When the visitor is about to leave: the pointer goes out through the top of the window, or, on a touch screen, the back button, a quick scroll up or a switch to another tab. It also opens once the visitor has scrolled 100 pixels down. |
| **Page load** | `page-load` | As soon as the page is ready. |
| **Delay** | `delay` | After `trigger-delay` milliseconds (the screen asks for seconds). |
| **Scroll (pixels)** | `scroll` | When the visitor has scrolled `trigger-scroll-distance` pixels down. |
| **Scroll (percent)** | `scroll-percent` | When the visitor has scrolled `trigger-scroll-percent` percent of the page's height. |
| **Manual** | `manual` | Never by itself: your page opens it, see [Open it from your page](#open-it-from-your-page). |
| **Times page shown** | `times-shown` | Once a page with this embed has loaded `trigger-page-views` times in this browser, and on every load after that. |

Pick one trigger. Besides the trigger, a pop-up can follow **rules**, which the visitor's browser checks before it opens:

* **Excludes**: pages it must not open on, and not again after the visitor closed it or after it opened once.
* **Visitors**: new or returning visitors, the time spent on the page, the number of pages seen on your site.
* **Schedule**: a range of dates, weekdays and hours of the day, by the visitor's clock.
* **Acquisition**: direct traffic only, the referrer, the page's address, and its `utm_source`, `utm_medium` and `utm_campaign`.
* **View limits**: at most so many views in a number of hours.

Every rule that is set has to pass, for each trigger, **Manual** included. The checks use the page's address, the referrer, the clock and counters that the browser keeps in its own storage, so clearing the site's data in the browser resets the counts and the "do not show again" flags. [Embed settings](https://docs.widgetic.com/docs/api/embed-settings.md) lists each rule and its limits.

Here is a pop-up in the bottom-right corner that opens 8 seconds after the page is ready, only on weekdays from 09:00 to 17:00 by the visitor's clock, only for visitors who arrive from a link whose `utm_campaign` contains `spring`, never on `/checkout` or `/cart`, and not again once the visitor has closed it:

```html theme={"theme":"css-variables"}
<script
  src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6"
  data-api-url="https://api.widgetic.com"
></script>
<widgetic
  composition-id="COMPOSITION_ID"
  resize="fill-width"
  width="405"
  height="300"
  autoscale="on"
  adaptive="480"
  embed-target="overlay"
  overlay="enabled"
  overlay-position="bottom-right"
  event-trigger="delay"
  trigger-delay="8000"
  overlay-backdrop="false"
  exclude-url-patterns="/checkout,/cart"
  exclude-after-dismiss="true"
  schedule-days="mon,tue,wed,thu,fri"
  schedule-time-start="09:00"
  schedule-time-end="17:00"
  utm-campaign-match="spring"
></widgetic>
```

### Open it from your page

With the **Manual** trigger the pop-up never opens by itself. Your page opens it with `Widgetic.show`, for example from a button, and gives it the `composition-id` of the tag:

```html theme={"theme":"css-variables"}
<script
  src="https://cdn.widgetic.com/sdks/browser/embed-sdk.js?v=6"
  data-api-url="https://api.widgetic.com"
></script>
<widgetic
  composition-id="COMPOSITION_ID"
  resize="fill-width"
  width="640"
  height="400"
  autoscale="on"
  adaptive="480"
  embed-target="overlay"
  overlay="enabled"
  overlay-position="center-center"
  event-trigger="manual"
  overlay-backdrop="true"
  overlay-background-color="#000000"
  overlay-background-opacity="0.5"
></widgetic>

<button id="open-widget" type="button">Play and win</button>
<script>
  document.getElementById('open-widget').addEventListener('click', () => {
    // true when the page has a pop-up with this id, false when it has none yet.
    Widgetic.show('COMPOSITION_ID');
  });
</script>
```

`Widgetic.show` is there once the embed script has run. It returns `true` when the page has a pop-up for that id, and `false` when it has none, for example because the script has not found the composition yet. The rules and the view limits still apply, so a `true` is not a promise that the pop-up opened: listen for `widgetic:overlay-blocked` (below) to know why it did not.

## Set the options with the API

The options of a composition are saved in its `embedSettings`, under the keys listed in [Embed settings](https://docs.widgetic.com/docs/api/embed-settings.md):

* `GET /compositions/{id}/schema/embed` returns the schema of the options, which is the same for every composition, with the `defaults` of all of them.
* `GET /compositions/{id}/embed-settings` returns what the composition saved. A composition that has saved nothing answers `404`: start from the `defaults` of the schema.
* `PUT /compositions/{id}` with `embedSettings` saves them. The object you send **replaces** the saved one, so send every setting you want to keep.

```bash theme={"theme":"css-variables"}
curl -sS -X PUT "https://api.widgetic.com/v1/compositions/$COMPOSITION_ID" \
  -H "Authorization: Bearer $WIDGETIC_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "embedSettings": {
      "embedTarget": "overlay",
      "overlayPosition": "bottom-right",
      "eventTriggers": "delay",
      "triggerDelaySeconds": 8,
      "restrictions": true,
      "maxViews": 2,
      "timePeriod": 72
    }
  }'
```

* **Send each value as the schema types it**: a switch is `true` or `false`, a number is a number (`8`, not `"8"`). The opacity of a color is saved beside it, as `overlayBackgroundColor_opacity`, a number from 0 to 1.
* **Send only keys the schema lists.** The gateway checks the object before it saves it: a key it does not know, or a value of the wrong kind, makes the call fail and nothing is saved. Read the schema rather than guessing a name.
* **The next new composition starts from them.** The settings saved last on any composition of a widget are where a new composition of the same widget starts, unless you give it `embedSettings` of its own. A composition filed under a [managed user](https://docs.widgetic.com/docs/partners/managed-users.md) is the exception: its settings stay with it, and no other composition starts from them.
* **Build the tag from the settings** the way [Compositions](https://docs.widgetic.com/docs/api/compositions.md#embedding-compositions) shows, or hand the job to an assistant: `get_embed_settings` and `save_embed_settings` read and save them, `apply_embed_code` copies them from a code you paste, and `get_embed_code` returns the standard JavaScript embed code (or an iframe, for a page that cannot run a script), plus a share link. See [Embed settings](https://docs.widgetic.com/docs/agents/tools.md#embed-settings) in the MCP tools.

## Use the npm package

If you bundle your own JavaScript (Vite, webpack, Next.js) and would rather not add a third-party script tag, the same code is on npm:

```bash theme={"theme":"css-variables"}
npm install @widgetic/embed-sdk
```

```javascript theme={"theme":"css-variables"}
import { getSDK } from '@widgetic/embed-sdk';

// Loading the package in a browser starts the embed script: it finds the <widgetic> tags and watches for new ones.
// Calling getSDK() keeps your bundler from dropping the import.
getSDK();
```

Load it in the browser only: it scans the page, and a server has no page. In React or Next.js, call it from an effect: `import('@widgetic/embed-sdk').then(({ getSDK }) => getSDK())`. The `<widgetic>` tag and its attributes are the same as above. For a page, a CMS or a template where you can add a `<script>` tag, the script from the CDN stays the supported way. It is published separately from the package, so it can be a different build from the version you installed.

## Allowed domains

By default, a published widget can appear on any website. You can limit it:

1. **Dashboard:** workspace **Settings → Allowed domains** (team workspaces: admins and owners only).
2. **API:** `GET` and `POST /organizations/{orgId}/domains`, `PUT` and `DELETE /organizations/{orgId}/domains/{domainId}` (`orgId` is the id of the workspace) — see [Domain validation](https://docs.widgetic.com/resources/security.md#domain-validation) and the matching `widgetic api organizations …` commands. The routes under `/users/{id}/workspace/domains` are gone.

`localhost`, Widgetic's own sites and (for partners) your **platform domain** are always allowed. A page on another site gets `403 DOMAIN_NOT_ALLOWED`; the embed script draws nothing and sets `data-widgetic-refused="DOMAIN_NOT_ALLOWED"`.

Partners can also set `allowedDomains` on the partner account; those domains are added to the allowed domains of your partner's workspace, which is where your customers' embeds are filed.

## Events from the embed

The `<widgetic>` tag fires bubbling DOM events (same names as inside the iframe), including:

| Event | When |
| - | - |
| `widgetic:ready` | The composition is shown |
| `widgetic:error` | Lookup failed (`detail.code` is lower case, e.g. `domain_not_allowed`) |
| `widgetic:submitted` | A visitor submitted something the widget keeps |
| `widgetic:resize` | The box changed size |
| `widgetic:autoscale` | Autoscale changed the height of the box (`detail` has `compositionId`, `oldHeight`, `newHeight` and `contentHeight`) |
| `widgetic:data` | Visitor data changed |
| `widgetic:interaction` | An in-widget action |
| `widgetic:adaptive` | Window crossed the `adaptive` breakpoint |

Workflow embeds also fire `widgetic:workflow-step`. Send page events into a workflow with `sendWorkflowEvent` from `@widgetic/embed-sdk` or `widgetic:workflow-event` on the tag — see [Embed a workflow](https://docs.widgetic.com/docs/api/embedding-workflows.md#what-moves-a-visitor).

A pop-up or a floating box announces itself on `document`, with the id of the composition in `detail.widgetId` (a second tag for the same composition on the page gets `-2` after the id, a third `-3`):

| Event | When |
| - | - |
| `widgetic:overlay-shown` | The pop-up, or the floating box, opened |
| `widgetic:overlay-hidden` | It was closed and the widget went back to its place |
| `widgetic:overlay-blocked` | A rule or a view limit kept it closed. `detail.reason` is `show-rules` or `recurrence-limit` |

```javascript theme={"theme":"css-variables"}
document.addEventListener('widgetic:overlay-blocked', (event) => {
  // For example { widgetId: 'COMPOSITION_ID', reason: 'recurrence-limit' }
  console.log('The pop-up stayed closed', event.detail);
});
```

## If the widget does not appear

| What you see | Why | What to do |
| - | - | - |
| Nothing, and the tag has `data-widgetic-refused="DOMAIN_NOT_ALLOWED"` | The website is not on your list of allowed websites | Add it, see [Allowed domains](#allowed-domains) |
| Nothing, and the tag has `data-device-restricted="true"` | **Display on mobile** or **Display on desktop** is off for this kind of device | Turn it on, or try another device |
| An Overlay embed shows nothing | By design: the tag stays hidden until its trigger opens it. `data-widgetic-overlay` is `pending` until the script has found the composition and `armed` after | Check the trigger. With **Manual**, call `Widgetic.show` |
| A pop-up opened before and not any more | A view limit, **After close**, **After open** or another rule keeps it closed | Listen for `widgetic:overlay-blocked`. To try again, clear the site's data in the browser or use a private window |
| The tag stays empty | The composition's widget has no published version (its public address answers `404`), or the id in the tag is wrong | Publish the widget, and check `composition-id` |

## Related

> [card] [Embed settings](https://docs.widgetic.com/docs/api/embed-settings.md)
>
> Every option of the Embed code screen, with its key and its attribute

> [card] [Embed a workflow](https://docs.widgetic.com/docs/api/embedding-workflows.md)
>
> Show a path of compositions in one place, or in a frame

> [card] [Compositions API](https://docs.widgetic.com/docs/api/compositions.md)
>
> Create and publish compositions your embed points at

> [card] [Security](https://docs.widgetic.com/resources/security.md)
>
> Domains, keys in the browser and webhook signatures


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.