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 thewindow.AGO object before loading the widget script. These options control authentication, appearance, and behavior.
Updating JWT at Runtime
UsesendJwtToAGO() 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.
Updating Auth Token at Runtime
UsesendAuthTokenToAGO() 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 thesendMetadataToAGO 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.THREAD_CREATED
Sent when a new conversation thread is started.TICKET_CREATED
Sent when a support ticket is created from the conversation.NAVIGATE
Sent when the widget requests navigation to a specific page.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: Setnotifications: true in the window.AGO configuration object:
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
Widget Not Receiving Metadata
Widget Not Receiving Metadata
- Ensure the widget script has finished loading before calling
sendMetadataToAGO - Check browser console for errors
- Verify
sendMetadataToAGOis available globally (it is defined byframe.js)
Messages Not Being Received
Messages Not Being Received
- Check origin validation in your event listener
- Ensure widget domain is correct
- Check browser console for blocked cross-origin messages
Metadata Not Appearing in Tickets
Metadata Not Appearing in Tickets
- Verify metadata was sent before ticket creation
- Check that metadata format is valid JSON
- Ensure ticketing integration is properly configured
Browser Compatibility
ThepostMessage API is supported in all modern browsers:
No polyfills required.
Related Documentation
- Widget Authentication - Configure widget security
- Widget Configuration - Widget appearance and behavior
- Ticketing Tool - Ticket creation from conversations
- Mobile Authentication - Mobile SDK authentication
