Skip to main content
The widget supports bidirectional communication between the parent page and the embedded chat iframe using the postMessage API. This enables rich integrations where the parent application can send context to the widget and receive events back.

Overview

Features

  • Metadata Injection: Parent page can send arbitrary metadata to the widget
  • Event Notification: Widget notifies parent of user actions and state changes
  • Mobile State Detection: Automatic detection and handling of mobile/desktop states
  • Widget Mode Detection: Automatic detection when running in iframe vs standalone mode
  • Secure Communication: Origin validation for all messages

Parent to Widget Communication

Window Configuration

When embedding the widget, configure it through the window.AGO object before loading the widget script. These options control authentication, appearance, and behavior.

Updating JWT at Runtime

Use sendJwtToAGO() to update the JWT token after the widget has loaded. This is useful when the user logs in after the widget is already open, or when the JWT token is refreshed.
This updates the JWT in both the chat iframe and the notification iframe, so unread counts are fetched with the correct identity.

Updating Auth Token at Runtime

Use sendAuthTokenToAGO() to pass an external authentication token to the widget. This token is forwarded to agent tools (e.g., HTTP request tool) as the X-Auth-Token header, allowing agents to call your authenticated APIs on behalf of the user.

Sending Metadata

Use the sendMetadataToAGO function to pass context from your application to the widget.

Metadata Fields Reference

Usage in React Native WebView


Widget to Parent Communication

Setting Up the Event Listener

The parent page should listen for messages from the widget:

Built-in Message Types

CLOSE_CHAT

Sent when the user clicks the close button or requests to minimize the widget.
Use Case: Hide the widget container or minimize to a button.

THREAD_CREATED

Sent when a new conversation thread is started.
Use Case: Track conversation starts in your analytics.

TICKET_CREATED

Sent when a support ticket is created from the conversation.
Use Case: Show confirmation or update your ticket tracking.
Sent when the widget requests navigation to a specific page.
Use Case: Handle in-app navigation from agent responses.

UNREAD_STAFF_COUNT

Sent periodically (every 30 seconds) by the notification frame to inform the parent page of unread staff messages. The notification frame is a hidden iframe that polls for unread messages. It only reports counts for users who have an existing widget session (i.e., a widget ID in localStorage from a previous chat interaction) — new visitors with no prior conversations will not trigger any polls. Enabling notifications: Set notifications: true in the window.AGO configuration object:
When enabled, the standard widget automatically displays a red badge on the chat button. If you build a custom integration, listen for the event yourself:
Use Case: Display an unread message badge on your chat button.
JWT authentication: If your widget uses JWT authentication, call sendJwtToAGO(token) whenever the token is refreshed. This updates both the chat iframe and the notification frame so that unread counts are fetched with the correct identity.

Complete Integration Examples

Basic Web Integration

React Integration


Ticket Integration

When tickets are created from conversations, metadata is automatically included and formatted for support agents.

HelpScout Integration

Metadata appears as formatted JSON in private comments:

Zendesk Integration

Metadata is added to custom ticket fields and internal notes, providing support agents with comprehensive context.

Security Considerations

Origin Validation

Always validate the message origin:

Sensitive Data

  • Do not send: Passwords, API keys, tokens, credit card numbers
  • Safe to send: User IDs, order IDs, plan names, page context, preferences

Content Security Policy

If using CSP, ensure your policy allows communication with the widget:

Troubleshooting

  1. Ensure the widget script has finished loading before calling sendMetadataToAGO
  2. Check browser console for errors
  3. Verify sendMetadataToAGO is available globally (it is defined by frame.js)
  1. Check origin validation in your event listener
  2. Ensure widget domain is correct
  3. Check browser console for blocked cross-origin messages
  1. Verify metadata was sent before ticket creation
  2. Check that metadata format is valid JSON
  3. Ensure ticketing integration is properly configured

Browser Compatibility

The postMessage API is supported in all modern browsers: No polyfills required.