docs: added search track event (#2051)
* added search track event * handle all search inputs events
This commit is contained in:
@@ -1,7 +1,39 @@
|
|||||||
import React from 'react';
|
import React, { useEffect } from 'react';
|
||||||
|
|
||||||
import SearchBar from '@theme-original/SearchBar';
|
import SearchBar from '@theme-original/SearchBar';
|
||||||
|
import useIsBrowser from '@docusaurus/useIsBrowser';
|
||||||
|
import {useLocation} from '@docusaurus/router';
|
||||||
|
|
||||||
export default function SearchBarWrapper(props) {
|
export default function SearchBarWrapper(props) {
|
||||||
|
|
||||||
|
const isBrowser = useIsBrowser();
|
||||||
|
const location = useLocation();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isBrowser) {
|
||||||
|
|
||||||
|
function trackSearch(e) {
|
||||||
|
if (!e.target.classList.contains('DocSearch-Input') && !(e.target.tagName.toLowerCase() === "input" && e.target.getAttribute('type') === 'search')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = e.target.value;
|
||||||
|
if (query.length >= 3) {
|
||||||
|
//send event to segment
|
||||||
|
window.analytics.track('search', {
|
||||||
|
query
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.body.addEventListener('keyup', trackSearch);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.body.removeEventListener('keyup', trackSearch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [isBrowser, location.pathname]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<SearchBar {...props} />
|
<SearchBar {...props} />
|
||||||
|
|||||||
Reference in New Issue
Block a user