Skip to content

Commit

Permalink
fix(recommend): provide insights query parameters to recommend queries
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Apr 8, 2024
1 parent 8498792 commit a6c730c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1631,10 +1631,16 @@ AlgoliaSearchHelper.prototype._recommend = function () {
return derivedState._buildQueries(derivedIndex);
});

var queries = Array.prototype.concat.apply(
this.recommendState._buildQueries(index),
derivedQueries
);
var queries = Array.prototype.concat
.apply(this.recommendState._buildQueries(index), derivedQueries)
.map(function (query) {
return Object.assign(query, {
queryParameters: Object.assign(query.queryParameters || {}, {
clickAnalytics: searchState.clickAnalytics,
userToken: searchState.userToken,
}),
});
});

if (queries.length === 0) {
return;
Expand Down
40 changes: 39 additions & 1 deletion packages/algoliasearch-helper/test/spec/recommend.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ describe('recommend()', () => {
indexName: 'indexName',
model: 'bought-together',
objectID: 'A0E20000000279B',
queryParameters: expect.any(Object),
},
{
indexName: 'indexName',
model: 'trending-facets',
facetName: 'brand',
queryParameters: expect.any(Object),
},
{ indexName: 'indexName', model: 'trending-facets', facetName: 'brand' },
]);
});

Expand Down Expand Up @@ -121,4 +127,36 @@ describe('recommend()', () => {

expect(client.getRecommendations).toHaveBeenCalledTimes(1);
});

test('adds `clickAnalytics` and `userToken` to the queries if available', () => {
var client = {
getRecommendations: jest.fn().mockImplementationOnce(() => {
return Promise.resolve({ results: [] });
}),
};

var helper = algoliasearchHelper(client, 'indexName', {});

// Set `clickAnalytics` and `userToken` on the helper search state
helper.overrideStateWithoutTriggeringChangeEvent({
...helper.state,
clickAnalytics: true,
userToken: 'userToken',
});

helper.addFrequentlyBoughtTogether({ $$id: 1 });
helper.recommend();

expect(client.getRecommendations).toHaveBeenCalledTimes(1);
expect(client.getRecommendations).toHaveBeenLastCalledWith([
{
indexName: 'indexName',
model: 'bought-together',
queryParameters: {
clickAnalytics: true,
userToken: 'userToken',
},
},
]);
});
});

0 comments on commit a6c730c

Please sign in to comment.