Merge pull request #14794 from philberryman/feat/analytics-number-of-search-results
add number of searches
This commit is contained in:
@@ -335,6 +335,7 @@ export class ElasticSearchSearchEngine implements SearchEngine {
|
||||
),
|
||||
nextPageCursor,
|
||||
previousPageCursor,
|
||||
numberOfResults: result.body.hits.total.value,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error.meta?.body?.error?.type === 'index_not_found_exception') {
|
||||
|
||||
@@ -238,6 +238,7 @@ export class LunrSearchEngine implements SearchEngine {
|
||||
}),
|
||||
},
|
||||
})),
|
||||
numberOfResults: results.length,
|
||||
nextPageCursor,
|
||||
previousPageCursor,
|
||||
};
|
||||
|
||||
@@ -205,6 +205,7 @@ export class AuthorizedSearchEngine implements SearchEngine {
|
||||
(nextPageCursor || filteredResults.length > targetResults)
|
||||
? encodePageCursor({ page: page + 1 })
|
||||
: undefined,
|
||||
numberOfResults: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,8 @@ export interface ResultSet<TDocument extends SearchDocument> {
|
||||
// (undocumented)
|
||||
nextPageCursor?: string;
|
||||
// (undocumented)
|
||||
numberOfResults?: number;
|
||||
// (undocumented)
|
||||
previousPageCursor?: string;
|
||||
// (undocumented)
|
||||
results: Result<TDocument>[];
|
||||
|
||||
@@ -87,6 +87,7 @@ export interface ResultSet<TDocument extends SearchDocument> {
|
||||
results: Result<TDocument>[];
|
||||
nextPageCursor?: string;
|
||||
previousPageCursor?: string;
|
||||
numberOfResults?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}</>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user