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
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-react': minor
---
The `value` of a search analytics event is now set as the total number of search results (when available)
+7
View File
@@ -0,0 +1,7 @@
---
'@backstage/plugin-search-backend': minor
'@backstage/plugin-search-backend-module-elasticsearch': minor
'@backstage/plugin-search-backend-node': minor
---
numberOfResults is now provided alongside the query result
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-common': minor
---
numberOfResults (total number of results for a given query) can now be provided by each search engine and consumed as part of the search results response
+7 -7
View File
@@ -52,13 +52,13 @@ learn how to contribute the integration yourself!
The following table summarizes events that, depending on the plugins you have
installed, may be captured.
| Action | Subject | Other Notes |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- |
| `navigate` | The URL of the page that was navigated to | |
| `click` | The text of the link that was clicked on | The `to` attribute represents the URL clicked to |
| `create` | The `name` of the software being created; if no `name` property is requested by the given Software Template, then the string `new {templateName}` is used instead. | The context holds an `entityRef`, set to the template's ref (e.g. `template:default/template-name`) |
| `search` | The search term entered in any search bar component | The context holds `searchTypes`, representing `types` constraining the search |
| `discover` | The title of the search result that was clicked on | The `value` is the result rank. A `to` attribute is also provided |
| Action | Subject | Other Notes |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `navigate` | The URL of the page that was navigated to | |
| `click` | The text of the link that was clicked on | The `to` attribute represents the URL clicked to |
| `create` | The `name` of the software being created; if no `name` property is requested by the given Software Template, then the string `new {templateName}` is used instead. | The context holds an `entityRef`, set to the template's ref (e.g. `template:default/template-name`) |
| `search` | The search term entered in any search bar component | - The context holds `searchTypes`, representing `types` constraining the search. The `value` represents the total number of search results for the query. This may not be visible if the permission framework is being used. |
| `discover` | The title of the search result that was clicked on | The `value` is the result rank. A `to` attribute is also provided |
If there is an event you'd like to see captured, please [open an
issue][add-event] describing the event you want to see and the questions it
@@ -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,
};
}
+2
View File
@@ -73,6 +73,8 @@ export interface ResultSet<TDocument extends SearchDocument> {
// (undocumented)
nextPageCursor?: string;
// (undocumented)
numberOfResults?: number;
// (undocumented)
previousPageCursor?: string;
// (undocumented)
results: Result<TDocument>[];
+1
View File
@@ -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}</>;
};