Merge pull request #14794 from philberryman/feat/analytics-number-of-search-results

add number of searches
This commit is contained in:
Eric Peterson
2022-11-30 12:09:14 +01:00
committed by GitHub
10 changed files with 49 additions and 12 deletions
@@ -17,20 +17,34 @@
import React, { useEffect } from 'react';
import { useAnalytics } from '@backstage/core-plugin-api';
import { useSearch } from '../../context';
import usePrevious from 'react-use/lib/usePrevious';
/**
* Capture search event on term change.
*/
export const TrackSearch = ({ children }: { children: React.ReactChild }) => {
const useHasChanged = (value: any) => {
const previousVal = usePrevious(value);
return previousVal !== value;
};
const analytics = useAnalytics();
const { term } = useSearch();
const { term, result } = useSearch();
const numberOfResults = result.value?.numberOfResults ?? undefined;
// Stops the analtyics event from firing before the new search engine response is returned
const hasStartedLoading = useHasChanged(result.loading);
const hasFinishedLoading = hasStartedLoading && !result.loading;
useEffect(() => {
if (term) {
// Capture analytics search event with search term provided as value
analytics.captureEvent('search', term);
if (term && hasFinishedLoading) {
// Capture analytics search event with search term and numberOfResults (provided as value)
analytics.captureEvent('search', term, {
value: numberOfResults,
});
}
}, [analytics, term]);
}, [analytics, term, numberOfResults, hasFinishedLoading]);
return <>{children}</>;
};