()
| 5 | import { useEffect, useRef } from 'react'; |
| 6 | |
| 7 | export function ScrollTracker() { |
| 8 | const { track } = useOpenPanel(); |
| 9 | const pathname = usePathname(); |
| 10 | const hasFired = useRef(false); |
| 11 | |
| 12 | useEffect(() => { |
| 13 | hasFired.current = false; |
| 14 | |
| 15 | const handleScroll = () => { |
| 16 | if (hasFired.current) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | const scrollTop = window.scrollY; |
| 21 | const docHeight = |
| 22 | document.documentElement.scrollHeight - window.innerHeight; |
| 23 | const percent = |
| 24 | docHeight > 0 ? (scrollTop / docHeight) * 100 : 0; |
| 25 | |
| 26 | if (percent >= 50) { |
| 27 | hasFired.current = true; |
| 28 | track('scroll_half_way', { percent: Math.round(percent) }); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | window.addEventListener('scroll', handleScroll, { passive: true }); |
| 33 | return () => window.removeEventListener('scroll', handleScroll); |
| 34 | }, [track, pathname]); |
| 35 | |
| 36 | return null; |
| 37 | } |
nothing calls this directly
no test coverage detected