|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import SearchBar from '@theme-original/SearchBar'; |
| 3 | + |
| 4 | +export default function SearchBarWrapper(props: Record<string, unknown>) { |
| 5 | + useEffect(() => { |
| 6 | + // Track Algolia DocSearch events |
| 7 | + const trackSearch = (query: string) => { |
| 8 | + if (typeof window !== 'undefined' && window.gtag) { |
| 9 | + window.gtag('event', 'search', { |
| 10 | + search_term: query, |
| 11 | + event_category: 'engagement', |
| 12 | + event_label: 'documentation_search', |
| 13 | + }); |
| 14 | + } |
| 15 | + }; |
| 16 | + |
| 17 | + // Listen for DocSearch input events |
| 18 | + const handleDocSearchInput = (event: Event) => { |
| 19 | + const target = event.target as HTMLInputElement; |
| 20 | + if (target && target.value && target.value.length > 2) { |
| 21 | + // Debounce to avoid too many events |
| 22 | + clearTimeout((window as any).searchDebounce); |
| 23 | + (window as any).searchDebounce = setTimeout(() => { |
| 24 | + trackSearch(target.value); |
| 25 | + }, 1000); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + // Attach listener to DocSearch input |
| 30 | + const docSearchInput = document.querySelector('.DocSearch-Input'); |
| 31 | + if (docSearchInput) { |
| 32 | + docSearchInput.addEventListener('input', handleDocSearchInput); |
| 33 | + } |
| 34 | + |
| 35 | + return () => { |
| 36 | + if (docSearchInput) { |
| 37 | + docSearchInput.removeEventListener('input', handleDocSearchInput); |
| 38 | + } |
| 39 | + }; |
| 40 | + }, []); |
| 41 | + |
| 42 | + return <SearchBar {...props} />; |
| 43 | +} |
0 commit comments