Skip to content

Commit

Permalink
fix(insights): split large view event payloads into multiple chunks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhayab committed Feb 10, 2023
1 parent 118f272 commit df58096
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"path": "packages/autocomplete-plugin-algolia-insights/dist/umd/index.production.js",
"maxSize": "2 kB"
"maxSize": "2.1 kB"
},
{
"path": "packages/autocomplete-plugin-redirect-url/dist/umd/index.production.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,62 @@ describe('createAlgoliaInsightsPlugin', () => {
});
});

test('sends as many `viewedObjectIDs` events as there are compatible sources', async () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const { inputElement } = createPlayground(createAutocomplete, {
plugins: [insightsPlugin],
defaultActiveItemId: 0,
openOnFocus: true,
getSources() {
return [
createSource({
sourceId: 'source1',
getItems: () => [
{
label: '1',
objectID: '1',
__autocomplete_indexName: 'index1',
__autocomplete_queryID: 'queryID1',
},
],
}),
createSource({
sourceId: 'source2',
getItems: () => [
{
label: '2',
objectID: '2',
__autocomplete_indexName: 'index2',
__autocomplete_queryID: 'queryID2',
},
],
}),
];
},
});

insightsClient.mockClear();

inputElement.focus();

await runAllMicroTasks();
jest.runAllTimers();

expect(insightsClient).toHaveBeenCalledTimes(2);
expect(insightsClient).toHaveBeenNthCalledWith(1, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: ['1'],
});
expect(insightsClient).toHaveBeenNthCalledWith(2, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index2',
objectIDs: ['2'],
});
});

test('sends a custom event', async () => {
const insightsClient = jest.fn();
const insightsPlugin = createAlgoliaInsightsPlugin({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
import { createSearchInsightsApi } from '../createSearchInsightsApi';

describe('createSearchInsightsApi', () => {
test.todo('tests');
test('viewedObjectIDs splits large payloads into multiple chunks', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);

insightsApi.viewedObjectIDs({
eventName: 'Items Viewed',
index: 'index1',
objectIDs: Array.from({ length: 50 }, (_, i) => `${i}`),
});

expect(insightsClient).toHaveBeenCalledTimes(3);
expect(insightsClient).toHaveBeenNthCalledWith(1, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: Array.from({ length: 20 }, (_, i) => `${i}`),
});
expect(insightsClient).toHaveBeenNthCalledWith(2, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: Array.from({ length: 20 }, (_, i) => `${i + 20}`),
});
expect(insightsClient).toHaveBeenNthCalledWith(3, 'viewedObjectIDs', {
eventName: 'Items Viewed',
index: 'index1',
objectIDs: Array.from({ length: 10 }, (_, i) => `${i + 40}`),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import {
ViewedObjectIDsParams,
} from './types';

function chunk<TItem extends { objectIDs: string[] }>(
item: TItem,
chunkSize: number = 20
): TItem[] {
const chunks: TItem[] = [];
for (let i = 0; i < item.objectIDs.length; i += chunkSize) {
chunks.push({
...item,
objectIDs: item.objectIDs.slice(i, i + chunkSize),
});
}
return chunks;
}

export function createSearchInsightsApi(searchInsights: InsightsClient) {
return {
/**
Expand Down Expand Up @@ -95,7 +109,12 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*/
viewedObjectIDs(...params: ViewedObjectIDsParams[]) {
if (params.length > 0) {
searchInsights('viewedObjectIDs', ...params);
params
.reduce(
(acc, param) => [...acc, ...chunk<ViewedObjectIDsParams>(param)],
[] as ViewedObjectIDsParams[]
)
.forEach((param) => searchInsights('viewedObjectIDs', param));
}
},
/**
Expand Down

0 comments on commit df58096

Please sign in to comment.