![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/src/components/ |
import React, { useEffect, useRef, useCallback } from 'react';
import { useRouter } from 'next/router';
import { usePublicNotifications } from '@/context/PublicNotificationContext';
const SmartEngagement: React.FC = () => {
const router = useRouter();
const { visitorBehavior, showEducationalPrompt, showNewsletterPrompt, showGroupChatPrompt } = usePublicNotifications();
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
// Clear any existing timeout before setting a new one
const setSmartTimeout = useCallback((callback: () => void, delay: number) => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(callback, delay);
}, []);
// Cleanup timeout on unmount
useEffect(() => {
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);
useEffect(() => {
const currentPage = router.asPath;
const timeOnPage = visitorBehavior.timeOnSite;
// Page-specific engagement prompts
const pagePrompts = {
'/': {
timeThreshold: 45,
prompt: () => {
const rand = Math.random();
if (rand > 0.66) {
showEducationalPrompt('class-action');
} else if (rand > 0.33) {
showNewsletterPrompt();
} else {
showGroupChatPrompt();
}
}
},
'/faq': {
timeThreshold: 60,
prompt: () => {
if (Math.random() > 0.5) {
showEducationalPrompt('rights');
} else {
showGroupChatPrompt();
}
}
},
'/about': {
timeThreshold: 30,
prompt: () => {
if (Math.random() > 0.5) {
showNewsletterPrompt();
} else {
showGroupChatPrompt();
}
}
},
'/class-action': {
timeThreshold: 90,
prompt: () => {
const rand = Math.random();
if (rand > 0.66) {
showNewsletterPrompt();
} else if (rand > 0.33) {
showEducationalPrompt('rights');
} else {
showGroupChatPrompt();
}
}
},
'/contact': {
timeThreshold: 20,
prompt: () => {
if (Math.random() > 0.5) {
showEducationalPrompt('class-action');
} else {
showGroupChatPrompt();
}
}
},
'/resources': {
timeThreshold: 45,
prompt: () => {
if (Math.random() > 0.5) {
showNewsletterPrompt();
} else {
showGroupChatPrompt();
}
}
},
'/privacy-policy': {
timeThreshold: 15,
prompt: () => showEducationalPrompt('rights')
},
'/terms': {
timeThreshold: 15,
prompt: () => showEducationalPrompt('class-action')
},
'/who': {
timeThreshold: 30,
prompt: () => showGroupChatPrompt()
}
};
// Check if we should show a page-specific prompt
const pageConfig = pagePrompts[currentPage as keyof typeof pagePrompts];
if (pageConfig && timeOnPage > pageConfig.timeThreshold && typeof window !== 'undefined') {
// Only show once per page visit
const promptKey = `page_prompt_${currentPage}_${visitorBehavior.sessionId}`;
if (!sessionStorage.getItem(promptKey)) {
sessionStorage.setItem(promptKey, 'shown');
// Delay prompt slightly to not interrupt reading
setSmartTimeout(() => {
pageConfig.prompt();
}, 2000);
}
}
}, [router.asPath]);
// Returning visitor detection and special prompts
useEffect(() => {
if (typeof window !== 'undefined') {
const isReturningVisitor = localStorage.getItem('hasVisitedBefore');
const visitCount = parseInt(localStorage.getItem('visitCount') || '1');
if (!isReturningVisitor) {
localStorage.setItem('hasVisitedBefore', 'true');
localStorage.setItem('visitCount', '1');
} else {
localStorage.setItem('visitCount', (visitCount + 1).toString());
}
// Special prompts for returning visitors - only once per session
if (isReturningVisitor && visitCount >= 3 && visitorBehavior.timeOnSite > 60) {
const returningPromptKey = `returning_visitor_${visitorBehavior.sessionId}`;
if (!sessionStorage.getItem(returningPromptKey)) {
sessionStorage.setItem(returningPromptKey, 'shown');
setSmartTimeout(() => {
if (!visitorBehavior.hasSignedUp) {
showNewsletterPrompt();
}
}, 5000);
}
}
}
}, []);
// Quebec-specific prompts
useEffect(() => {
if (typeof window !== 'undefined' && visitorBehavior.region === 'quebec' && visitorBehavior.timeOnSite > 30) {
const quebecPromptKey = `quebec_prompt_${visitorBehavior.sessionId}`;
if (!sessionStorage.getItem(quebecPromptKey)) {
sessionStorage.setItem(quebecPromptKey, 'shown');
// Quebec residents get priority messaging
setSmartTimeout(() => {
showEducationalPrompt('class-action');
}, 3000);
}
}
}, []);
// Multi-page visit engagement - only once per session
useEffect(() => {
if (typeof window !== 'undefined') {
const uniquePages = new Set(visitorBehavior.pagesVisited).size;
if (uniquePages >= 3 && visitorBehavior.timeOnSite > 180) {
const engagedPromptKey = `engaged_visitor_${visitorBehavior.sessionId}`;
if (!sessionStorage.getItem(engagedPromptKey)) {
sessionStorage.setItem(engagedPromptKey, 'shown');
// Delay to avoid conflicts with other prompts
setSmartTimeout(() => {
if (!visitorBehavior.hasSignedUp &&
!sessionStorage.getItem(`newsletter_prompt_${visitorBehavior.sessionId}`) &&
!sessionStorage.getItem(`groupchat_prompt_${visitorBehavior.sessionId}`)) {
// Show group chat to highly engaged visitors
if (Math.random() > 0.5) {
showGroupChatPrompt();
} else {
showNewsletterPrompt();
}
}
}, 2000);
}
}
}
}, []);
// This component doesn't render anything visible
return null;
};
export default SmartEngagement;