1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

feat: track interaction with search (#7526)

Now we will start tracking how users interact with search box. Whether
they open it manually or use CTRL K
This commit is contained in:
Jaanus Sellin 2024-07-03 12:27:20 +03:00 committed by GitHub
parent 72615cc6d5
commit 57c1a6edd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import { useOnClickOutside } from 'hooks/useOnClickOutside';
import { useSavedQuery } from './useSavedQuery';
import { useOnBlur } from 'hooks/useOnBlur';
import { SearchHistory } from './SearchSuggestions/SearchHistory';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
interface ISearchProps {
id?: string;
@ -110,9 +111,11 @@ export const Search = ({
debounceTime = 200,
...rest
}: ISearchProps) => {
const { trackEvent } = usePlausibleTracker();
const searchInputRef = useRef<HTMLInputElement>(null);
const searchContainerRef = useRef<HTMLInputElement>(null);
const [showSuggestions, setShowSuggestions] = useState(false);
const [usedHotkey, setUsedHotkey] = useState(false);
const hideSuggestions = () => {
setShowSuggestions(false);
onBlur?.();
@ -136,6 +139,7 @@ export const Search = ({
preventDefault: true,
},
() => {
setUsedHotkey(true);
if (document.activeElement === searchInputRef.current) {
searchInputRef.current?.blur();
} else {
@ -143,6 +147,27 @@ export const Search = ({
}
},
);
useEffect(() => {
if (!showSuggestions) {
return;
}
if (usedHotkey) {
trackEvent('search-opened', {
props: {
eventType: 'hotkey',
},
});
} else {
trackEvent('search-opened', {
props: {
eventType: 'manual',
},
});
}
setUsedHotkey(false);
}, [showSuggestions]);
useKeyboardShortcut({ key: 'Escape' }, () => {
if (searchContainerRef.current?.contains(document.activeElement)) {
searchInputRef.current?.blur();

View File

@ -64,7 +64,8 @@ export type CustomEvents =
| 'feature-lifecycle'
| 'command-bar'
| 'new-in-unleash-click'
| 'new-in-unleash-dismiss';
| 'new-in-unleash-dismiss'
| 'search-opened';
export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);