Skip to content

Commit

Permalink
feat: add searchParameters prop
Browse files Browse the repository at this point in the history
This replaces the following props: `facetFilters`, `analytics` and `clickAnalytics`.
  • Loading branch information
francoischalifour committed May 10, 2021
1 parent c5cc3d1 commit db65cf8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 43 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,17 @@ The product component to display.
The number of recommendations to retrieve.

### `facetFilters`

> list of strings
Additional [facet filters](https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/?client=javascript) to forward.

### `fallbackFilters`

> list of strings
Additional filters to use as fallback should there not be enough recommendations.

### `analytics`

> `boolean` | defaults to `false`
Whether you want [Search Analytics](https://www.algolia.com/doc/api-reference/api-parameters/analytics/?client=javascript) to be enabled.

### `clickAnalytics`
### `searchParameters`

> `boolean` | defaults to `false`
> [`SearchParameters`](https://www.algolia.com/doc/api-reference/search-api-parameters/) | defaults to `{ analytics: false, enableABTest: false }`
Whether you want [Click Analytics](https://www.algolia.com/doc/api-reference/api-parameters/clickAnalytics/?client=javascript) to be enabled.
List of [search parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/) to send.

## Development

Expand Down
18 changes: 11 additions & 7 deletions examples/demo/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function App() {
objectID={selectedProduct.objectID}
hitComponent={({ hit }) => <Hit hit={hit} insights={insights} />}
maxRecommendations={3}
clickAnalytics={true}
analytics={true}
searchParameters={{
analytics: true,
clickAnalytics: true,
}}
>
{({ recommendations, children }) => {
if (recommendations.length === 0) {
Expand All @@ -117,14 +119,16 @@ function App() {
objectID={selectedProduct.objectID}
hitComponent={({ hit }) => <Hit hit={hit} insights={insights} />}
maxRecommendations={5}
facetFilters={[
`hierarchical_categories.lvl0:${selectedProduct.hierarchical_categories.lvl0}`,
]}
searchParameters={{
analytics: true,
clickAnalytics: true,
facetFilters: [
`hierarchical_categories.lvl0:${selectedProduct.hierarchical_categories.lvl0}`,
],
}}
fallbackFilters={[
`hierarchical_categories.lvl2:${selectedProduct.hierarchical_categories.lvl2}`,
]}
clickAnalytics={true}
analytics={true}
>
{({ recommendations, children }) => {
if (recommendations.length === 0) {
Expand Down
6 changes: 1 addition & 5 deletions packages/react-recommendations/src/Recommendations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ Recommendations.propTypes = {
objectID: PropTypes.string.isRequired,
hitComponent: PropTypes.elementType.isRequired,

analytics: PropTypes.bool,
clickAnalytics: PropTypes.bool,
facetFilters: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)])
),
fallbackFilters: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)])
),
maxRecommendations: PropTypes.number,
searchParameters: PropTypes.object,
threshold: PropTypes.number,

children: PropTypes.func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe('Recommendations', () => {
analyticsTags: ['alg-recommend_related-products'],
clickAnalytics: false,
enableABTest: false,
facetFilters: [],
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
Expand Down Expand Up @@ -122,7 +121,6 @@ describe('Recommendations', () => {
analyticsTags: ['alg-recommend_bought-together'],
clickAnalytics: false,
enableABTest: false,
facetFilters: [],
filters: 'NOT objectID:objectID',
hitsPerPage: 0,
optionalFilters: [],
Expand Down
26 changes: 12 additions & 14 deletions packages/react-recommendations/src/useRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ export type UseRecommendationsProps = {
objectID: string;
searchClient: SearchClient;

analytics?: boolean;
clickAnalytics?: boolean;
facetFilters?: SearchOptions['facetFilters'];
fallbackFilters?: SearchOptions['optionalFilters'];
maxRecommendations?: number;
searchParameters?: SearchOptions;
threshold?: number;
};

Expand All @@ -96,11 +94,18 @@ function getDefaultedProps(
props: UseRecommendationsProps
): InternalUseRecommendationsProps {
return {
analytics: false,
clickAnalytics: false,
facetFilters: [],
fallbackFilters: [],
maxRecommendations: 0,
searchParameters: {
analytics: false,
analyticsTags: [`alg-recommend_${props.model}`],
clickAnalytics: false,
enableABTest: false,
filters: `NOT objectID:${props.objectID}`,
ruleContexts: [`alg-recommend_${props.model}_${props.objectID}`],
typoTolerance: false,
...props.searchParameters,
},
threshold: 0,
...props,
};
Expand All @@ -122,14 +127,6 @@ export function useRecommendations<TObject extends ProductRecord>(
props.searchClient
.initIndex(props.indexName)
.search<TObject>('', {
analytics: props.analytics,
analyticsTags: [`alg-recommend_${props.model}`],
clickAnalytics: props.clickAnalytics,
enableABTest: false,
facetFilters: props.facetFilters,
filters: `NOT objectID:${props.objectID}`,
ruleContexts: [`alg-recommend_${props.model}_${props.objectID}`],
typoTolerance: false,
hitsPerPage: getHitsPerPage({
fallbackFilters: props.fallbackFilters,
maxRecommendations: props.maxRecommendations,
Expand All @@ -140,6 +137,7 @@ export function useRecommendations<TObject extends ProductRecord>(
recommendations,
threshold: props.threshold,
}),
...props.searchParameters,
})
.then((result) => {
const hits = result.hits.map((hit) => {
Expand Down

0 comments on commit db65cf8

Please sign in to comment.