Skip to content

Commit

Permalink
feat: allow extra arguments in sendEvent signature (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Oct 20, 2023
1 parent fa9350b commit 20d20a2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
Expand Up @@ -252,6 +252,42 @@ describe('createSearchInsightsApi', () => {
});
});

test('allows arbitrary additional data to be sent', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);

insightsApi.convertedObjectIDsAfterSearch({
// Regular properties
eventName: 'Items Added to cart',
index: 'index1',
items: getAlgoliaItems(1),
queryID: 'queryID',
// Extra additional properties
eventSubtype: 'purchase',
objectData: [
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
],
value: 100,
currency: 'USD',
});

expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDsAfterSearch',
{
eventName: 'Items Added to cart',
index: 'index1',
objectIDs: ['0'],
queryID: 'queryID',
eventSubtype: 'purchase',
objectData: [
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
],
value: 100,
currency: 'USD',
}
);
});

test('viewedObjectIDs() splits large payloads into multiple chunks', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);
Expand Down
Expand Up @@ -11,6 +11,7 @@ import {
ConvertedObjectIDsParams,
InsightsClient,
InsightsClientMethod,
WithArbitraryParams,
InsightsParamsWithItems,
ViewedFiltersParams,
ViewedObjectIDsParams,
Expand Down Expand Up @@ -83,13 +84,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*/
clickedObjectIDsAfterSearch(
...params: Array<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
WithArbitraryParams<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
>
>
) {
if (params.length > 0) {
sendToInsights(
'clickedObjectIDsAfterSearch',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
>(params),
params[0].items
);
}
Expand All @@ -100,12 +105,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
*/
clickedObjectIDs(
...params: Array<InsightsParamsWithItems<ClickedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ClickedObjectIDsParams>>
>
) {
if (params.length > 0) {
sendToInsights(
'clickedObjectIDs',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ClickedObjectIDsParams>
>(params),
params[0].items
);
}
Expand All @@ -115,7 +124,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
*/
clickedFilters(...params: ClickedFiltersParams[]) {
clickedFilters(
...params: Array<WithArbitraryParams<ClickedFiltersParams>>
) {
if (params.length > 0) {
searchInsights('clickedFilters', ...params);
}
Expand All @@ -127,13 +138,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*/
convertedObjectIDsAfterSearch(
...params: Array<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
WithArbitraryParams<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
>
>
) {
if (params.length > 0) {
sendToInsights(
'convertedObjectIDsAfterSearch',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
>(params),
params[0].items
);
}
Expand All @@ -144,12 +159,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
*/
convertedObjectIDs(
...params: Array<InsightsParamsWithItems<ConvertedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ConvertedObjectIDsParams>>
>
) {
if (params.length > 0) {
sendToInsights(
'convertedObjectIDs',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ConvertedObjectIDsParams>
>(params),
params[0].items
);
}
Expand All @@ -159,7 +178,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
*/
convertedFilters(...params: ConvertedFiltersParams[]) {
convertedFilters(
...params: Array<WithArbitraryParams<ConvertedFiltersParams>>
) {
if (params.length > 0) {
searchInsights('convertedFilters', ...params);
}
Expand All @@ -170,7 +191,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
*/
viewedObjectIDs(
...params: Array<InsightsParamsWithItems<ViewedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ViewedObjectIDsParams>>
>
) {
if (params.length > 0) {
params
Expand Down Expand Up @@ -202,7 +225,7 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
*/
viewedFilters(...params: ViewedFiltersParams[]) {
viewedFilters(...params: Array<WithArbitraryParams<ViewedFiltersParams>>) {
if (params.length > 0) {
searchInsights('viewedFilters', ...params);
}
Expand Down
Expand Up @@ -6,6 +6,9 @@ export type AutocompleteInsightsApi = ReturnType<
typeof createSearchInsightsApi
>;

export type WithArbitraryParams<TParams extends Record<string, unknown>> =
Record<string, unknown> & TParams;

export type InsightsParamsWithItems<TParams extends { objectIDs: string[] }> =
Omit<TParams, 'objectIDs'> & {
items: AlgoliaInsightsHit[];
Expand Down

0 comments on commit 20d20a2

Please sign in to comment.