Control Widget
Control Widget
Overview
The Nexvio.ai JavaScript SDK provides methods to programmatically control your chatbot widget. This allows you to open, close, show, hide, and customize the widget based on user actions or other events on your website.
Basic Widget Controls
Opening and Closing the Widget
You can programmatically open and close the chatbot widget:
// Open the chatbot widgetwindow.nexvio('open');
// Close the chatbot widgetwindow.nexvio('close');
// Toggle the widget (open if closed, close if open)window.nexvio('toggle');Showing and Hiding the Widget
You can control the visibility of the widget:
// Hide the chatbot widget completelywindow.nexvio('hide');
// Show the chatbot widget (makes it visible again after hiding)window.nexvio('show');The difference between close and hide:
closecloses the chatbot window but keeps the launcher button visiblehidecompletely removes the chatbot from view (both window and launcher)
Advanced Widget Controls
Setting the Position
You can change the position of the widget:
// Position the widget in the bottom-right corner (default)window.nexvio('setPosition', 'bottom-right');
// Position the widget in the bottom-left cornerwindow.nexvio('setPosition', 'bottom-left');
// Position the widget in the top-right cornerwindow.nexvio('setPosition', 'top-right');
// Position the widget in the top-left cornerwindow.nexvio('setPosition', 'top-left');Customizing the Widget Size
You can adjust the size of the chatbot window:
// Set custom dimensions (width and height in pixels)window.nexvio('setSize', { width: 400, // in pixels height: 600 // in pixels});
// Reset to default sizewindow.nexvio('resetSize');Setting Custom Colors
You can update the widget color scheme programmatically:
// Change the primary color of the widgetwindow.nexvio('updateColors', { primary: '#FF5733', // Main color for buttons and accents secondary: '#427bf5', // Secondary color for certain elements background: '#ffffff' // Background color of the chat window});Practical Examples
Example 1: Opening the Chatbot After Page Load Delay
// Wait 5 seconds after page load, then open the chatbotdocument.addEventListener('DOMContentLoaded', function() { setTimeout(function() { window.nexvio('open'); }, 5000); // 5000 milliseconds = 5 seconds});Example 2: Opening the Chatbot Based on Scroll Depth
// Open the chatbot when the user scrolls 70% down the pagewindow.addEventListener('scroll', function() { const scrollPosition = window.scrollY; const totalHeight = document.body.scrollHeight - window.innerHeight; const scrollPercentage = (scrollPosition / totalHeight) * 100;
if (scrollPercentage > 70) { // Only open once if (!window.chatbotOpened) { window.nexvio('open'); window.chatbotOpened = true; } }});Example 3: Showing/Hiding Based on URL Path
// Hide the chatbot on certain pagesfunction manageChatbotVisibility() { const currentPath = window.location.pathname;
// Hide on checkout and login pages if (currentPath.includes('/checkout') || currentPath.includes('/login')) { window.nexvio('hide'); } else { window.nexvio('show'); }}
// Run on page loadmanageChatbotVisibility();
// For single-page applications, monitor URL changes// This is a simplified example - use proper router events in productionwindow.addEventListener('popstate', manageChatbotVisibility);Responding to User Inactivity
// Open the chatbot after 60 seconds of user inactivitylet inactivityTimer;
function resetInactivityTimer() { clearTimeout(inactivityTimer); inactivityTimer = setTimeout(function() { window.nexvio('open'); }, 60000); // 60000 milliseconds = 60 seconds}
// Reset the timer on user interaction['mousemove', 'mousedown', 'keypress', 'scroll', 'touchstart'].forEach(function(event) { document.addEventListener(event, resetInactivityTimer, true);});
// Start the timer when the page loadsresetInactivityTimer();Controlling the Widget on Mobile Devices
Responsive behavior considerations:
// Adjust widget size based on screen widthfunction adjustChatbotSize() { if (window.innerWidth < 768) { // Mobile breakpoint window.nexvio('setSize', { width: 320, height: 500 }); } else { // Desktop window.nexvio('setSize', { width: 400, height: 600 }); }}
// Run on page loadadjustChatbotSize();
// Run when window is resizedwindow.addEventListener('resize', adjustChatbotSize);Combining with Event Listeners
For more advanced use cases, you can combine widget controls with event listeners:
// Open the chatbot when a specific button is clickeddocument.getElementById('support-button').addEventListener('click', function() { window.nexvio('open');});
// Listen for chatbot open eventswindow.nexvio('on', 'open', function() { console.log('Chatbot was opened'); // Track analytics event if (typeof gtag === 'function') { gtag('event', 'chatbot_opened'); }});
// Listen for chatbot close eventswindow.nexvio('on', 'close', function() { console.log('Chatbot was closed');});Widget Visibility on Page Navigation
For single-page applications:
// Function to update chatbot visibility based on routefunction updateChatbotOnRouteChange() { const currentPath = window.location.pathname;
// Show or hide based on path if (currentPath === '/help' || currentPath === '/support') { // Auto-open on help/support pages window.nexvio('show'); window.nexvio('open'); } else if (currentPath === '/checkout') { // Hide completely on checkout window.nexvio('hide'); } else { // Default: show but closed window.nexvio('show'); window.nexvio('close'); }}
// For most single-page application frameworks,// you would hook this into your router's navigation eventsTips and Best Practices
-
Avoid Disrupting User Flow: Don’t open the chatbot at crucial moments like when users are filling out forms.
-
Mobile Considerations: Be extra careful with size and positioning on mobile devices to avoid obscuring content.
-
User Preferences: Consider storing the user’s preference (like if they manually closed the widget) in localStorage and respecting it on subsequent visits.
-
Performance: Avoid making frequent or rapid changes to the widget as it may impact page performance.
-
Combine with Analytics: Track when and why your widget is being shown, opened, or used to optimize its implementation.
// Example of respecting user preferencewindow.nexvio('on', 'close', function() { // Store that user manually closed the widget localStorage.setItem('chatbotClosed', 'true');});
// On page loaddocument.addEventListener('DOMContentLoaded', function() { if (localStorage.getItem('chatbotClosed') !== 'true') { // Only auto-open if user didn't manually close it before setTimeout(function() { window.nexvio('open'); }, 3000); }});