|
14 | 14 | let fuse;
|
15 | 15 | let searchData;
|
16 | 16 | let isFuseInitialized = false;
|
| 17 | + let debounceTimer; |
17 | 18 |
|
18 | 19 | // Function to load a script dynamically
|
19 | 20 | function loadScript(src) {
|
|
124 | 125 |
|
125 | 126 | if(searchInput) {
|
126 | 127 | searchInput.addEventListener('input', () => {
|
127 |
| - if (!isFuseInitialized) { |
128 |
| - searchResults.innerHTML = '<div class="text-center text-gray-500 py-4">Initializing search...</div>'; |
129 |
| - return; |
130 |
| - } |
131 |
| - const query = searchInput.value.trim(); |
132 |
| - if (query.length < 2) { |
133 |
| - searchResults.innerHTML = ''; |
134 |
| - if (searchPlaceholder) searchResults.appendChild(searchPlaceholder); |
135 |
| - return; |
136 |
| - } |
| 128 | + clearTimeout(debounceTimer); |
| 129 | + debounceTimer = setTimeout(() => { |
| 130 | + if (!isFuseInitialized) { |
| 131 | + searchResults.innerHTML = '<div class="text-center text-gray-500 py-4">Initializing search...</div>'; |
| 132 | + return; |
| 133 | + } |
| 134 | + const query = searchInput.value.trim(); |
| 135 | + if (query.length < 2) { |
| 136 | + searchResults.innerHTML = ''; |
| 137 | + if (searchPlaceholder) searchResults.appendChild(searchPlaceholder); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + // --- Google Analytics Event Tracking --- |
| 142 | + if (typeof gtag === 'function') { |
| 143 | + gtag('event', 'search', { |
| 144 | + search_term: query |
| 145 | + }); |
| 146 | + console.log(`GA Event sent: search, search_term: ${query}`); |
| 147 | + } |
| 148 | + // ----------------------------------------- |
137 | 149 |
|
138 |
| - const results = fuse.search(query, { limit: 20 }); |
139 |
| - renderResults(results, query); |
| 150 | + const results = fuse.search(query, { limit: 20 }); |
| 151 | + renderResults(results, query); |
| 152 | + }, 500); // Wait 500ms after user stops typing |
140 | 153 | });
|
141 | 154 | }
|
142 | 155 |
|
|
0 commit comments