Brainfish In-App Widget
This package provides a simple way to integrate a Brainfish in-app widget in your website or web application. It supports popup widgets with contextual help functionality.
Installation
npm install @brainfish-ai/web-widget
Directly in your HTML
<script type="module">
import Brainfish from "https://cdn.jsdelivr.net/npm/@brainfish-ai/web-widget@latest/dist/web.js"
Brainfish.Widgets.init({ widgetKey: "your-key" });
</script>
The popup widget provides a floating button that opens a search interface in a modal overlay.
Basic Usage
<script>
Brainfish.Widgets.init({
widgetKey: "your-widget-key",
environment: "production"
});
</script>
Widget Control API
Brainfish.Widgets.open();
Brainfish.Widgets.open({
position: 'bottom-right'
});
Brainfish.Widgets.close();
Brainfish.Widgets.createNewThread();
if (Brainfish.Widgets.isReady) {
}
User Management
Brainfish.Widgets.identify({
name: "John Doe",
email: "john@example.com",
userId: "user_123"
});
Brainfish.Widgets.setSecretAttributes("internal_data_123");
Customization with Overrides
Customize your widget's appearance and behavior using the overrides configuration:
Brainfish.Widgets.init({
widgetKey: "your-widget-key",
overrides: {
suggestions: [
'How do I reset my password?',
'How do I contact support?',
'What are your pricing plans?'
],
theme: {
inputButtonBgColor: '#3b82f6',
inputButtonColor: '#ffffff'
},
panelTitle: 'Help Center',
placeholder: 'Ask a question...',
bodyActionButtons: [
{
icon: 'Calendar',
type: 'link',
label: 'Book a Demo',
value: 'https://calendly.com/your-company'
}
]
}
});
📚 For complete customization options, see:
Custom Positioning
Both open() and onContextHelp() methods support custom positioning to control where the widget appears on screen. This is useful for creating contextual experiences or avoiding UI conflicts.
Available Positions:
'bottom-left' - Widget appears in bottom-left corner (default)
'bottom-right' - Widget appears in bottom-right corner
'top-left' - Widget appears in top-left corner
'top-right' - Widget appears in top-right corner
Usage Examples:
Brainfish.Widgets.open({ position: 'top-right' });
Brainfish.Widgets.open({ position: 'bottom-left' });
Brainfish.Widgets.onContextHelp("How do I reset my password?", {
position: 'top-left'
});
function showHelpNearElement(question, element) {
const rect = element.getBoundingClientRect();
const position = rect.left < window.innerWidth / 2 ? 'bottom-left' : 'bottom-right';
Brainfish.Widgets.onContextHelp(question, { position });
}
Contextual Help Feature
The contextual help feature allows you to open the widget with a pre-filled question based on user interaction.
Brainfish.Widgets.onContextHelp("How do I reset my password?");
Brainfish.Widgets.onContextHelp("How do I reset my password?", {
position: 'top-left'
});
Implementation Examples
HTML with Help Icons:
<div class="form-field">
<label>Email Address</label>
<input type="email" value="user@example.com">
<button class="help-icon"
data-question="How do I change my email address?"
data-position="top-left">
<i class="ph ph-question"></i>
</button>
</div>
<script>
document.querySelectorAll('.help-icon').forEach(icon => {
icon.addEventListener('click', (e) => {
const question = e.target.dataset.question;
const position = e.target.dataset.position || 'bottom-right';
Brainfish.Widgets.onContextHelp(question, { position });
});
});
</script>
Direct Button Integration:
<button onclick="Brainfish.Widgets.onContextHelp('How do I reset my password?', { position: 'top-right' })">
Reset Password
</button>
Programmatic Usage:
function showHelp(question, position = 'bottom-right') {
Brainfish.Widgets.onContextHelp(question, { position });
}
showHelp("How do I update my profile?", 'top-left');
showHelp("How do I contact support?", 'bottom-right');
Event Listeners
The widget emits events when it opens and closes. You can listen to these events:
const controller = new AbortController();
const { signal } = controller;
window.addEventListener('onBrainfishWidgetOpen', (event) => {
console.log('Widget opened');
}, { signal });
window.addEventListener('onBrainfishWidgetClose', (event) => {
console.log('Widget closed');
}, { signal });
controller.abort();
Analytics Events
The widget automatically tracks analytics events:
- Open Widget - When the widget is opened
- Open Widget - Contextual Help - When opened via contextual help
- Open Widget - Custom Trigger - When opened with custom trigger
- Close Widget - When the widget is closed
Build
Run the following command to build the widget:
yarn build:web-widget
Testing locally
Start the local server
cd packages/web-widget
npx serve ./ -p 8000 -C
Open the Playground
The playground provides a comprehensive testing environment for the Brainfish widget:
open http://localhost:8000/playground/index.html
The playground includes:
- Widget Configuration - Set environment, API key, and widget settings
- Contextual Help Testing - Test the new contextual help feature with sample questions
- Real-world Demo - See how customers would implement contextual help in their apps
- API Testing - Test all widget methods and functionality
- Live Logs - Monitor widget events and debugging information
Test Different Environments
You can test the widget in different environments by passing the env parameter:
open http://localhost:8000/playground/index.html?env=local
open http://localhost:8000/playground/index.html?env=staging
open http://localhost:8000/playground/index.html?env=prod
Agent Widget Development
If you want to test with the agent widget (Next.js app), start it locally:
cd packages/agent-widget
npm run dev
This will run the agent widget on port 3000, which the playground will automatically connect to in local development mode.