Skip to content

Commit 20d20a2

Browse files
authored
feat: allow extra arguments in sendEvent signature (#1210)
1 parent fa9350b commit 20d20a2

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

packages/autocomplete-plugin-algolia-insights/src/__tests__/createSearchInsightsApi.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,42 @@ describe('createSearchInsightsApi', () => {
252252
});
253253
});
254254

255+
test('allows arbitrary additional data to be sent', () => {
256+
const insightsClient = jest.fn();
257+
const insightsApi = createSearchInsightsApi(insightsClient);
258+
259+
insightsApi.convertedObjectIDsAfterSearch({
260+
// Regular properties
261+
eventName: 'Items Added to cart',
262+
index: 'index1',
263+
items: getAlgoliaItems(1),
264+
queryID: 'queryID',
265+
// Extra additional properties
266+
eventSubtype: 'purchase',
267+
objectData: [
268+
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
269+
],
270+
value: 100,
271+
currency: 'USD',
272+
});
273+
274+
expect(insightsClient).toHaveBeenCalledWith(
275+
'convertedObjectIDsAfterSearch',
276+
{
277+
eventName: 'Items Added to cart',
278+
index: 'index1',
279+
objectIDs: ['0'],
280+
queryID: 'queryID',
281+
eventSubtype: 'purchase',
282+
objectData: [
283+
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
284+
],
285+
value: 100,
286+
currency: 'USD',
287+
}
288+
);
289+
});
290+
255291
test('viewedObjectIDs() splits large payloads into multiple chunks', () => {
256292
const insightsClient = jest.fn();
257293
const insightsApi = createSearchInsightsApi(insightsClient);

packages/autocomplete-plugin-algolia-insights/src/createSearchInsightsApi.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ConvertedObjectIDsParams,
1212
InsightsClient,
1313
InsightsClientMethod,
14+
WithArbitraryParams,
1415
InsightsParamsWithItems,
1516
ViewedFiltersParams,
1617
ViewedObjectIDsParams,
@@ -83,13 +84,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
8384
*/
8485
clickedObjectIDsAfterSearch(
8586
...params: Array<
86-
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
87+
WithArbitraryParams<
88+
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
89+
>
8790
>
8891
) {
8992
if (params.length > 0) {
9093
sendToInsights(
9194
'clickedObjectIDsAfterSearch',
92-
mapToInsightsParamsApi(params),
95+
mapToInsightsParamsApi<
96+
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
97+
>(params),
9398
params[0].items
9499
);
95100
}
@@ -100,12 +105,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
100105
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
101106
*/
102107
clickedObjectIDs(
103-
...params: Array<InsightsParamsWithItems<ClickedObjectIDsParams>>
108+
...params: Array<
109+
WithArbitraryParams<InsightsParamsWithItems<ClickedObjectIDsParams>>
110+
>
104111
) {
105112
if (params.length > 0) {
106113
sendToInsights(
107114
'clickedObjectIDs',
108-
mapToInsightsParamsApi(params),
115+
mapToInsightsParamsApi<
116+
InsightsParamsWithItems<ClickedObjectIDsParams>
117+
>(params),
109118
params[0].items
110119
);
111120
}
@@ -115,7 +124,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
115124
*
116125
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
117126
*/
118-
clickedFilters(...params: ClickedFiltersParams[]) {
127+
clickedFilters(
128+
...params: Array<WithArbitraryParams<ClickedFiltersParams>>
129+
) {
119130
if (params.length > 0) {
120131
searchInsights('clickedFilters', ...params);
121132
}
@@ -127,13 +138,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
127138
*/
128139
convertedObjectIDsAfterSearch(
129140
...params: Array<
130-
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
141+
WithArbitraryParams<
142+
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
143+
>
131144
>
132145
) {
133146
if (params.length > 0) {
134147
sendToInsights(
135148
'convertedObjectIDsAfterSearch',
136-
mapToInsightsParamsApi(params),
149+
mapToInsightsParamsApi<
150+
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
151+
>(params),
137152
params[0].items
138153
);
139154
}
@@ -144,12 +159,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
144159
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
145160
*/
146161
convertedObjectIDs(
147-
...params: Array<InsightsParamsWithItems<ConvertedObjectIDsParams>>
162+
...params: Array<
163+
WithArbitraryParams<InsightsParamsWithItems<ConvertedObjectIDsParams>>
164+
>
148165
) {
149166
if (params.length > 0) {
150167
sendToInsights(
151168
'convertedObjectIDs',
152-
mapToInsightsParamsApi(params),
169+
mapToInsightsParamsApi<
170+
InsightsParamsWithItems<ConvertedObjectIDsParams>
171+
>(params),
153172
params[0].items
154173
);
155174
}
@@ -159,7 +178,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
159178
*
160179
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
161180
*/
162-
convertedFilters(...params: ConvertedFiltersParams[]) {
181+
convertedFilters(
182+
...params: Array<WithArbitraryParams<ConvertedFiltersParams>>
183+
) {
163184
if (params.length > 0) {
164185
searchInsights('convertedFilters', ...params);
165186
}
@@ -170,7 +191,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
170191
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
171192
*/
172193
viewedObjectIDs(
173-
...params: Array<InsightsParamsWithItems<ViewedObjectIDsParams>>
194+
...params: Array<
195+
WithArbitraryParams<InsightsParamsWithItems<ViewedObjectIDsParams>>
196+
>
174197
) {
175198
if (params.length > 0) {
176199
params
@@ -202,7 +225,7 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
202225
*
203226
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
204227
*/
205-
viewedFilters(...params: ViewedFiltersParams[]) {
228+
viewedFilters(...params: Array<WithArbitraryParams<ViewedFiltersParams>>) {
206229
if (params.length > 0) {
207230
searchInsights('viewedFilters', ...params);
208231
}

packages/autocomplete-plugin-algolia-insights/src/types/AutocompleteInsightsApi.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export type AutocompleteInsightsApi = ReturnType<
66
typeof createSearchInsightsApi
77
>;
88

9+
export type WithArbitraryParams<TParams extends Record<string, unknown>> =
10+
Record<string, unknown> & TParams;
11+
912
export type InsightsParamsWithItems<TParams extends { objectIDs: string[] }> =
1013
Omit<TParams, 'objectIDs'> & {
1114
items: AlgoliaInsightsHit[];

0 commit comments

Comments
 (0)