Skip to content

Commit

Permalink
Capture search event (#1307)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbastn authored Dec 3, 2024
1 parent 4eac3cf commit 291469d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
21 changes: 20 additions & 1 deletion content/search/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@
return $results;
};

var debounce = function debounce (func, delay) {
let timeout;
return function (...args) {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), delay);
};
};

var trackSearch = function trackSearch (query) {
window.posthog.capture('support-search', { query: $input.value });
};

if (typeof module === 'object' && typeof module.exports === 'object')
module.exports = {
parseQueryParams: parseQueryParams,
Expand All @@ -62,11 +74,18 @@
var $input = document.getElementById('input-search');
var q = parseQueryParams(window.location.search);

if (q.length)
if (q.length) {
showResults($main, $input, q);
trackSearch(q);
}

$input.oninput = function (e) {
showResults($main, $input, $input.value);
};

$input.addEventListener('input', debounce(function () {
if ($input.value.trim().length > 0)
trackSearch($input.value);
}, 900));
}
})();
13 changes: 13 additions & 0 deletions layouts/analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,17 @@
disable_session_recording: true
})
</script>
<% else %>
<script>
// Dummy PostHog client
window.posthog = {
init() {},
identify(arg) {
console.log(`PostHog identify: ${arg}`);
},
capture(event, data) {
console.log(`PostHog capture - Event: ${event}`, data);
}
}
</script>
<% end %>

0 comments on commit 291469d

Please sign in to comment.