Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(insights): split large view event payloads into multiple chunks #1087

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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