1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-06 01:15:28 +02: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 { useSavedQuery } from './useSavedQuery';
import { useOnBlur } from 'hooks/useOnBlur'; import { useOnBlur } from 'hooks/useOnBlur';
import { SearchHistory } from './SearchSuggestions/SearchHistory'; import { SearchHistory } from './SearchSuggestions/SearchHistory';
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
interface ISearchProps { interface ISearchProps {
id?: string; id?: string;
@ -110,9 +111,11 @@ export const Search = ({
debounceTime = 200, debounceTime = 200,
...rest ...rest
}: ISearchProps) => { }: ISearchProps) => {
const { trackEvent } = usePlausibleTracker();
const searchInputRef = useRef<HTMLInputElement>(null); const searchInputRef = useRef<HTMLInputElement>(null);
const searchContainerRef = useRef<HTMLInputElement>(null); const searchContainerRef = useRef<HTMLInputElement>(null);
const [showSuggestions, setShowSuggestions] = useState(false); const [showSuggestions, setShowSuggestions] = useState(false);
const [usedHotkey, setUsedHotkey] = useState(false);
const hideSuggestions = () => { const hideSuggestions = () => {
setShowSuggestions(false); setShowSuggestions(false);
onBlur?.(); onBlur?.();
@ -136,6 +139,7 @@ export const Search = ({
preventDefault: true, preventDefault: true,
}, },
() => { () => {
setUsedHotkey(true);
if (document.activeElement === searchInputRef.current) { if (document.activeElement === searchInputRef.current) {
searchInputRef.current?.blur(); searchInputRef.current?.blur();
} else { } 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' }, () => { useKeyboardShortcut({ key: 'Escape' }, () => {
if (searchContainerRef.current?.contains(document.activeElement)) { if (searchContainerRef.current?.contains(document.activeElement)) {
searchInputRef.current?.blur(); searchInputRef.current?.blur();

View File

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