Skip to content

Commit cf96cc8

Browse files
authored
Fix/change type value of search tracking and add small delay (#169)
* feat: change the type value for search traking * fix: increase delay during search
1 parent 3ab1ce8 commit cf96cc8

File tree

1 file changed

+18
-86
lines changed

1 file changed

+18
-86
lines changed

docs/js/search-tracking.js

Lines changed: 18 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@
7676
async function sendAnalyticsEvent(eventType, payload) {
7777
// Check if analytics is disabled for internal users
7878
if (isAnalyticsDisabled()) {
79-
console.log(
80-
"Analytics disabled for internal user, skipping:",
81-
eventType,
82-
payload
83-
);
8479
return;
8580
}
8681

87-
if (!ANALYTICS_CONFIG.ENABLED || !ANALYTICS_CONFIG.API_ENDPOINT) {
88-
console.log("Search Analytics (Demo Mode):", eventType, payload);
82+
if (!ANALYTICS_CONFIG.ENABLED || !ANALYTICS_CONFIG.API_ENDPOINT) {
8983
return;
9084
}
9185

@@ -107,9 +101,7 @@
107101
// pageType, // docs / blog / marketing (uncomment if needed)
108102
...payload,
109103
}),
110-
});
111-
112-
console.log("Search analytics event sent:", eventType, payload);
104+
});
113105
} catch (error) {
114106
console.warn("Analytics API error:", error);
115107
}
@@ -126,18 +118,10 @@
126118
totalResults = resultCount;
127119

128120
// Send search event exactly like Vue.js reference
129-
sendAnalyticsEvent("search", {
121+
sendAnalyticsEvent("doc-search", {
130122
search_query: _q,
131123
result_count: resultCount,
132-
});
133-
134-
// Also send analytics for zero results (like Vue.js reference)
135-
if (resultCount === 0) {
136-
sendAnalyticsEvent("search", {
137-
search_query: _q,
138-
result_count: resultCount,
139-
});
140-
}
124+
});
141125
}
142126

143127
/**
@@ -146,16 +130,8 @@
146130
function trackResultClick(resultUrl, resultTitle, resultRank) {
147131
const _q = lastSearchQuery.trim();
148132

149-
console.log("trackResultClick called:", {
150-
resultUrl,
151-
resultTitle,
152-
resultRank,
153-
lastSearchQuery: _q,
154-
totalResults,
155-
});
156-
157133
// Send result_click event exactly like Vue.js reference
158-
sendAnalyticsEvent("result_click", {
134+
sendAnalyticsEvent("doc-search-click", {
159135
result_url: resultUrl,
160136
result_rank: resultRank,
161137
result_title: resultTitle,
@@ -168,11 +144,6 @@
168144
* Track page feedback (thumbs up/down) - new functionality
169145
*/
170146
function trackPageFeedback(feedbackValue, pageUrl, pageTitle) {
171-
console.log("trackPageFeedback called:", {
172-
feedbackValue,
173-
pageUrl,
174-
pageTitle,
175-
});
176147

177148
// Send feedback event to same analytics endpoint
178149
sendAnalyticsEvent("reaction", {
@@ -233,15 +204,6 @@
233204
);
234205
const resultRank = Array.from(allResults).indexOf(resultItem) + 1;
235206

236-
console.log("Extracted result info:", {
237-
url: resultUrl,
238-
title: resultTitle,
239-
rank: resultRank,
240-
resultItem: resultItem,
241-
linkElement: linkElement,
242-
titleElement: titleElement,
243-
});
244-
245207
return {
246208
url: resultUrl,
247209
title: resultTitle,
@@ -256,8 +218,6 @@
256218
let searchTimeout;
257219
let lastTrackedQuery = "";
258220

259-
console.log("Initializing MkDocs search tracking...");
260-
261221
// Update context when search becomes active (like Vue.js modal opening)
262222
const updateContextOnSearchActivation = () => {
263223
try {
@@ -295,7 +255,7 @@
295255
const resultCount = getSearchResultCount();
296256
trackSearchQuery(query, resultCount);
297257
lastTrackedQuery = query;
298-
}, 500); // Matching Vue.js 500ms debounce
258+
}, 600); // Matching Vue.js 600ms debounce
299259
}
300260

301261
// Clear tracking when input is cleared
@@ -308,8 +268,6 @@
308268

309269
// Track search result clicks with enhanced logic
310270
document.addEventListener("click", function (e) {
311-
console.log("Click detected on:", e.target);
312-
313271
// Multiple ways to detect search result clicks
314272
const isSearchResult =
315273
e.target.closest(".md-search-result__item") ||
@@ -323,18 +281,10 @@
323281
e.target.closest(".md-search-result") ||
324282
(e.target.href && document.querySelector(".md-search--active")));
325283

326-
if (!isSearchResult && !isSearchLink) {
327-
console.log("Not a search result click, ignoring");
284+
if (!isSearchResult && !isSearchLink) {
328285
return;
329286
}
330287

331-
console.log("Search result clicked!", {
332-
isSearchResult: !!isSearchResult,
333-
isSearchLink: !!isSearchLink,
334-
targetElement: e.target,
335-
closestResult: e.target.closest(".md-search-result__item"),
336-
});
337-
338288
// Check if search is active (more lenient check)
339289
const searchContainer = document.querySelector(".md-search");
340290
const searchInput = document.querySelector(".md-search__input");
@@ -344,33 +294,27 @@
344294
!searchContainer ||
345295
(!searchContainer.classList.contains("md-search--active") &&
346296
!hasSearchValue)
347-
) {
348-
console.log("Search not active and no search value, ignoring click");
297+
) {
349298
return;
350299
}
351300

352301
// Update context right before tracking click (ensure fresh data)
353302
updateContextOnSearchActivation();
354303

355-
const resultInfo = extractResultInfo(e.target);
356-
console.log("Result info extracted:", resultInfo);
304+
const resultInfo = extractResultInfo(e.target);
357305

358-
if (resultInfo && resultInfo.url) {
359-
console.log("Calling trackResultClick with:", resultInfo);
306+
if (resultInfo && resultInfo.url) {
360307
trackResultClick(resultInfo.url, resultInfo.title, resultInfo.rank);
361308
} else {
362309
console.warn("Could not extract result info from clicked element");
363310
}
364-
});
365-
366-
console.log("MkDocs search tracking initialized");
311+
});
367312
}
368313

369314
/**
370315
* Initialize feedback tracking - new functionality for page feedback
371316
*/
372-
function initializeFeedbackTracking() {
373-
console.log("Initializing MkDocs feedback tracking...");
317+
function initializeFeedbackTracking() {
374318

375319
// Track feedback button clicks
376320
document.addEventListener("click", function (e) {
@@ -379,9 +323,7 @@
379323

380324
if (!feedbackButton) {
381325
return;
382-
}
383-
384-
console.log("Feedback button clicked:", feedbackButton);
326+
}
385327

386328
// Get feedback value from data-md-value attribute
387329
const feedbackValue = feedbackButton.getAttribute("data-md-value");
@@ -398,17 +340,11 @@
398340
// Convert feedback value to number
399341
const feedbackValueNum = parseInt(feedbackValue, 10);
400342

401-
console.log("Tracking feedback:", {
402-
value: feedbackValueNum,
403-
url: pageUrl,
404-
title: pageTitle,
405-
});
406343

407344
// Track the feedback
408345
trackPageFeedback(feedbackValueNum, pageUrl, pageTitle);
409346
});
410-
411-
console.log("MkDocs feedback tracking initialized");
347+
412348
}
413349

414350
/**
@@ -459,13 +395,11 @@
459395
testClickDetection: function () {
460396
const searchResults = document.querySelectorAll(
461397
".md-search-result__item, .md-search-result, [data-md-component='search-result']"
462-
);
463-
console.log("Found search results:", searchResults);
398+
);
464399
return searchResults;
465400
},
466401
testFeedbackDetection: function () {
467-
const feedbackButtons = document.querySelectorAll(".md-feedback__icon");
468-
console.log("Found feedback buttons:", feedbackButtons);
402+
const feedbackButtons = document.querySelectorAll(".md-feedback__icon");
469403
return feedbackButtons;
470404
},
471405
getCurrentSearchState: function () {
@@ -489,8 +423,7 @@
489423
);
490424
if (results[index]) {
491425
const link = results[index].querySelector("a");
492-
if (link) {
493-
console.log("Simulating click on:", link);
426+
if (link) {
494427
link.click();
495428
}
496429
}
@@ -499,8 +432,7 @@
499432
const feedbackButton = document.querySelector(
500433
`.md-feedback__icon[data-md-value="${value}"]`
501434
);
502-
if (feedbackButton) {
503-
console.log("Simulating feedback click:", feedbackButton);
435+
if (feedbackButton) {
504436
feedbackButton.click();
505437
}
506438
},

0 commit comments

Comments
 (0)