tessl install tessl/npm-posthog-js@1.335.0PostHog Browser JS Library is a comprehensive browser analytics and feature management SDK that enables developers to capture user events, track product analytics, manage feature flags, record session replays, and implement feedback mechanisms like surveys and conversations in web applications.
Conversations provides an interactive chat widget that enables real-time communication between your application and users. It supports displaying messages, collecting feedback, and managing conversation state.
Note: The Conversations API is lazy-loaded and may not be immediately available after initialization.
Displays the conversations widget on the page.
/**
* Shows the conversations widget
* The widget will be displayed as a floating chat interface
*/
function show(): void;Usage Examples:
// Show conversations widget
posthog.conversations.show();
// Show on button click
button.addEventListener('click', () => {
posthog.conversations.show();
});
// Show after user action
function openSupport() {
posthog.conversations.show();
posthog.capture('support_opened');
}Hides the conversations widget from the page.
/**
* Hides the conversations widget
*/
function hide(): void;Usage Examples:
// Hide conversations widget
posthog.conversations.hide();
// Hide on close button
closeButton.addEventListener('click', () => {
posthog.conversations.hide();
});Resets the conversations state, clearing any cached data.
/**
* Resets conversations state
* Useful when user logs out or switches accounts
*/
function reset(): void;Usage Examples:
// Reset on logout
function logout() {
posthog.conversations.reset();
posthog.reset();
}
// Reset when switching accounts
function switchAccount(newAccountId) {
posthog.conversations.reset();
posthog.identify(newAccountId);
}Conversations can be enabled or disabled during initialization:
posthog.init('<your-project-api-key>', {
disable_conversations: false // Enable conversations (default: false)
});/**
* Remote configuration for conversations loaded from PostHog
*/
interface ConversationsRemoteConfig {
/**
* Whether conversations are enabled
*/
enabled?: boolean;
/**
* Widget appearance configuration
*/
appearance?: {
primaryColor?: string;
backgroundColor?: string;
textColor?: string;
position?: 'left' | 'right';
};
/**
* Auto-open settings
*/
autoOpen?: boolean;
/**
* Welcome message
*/
welcomeMessage?: string;
}/**
* User traits that can be provided to conversations
*/
interface UserProvidedTraits {
/**
* User's name
*/
name?: string;
/**
* User's email
*/
email?: string;
/**
* Additional custom properties
*/
[key: string]: any;
}// Show conversations for support
function ContactSupport() {
return (
<button onClick={() => {
posthog.conversations.show();
posthog.capture('support_widget_opened');
}}>
Contact Support
</button>
);
}// Show conversations only to authenticated users
if (user.isAuthenticated) {
posthog.identify(user.id, {
email: user.email,
name: user.name
});
// Enable conversations for authenticated users
if (user.needsHelp) {
posthog.conversations.show();
}
}// Track conversation state
let conversationsOpen = false;
function toggleConversations() {
if (conversationsOpen) {
posthog.conversations.hide();
conversationsOpen = false;
} else {
posthog.conversations.show();
conversationsOpen = true;
}
}// Auto-open conversations for first-time users
posthog.onFeatureFlags(() => {
const isNewUser = !posthog.get_property('has_opened_conversations');
if (isNewUser) {
setTimeout(() => {
posthog.conversations.show();
posthog.register({ has_opened_conversations: true });
}, 3000); // Wait 3 seconds before showing
}
});// Show conversations with context
function showHelpForFeature(featureName: string) {
// Set context before showing conversations
posthog.capture('help_requested', {
feature: featureName,
page: window.location.pathname
});
posthog.conversations.show();
}// Add a floating chat button
function ChatButton() {
const [isOpen, setIsOpen] = useState(false);
const toggleChat = () => {
if (isOpen) {
posthog.conversations.hide();
} else {
posthog.conversations.show();
}
setIsOpen(!isOpen);
};
return (
<button
className="chat-button"
onClick={toggleChat}
aria-label="Open chat"
>
{isOpen ? 'Close' : 'Chat'}
</button>
);
}// Properly clean up conversations on logout
function handleUserLogout() {
// Hide widget
posthog.conversations.hide();
// Reset conversations state
posthog.conversations.reset();
// Reset PostHog state
posthog.reset();
// Clear user session
clearUserSession();
}