import { useEffect, useCallback, useRef } from 'react'; import { behavioralTracker } from '../tracker'; import type { BehavioralEvent, EventType } from '../types'; export const useBehavioralTracking = () => { const scrollDepthRef = useRef(0); const interactionCountRef = useRef(0); useEffect(() => { // Track page view on mount behavioralTracker.trackPageView(); behavioralTracker.resetPageTimer(); // Track scroll depth const handleScroll = () => { const scrollPercent = Math.round( ((window.scrollY + window.innerHeight) / document.documentElement.scrollHeight) * 100 ); if (scrollPercent > scrollDepthRef.current) { scrollDepthRef.current = scrollPercent; // Track at 25%, 50%, 75%, 100% if ([25, 50, 75, 100].includes(scrollPercent)) { behavioralTracker.track('scroll_depth', { scroll_depth_percent: scrollPercent, }); } } }; // Track interactions const handleClick = () => { interactionCountRef.current++; }; // Track time spent on page unload const handleBeforeUnload = () => { const timeSpent = behavioralTracker.getTimeSpent(); // Use sendBeacon for reliable tracking if (navigator.sendBeacon && timeSpent > 0) { const data = JSON.stringify({ event_type: 'time_spent', time_spent_seconds: timeSpent, scroll_depth_percent: scrollDepthRef.current, interaction_count: interactionCountRef.current, consent_given: behavioralTracker.hasConsent(), }); const blob = new Blob([data], { type: 'application/json' }); const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'; navigator.sendBeacon(`${apiUrl}/api/behavioral/track`, blob); } }; window.addEventListener('scroll', handleScroll, { passive: true }); document.addEventListener('click', handleClick); window.addEventListener('beforeunload', handleBeforeUnload); return () => { window.removeEventListener('scroll', handleScroll); document.removeEventListener('click', handleClick); window.removeEventListener('beforeunload', handleBeforeUnload); }; }, []); const trackEvent = useCallback( async (eventType: EventType, data?: Partial) => { return behavioralTracker.track(eventType, data); }, [] ); const trackProductView = useCallback((productId: number, price?: number) => { return behavioralTracker.trackProductView(productId, price); }, []); const trackAddToCart = useCallback( (productId: number, quantity: number, price: number) => { return behavioralTracker.trackAddToCart(productId, quantity, price); }, [] ); const trackRemoveFromCart = useCallback((productId: number) => { return behavioralTracker.trackRemoveFromCart(productId); }, []); const trackUpdateCart = useCallback( (productId: number, quantity: number, price: number) => { return behavioralTracker.trackUpdateCart(productId, quantity, price); }, [] ); const trackSearch = useCallback((query: string, resultsCount: number) => { return behavioralTracker.trackSearch(query, resultsCount); }, []); const trackOrderPlaced = useCallback((orderId: number, total?: number) => { return behavioralTracker.trackOrderPlaced(orderId, total); }, []); const trackCheckoutInitiated = useCallback((itemCount?: number, total?: number) => { return behavioralTracker.trackCheckoutInitiated(itemCount, total); }, []); const trackAddToWishlist = useCallback((productId: number) => { return behavioralTracker.trackAddToWishlist(productId); }, []); const trackRemoveFromWishlist = useCallback((productId: number) => { return behavioralTracker.trackRemoveFromWishlist(productId); }, []); const trackCategoryView = useCallback((categoryId: number) => { return behavioralTracker.trackCategoryView(categoryId); }, []); const trackReviewSubmitted = useCallback((productId: number, rating?: number) => { return behavioralTracker.trackReviewSubmitted(productId, rating); }, []); const trackShareProduct = useCallback((productId: number, platform?: string) => { return behavioralTracker.trackShareProduct(productId, platform); }, []); return { trackEvent, trackProductView, trackAddToCart, trackRemoveFromCart, trackUpdateCart, trackSearch, trackOrderPlaced, trackCheckoutInitiated, trackAddToWishlist, trackRemoveFromWishlist, trackCategoryView, trackReviewSubmitted, trackShareProduct, }; };