widget.mdx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. ---
  2. title: "Website Widget"
  3. description: "Embed the Agencii chat widget on any site in minutes."
  4. icon: "window"
  5. ---
  6. ## Feature Overview
  7. - Launch your existing agent as an embeddable web widget with zero additional hosting.
  8. - Switch agencies on the fly by updating the widget in the dashboard — no code redeploy needed.
  9. - Personalize every deployment with optional `additionalInstructions` that shape the assistant's behavior per site.
  10. ![Widget preview](/images/integrations/widget/widget_1.png)
  11. ## Prerequisites
  12. - Deployed Agency in the Agencii dashboard.
  13. ## Quick Start
  14. <Steps>
  15. <Step title="Create a new widget">
  16. 1. In the Agencii dashboard, open **Deploy → Widgets** and create a new widget.
  17. 2. Configure the widget's name, description, position and conversation starters.
  18. 3. Customize the widget's appearance by choosing a theme mode, logo, and color scheme.
  19. 4. Select the agency and any selectable agents.
  20. 5. Configure advanced settings such as Allowed Domains and auto-open behavior as needed.
  21. 6. Save changes.
  22. 7. Test your widget!
  23. </Step>
  24. <Step title="Embed the widget">
  25. 1. In the Agencii dashboard, open **Deploy → Widgets** and find the widget you created.
  26. 2. Click the three-dot menu on the right side of the widget row and select **Embed**.
  27. ![Embed widget](/images/integrations/widget/widget_2.png)
  28. 3. Copy the embedding script.
  29. 4. Paste the script into your website's HTML file (or adjust the script to your needs).
  30. 5. Load the page and confirm the widget works as expected.
  31. </Step>
  32. <Step title="Share the widget">
  33. 1. In the Agencii dashboard, open **Deploy → Widgets** and find the widget you created.
  34. 2. Click the **Share** button on the right side of the widget row.
  35. 3. This opens a new window with the widget embedded on a blank page.
  36. ![Live widget embedded on a landing page](/images/integrations/widget/widget_3.png)
  37. </Step>
  38. </Steps>
  39. <Tip>
  40. You can embed several widgets on the same page by using the embedding script multiple times with different `widgetId`s and `chatContainer` selectors.
  41. </Tip>
  42. <Tip>
  43. If you are prototyping locally, temporarily add `http://localhost:3000` (or your dev origin) to the widget's allowed domains list if you are using the Allowed Domains feature.
  44. </Tip>
  45. ## Widget Controls
  46. You can control the widget from your site/app by dispatching a `CustomEvent` named **"widget-control"** on `window` with a `detail` payload. The widget listens for this event and performs the requested action.
  47. **Event name:** `widget-control`
  48. <Accordion title="Payload shape (TypeScript)" defaultOpen={false}>
  49. ```tsx
  50. interface WidgetControlEventDetail {
  51. /** Command name */
  52. type: 'open-widget' | 'send-message-to-widget' | 'set-widget-context';
  53. /** Command-specific payload (not used for set-widget-context) */
  54. data?: boolean | string;
  55. /** Target widget instance */
  56. widgetId: string; // e.g., 'WIDGET_123'
  57. /** Structured data passed to Agency Context */
  58. userContext?: Record<string, unknown>;
  59. /** Extra guidance for the agent */
  60. additionalInstructions?: string;
  61. }
  62. ```
  63. **Dispatch pattern:**
  64. ```tsx
  65. window.dispatchEvent(
  66. new CustomEvent<WidgetControlEventDetail>('widget-control', {
  67. detail: { type, data, widgetId },
  68. })
  69. );
  70. ```
  71. </Accordion>
  72. <Note>
  73. The widget must be initialized on the page. Replace `WIDGET_ID` with the identifier from your embed script.
  74. </Note>
  75. ## Commands
  76. ### Open or close the widget
  77. Opens or closes the widget UI.
  78. **Type:** `open-widget`
  79. **Data:** `boolean` — `true` to **open**, `false` to **close**.
  80. <Accordion title="Examples" defaultOpen={false}>
  81. <CodeGroup>
  82. ```jsx Open
  83. window.dispatchEvent(
  84. new CustomEvent('widget-control', {
  85. detail: {
  86. type: 'open-widget',
  87. data: true,
  88. widgetId: WIDGET_ID,
  89. },
  90. })
  91. );
  92. ```
  93. ```jsx Close
  94. window.dispatchEvent(
  95. new CustomEvent('widget-control', {
  96. detail: {
  97. type: 'open-widget',
  98. data: false,
  99. widgetId: WIDGET_ID,
  100. },
  101. })
  102. );
  103. ```
  104. </CodeGroup>
  105. </Accordion>
  106. #### Typical use cases
  107. - Bind to a "Chat" / "Support" button to open the widget.
  108. - Close the widget after a route change or when a modal shows.
  109. ### Set widget context
  110. Sets persistent context for **all future messages** — both programmatic (`send-message-to-widget`) and user-typed.
  111. **Type:** `set-widget-context`
  112. | Property | Type | Required | Description |
  113. | --- | --- | --- | --- |
  114. | `userContext` | object | No | Structured data (user IDs, preferences, feature flags) passed to [Agency Context](/additional-features/agency-context). Accessible within tools but **not** exposed to the LLM. |
  115. | `additionalInstructions` | string | No | Extra guidance for the agent on all subsequent requests. |
  116. <Accordion title="Example" defaultOpen={false}>
  117. ```jsx
  118. window.dispatchEvent(
  119. new CustomEvent('widget-control', {
  120. detail: {
  121. type: 'set-widget-context',
  122. widgetId: WIDGET_ID,
  123. userContext: { customerId: '123' },
  124. additionalInstructions: 'Be helpful',
  125. },
  126. })
  127. );
  128. ```
  129. </Accordion>
  130. <Tip>
  131. Use `set-widget-context` once on page load to inject user-specific data (e.g., customer ID, subscription tier) so every conversation automatically has access to it.
  132. </Tip>
  133. ### Send a message to the widget
  134. Programmatically sends a text message into the widget (e.g., seed a greeting or pass context).
  135. This event does not open the widget automatically, so dispatch an `open-widget` event first.
  136. **Type:** `send-message-to-widget`
  137. | Property | Type | Required | Description |
  138. | --- | --- | --- | --- |
  139. | `data` | string | Yes | The message body to send. |
  140. | `userContext` | object | No | Structured data (user IDs, preferences, feature flags) passed to [Agency Context](/additional-features/agency-context). Accessible within tools but **not** exposed to the LLM. **Overrides** any context set via `set-widget-context`. |
  141. | `additionalInstructions` | string | No | Extra guidance for the agent on this specific request. **Overrides** any instructions set via `set-widget-context`. |
  142. <Accordion title="Examples" defaultOpen={false}>
  143. <CodeGroup>
  144. ```jsx Basic
  145. window.dispatchEvent(
  146. new CustomEvent('widget-control', {
  147. detail: {
  148. type: 'send-message-to-widget',
  149. data: 'Hi',
  150. widgetId: WIDGET_ID,
  151. },
  152. })
  153. );
  154. ```
  155. ```jsx With Context
  156. window.dispatchEvent(
  157. new CustomEvent('widget-control', {
  158. detail: {
  159. type: 'send-message-to-widget',
  160. data: 'Hello world',
  161. widgetId: WIDGET_ID,
  162. userContext: { customerId: '123', tier: 'premium' },
  163. additionalInstructions: 'Be extra helpful',
  164. },
  165. })
  166. );
  167. ```
  168. </CodeGroup>
  169. </Accordion>
  170. ## Multiple Widgets on One Page
  171. If you embed multiple widget instances, dispatch events with the **specific** `widgetId` you want to control. The widget code should internally ignore events addressed to other IDs.
  172. ```jsx
  173. const SALES_WIDGET = 'WIDGET_SALES';
  174. const SUPPORT_WIDGET = 'WIDGET_SUPPORT';
  175. // Open Support, keep Sales unchanged
  176. window.dispatchEvent(new CustomEvent('widget-control', {
  177. detail: { type: 'open-widget', data: true, widgetId: SUPPORT_WIDGET },
  178. }));
  179. ```