Skip to content

Commit 17ef71c

Browse files
author
Eunjae Lee
authoredMar 5, 2021
feat(insights): add hits and attributes to InsightsEvent (#4667)
* feat(insights): add hits and attributes to InsightsEvent * replace Array.from with spread * delete __escaped from hits * remove __escaped from hits
1 parent ceb48c2 commit 17ef71c

22 files changed

+338
-9
lines changed
 

‎src/connectors/autocomplete/__tests__/connectAutocomplete-test.ts

+38
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@ search.addWidgets([
770770
expect(sendEventToInsights).toHaveBeenCalledTimes(2);
771771
expect(sendEventToInsights.mock.calls[0][0]).toEqual({
772772
eventType: 'view',
773+
hits: [
774+
{
775+
__position: 0,
776+
__queryID: 'test-query-id',
777+
name: 'Hit 1-1',
778+
objectID: '1-1',
779+
},
780+
],
773781
insightsMethod: 'viewedObjectIDs',
774782
payload: {
775783
eventName: 'Hits Viewed',
@@ -780,6 +788,20 @@ search.addWidgets([
780788
});
781789
expect(sendEventToInsights.mock.calls[1][0]).toEqual({
782790
eventType: 'view',
791+
hits: [
792+
{
793+
__position: 0,
794+
__queryID: 'test-query-id',
795+
name: 'Hit 2-1',
796+
objectID: '2-1',
797+
},
798+
{
799+
__position: 1,
800+
__queryID: 'test-query-id',
801+
name: 'Hit 2-2',
802+
objectID: '2-2',
803+
},
804+
],
783805
insightsMethod: 'viewedObjectIDs',
784806
payload: {
785807
eventName: 'Hits Viewed',
@@ -803,6 +825,14 @@ search.addWidgets([
803825
expect(sendEventToInsights).toHaveBeenCalledTimes(3);
804826
expect(sendEventToInsights.mock.calls[2][0]).toEqual({
805827
eventType: 'click',
828+
hits: [
829+
{
830+
__position: 0,
831+
__queryID: 'test-query-id',
832+
name: 'Hit 2-1',
833+
objectID: '2-1',
834+
},
835+
],
806836
insightsMethod: 'clickedObjectIDsAfterSearch',
807837
payload: {
808838
eventName: 'Product Added',
@@ -828,6 +858,14 @@ search.addWidgets([
828858
expect(sendEventToInsights).toHaveBeenCalledTimes(3);
829859
expect(sendEventToInsights.mock.calls[2][0]).toEqual({
830860
eventType: 'conversion',
861+
hits: [
862+
{
863+
__position: 0,
864+
__queryID: 'test-query-id',
865+
name: 'Hit 1-1',
866+
objectID: '1-1',
867+
},
868+
],
831869
insightsMethod: 'convertedObjectIDsAfterSearch',
832870
payload: {
833871
eventName: 'Product Ordered',

‎src/connectors/geo-search/__tests__/connectGeoSearch-test.js

+54-1
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,35 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/
15451545
);
15461546
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
15471547
eventType: 'view',
1548+
hits: [
1549+
{
1550+
__position: 0,
1551+
__queryID: 'test-query-id',
1552+
_geoloc: {
1553+
lat: 10,
1554+
lng: 12,
1555+
},
1556+
objectID: 123,
1557+
},
1558+
{
1559+
__position: 1,
1560+
__queryID: 'test-query-id',
1561+
_geoloc: {
1562+
lat: 12,
1563+
lng: 14,
1564+
},
1565+
objectID: 456,
1566+
},
1567+
{
1568+
__position: 2,
1569+
__queryID: 'test-query-id',
1570+
_geoloc: {
1571+
lat: 14,
1572+
lng: 16,
1573+
},
1574+
objectID: 789,
1575+
},
1576+
],
15481577
insightsMethod: 'viewedObjectIDs',
15491578
payload: {
15501579
eventName: 'Hits Viewed',
@@ -1568,6 +1597,17 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/
15681597
);
15691598
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
15701599
eventType: 'click',
1600+
hits: [
1601+
{
1602+
__position: 0,
1603+
__queryID: 'test-query-id',
1604+
_geoloc: {
1605+
lat: 10,
1606+
lng: 12,
1607+
},
1608+
objectID: 123,
1609+
},
1610+
],
15711611
insightsMethod: 'clickedObjectIDsAfterSearch',
15721612
payload: {
15731613
eventName: 'Location Added',
@@ -1591,8 +1631,21 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/geo-search/
15911631
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(
15921632
2
15931633
);
1594-
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1634+
expect(
1635+
instantSearchInstance.sendEventToInsights
1636+
).toHaveBeenLastCalledWith({
15951637
eventType: 'conversion',
1638+
hits: [
1639+
{
1640+
__position: 0,
1641+
__queryID: 'test-query-id',
1642+
_geoloc: {
1643+
lat: 10,
1644+
lng: 12,
1645+
},
1646+
objectID: 123,
1647+
},
1648+
],
15961649
insightsMethod: 'convertedObjectIDsAfterSearch',
15971650
payload: {
15981651
eventName: 'Location Saved',

‎src/connectors/hierarchical-menu/__tests__/connectHierarchicalMenu-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hierarchica
13971397
1
13981398
);
13991399
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1400+
attribute: 'category',
14001401
eventType: 'click',
14011402
insightsMethod: 'clickedFilters',
14021403
payload: {

‎src/connectors/hits/__tests__/connectHits-test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,20 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
729729
instantSearchInstance.sendEventToInsights
730730
).toHaveBeenCalledWith({
731731
eventType: 'view',
732+
hits: [
733+
{
734+
__position: 0,
735+
__queryID: 'test-query-id',
736+
fake: 'data',
737+
objectID: '1',
738+
},
739+
{
740+
__position: 1,
741+
__queryID: 'test-query-id',
742+
objectID: '2',
743+
sample: 'infos',
744+
},
745+
],
732746
insightsMethod: 'viewedObjectIDs',
733747
payload: {
734748
eventName: 'Hits Viewed',
@@ -760,6 +774,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
760774
instantSearchInstance.sendEventToInsights
761775
).toHaveBeenCalledWith({
762776
eventType: 'click',
777+
hits: [
778+
{
779+
__position: 0,
780+
__queryID: 'test-query-id',
781+
fake: 'data',
782+
objectID: '1',
783+
},
784+
],
763785
insightsMethod: 'clickedObjectIDsAfterSearch',
764786
payload: {
765787
eventName: 'Product Added',
@@ -793,6 +815,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
793815
instantSearchInstance.sendEventToInsights
794816
).toHaveBeenCalledWith({
795817
eventType: 'conversion',
818+
hits: [
819+
{
820+
__position: 1,
821+
__queryID: 'test-query-id',
822+
objectID: '2',
823+
sample: 'infos',
824+
},
825+
],
796826
insightsMethod: 'convertedObjectIDsAfterSearch',
797827
payload: {
798828
eventName: 'Product Ordered',
@@ -817,6 +847,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
817847
JSON.parse(atob(payload.substr('data-insights-event='.length)))
818848
).toEqual({
819849
eventType: 'click',
850+
hits: [
851+
{
852+
__position: 0,
853+
__queryID: 'test-query-id',
854+
fake: 'data',
855+
objectID: '1',
856+
},
857+
],
820858
insightsMethod: 'clickedObjectIDsAfterSearch',
821859
payload: {
822860
eventName: 'Product Added',
@@ -840,6 +878,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
840878
JSON.parse(atob(payload.substr('data-insights-event='.length)))
841879
).toEqual({
842880
eventType: 'conversion',
881+
hits: [
882+
{
883+
__position: 1,
884+
__queryID: 'test-query-id',
885+
objectID: '2',
886+
sample: 'infos',
887+
},
888+
],
843889
insightsMethod: 'convertedObjectIDsAfterSearch',
844890
payload: {
845891
eventName: 'Product Ordered',

‎src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,20 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
13211321
instantSearchInstance.sendEventToInsights
13221322
).toHaveBeenCalledWith({
13231323
eventType: 'view',
1324+
hits: [
1325+
{
1326+
__position: 0,
1327+
__queryID: 'test-query-id',
1328+
fake: 'data',
1329+
objectID: '1',
1330+
},
1331+
{
1332+
__position: 1,
1333+
__queryID: 'test-query-id',
1334+
objectID: '2',
1335+
sample: 'infos',
1336+
},
1337+
],
13241338
insightsMethod: 'viewedObjectIDs',
13251339
payload: {
13261340
eventName: 'Hits Viewed',
@@ -1352,6 +1366,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
13521366
instantSearchInstance.sendEventToInsights
13531367
).toHaveBeenCalledWith({
13541368
eventType: 'click',
1369+
hits: [
1370+
{
1371+
__position: 0,
1372+
__queryID: 'test-query-id',
1373+
fake: 'data',
1374+
objectID: '1',
1375+
},
1376+
],
13551377
insightsMethod: 'clickedObjectIDsAfterSearch',
13561378
payload: {
13571379
eventName: 'Product Added',
@@ -1385,6 +1407,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
13851407
instantSearchInstance.sendEventToInsights
13861408
).toHaveBeenCalledWith({
13871409
eventType: 'conversion',
1410+
hits: [
1411+
{
1412+
__position: 1,
1413+
__queryID: 'test-query-id',
1414+
objectID: '2',
1415+
sample: 'infos',
1416+
},
1417+
],
13881418
insightsMethod: 'convertedObjectIDsAfterSearch',
13891419
payload: {
13901420
eventName: 'Product Ordered',
@@ -1409,6 +1439,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14091439
JSON.parse(atob(payload.substr('data-insights-event='.length)))
14101440
).toEqual({
14111441
eventType: 'click',
1442+
hits: [
1443+
{
1444+
__position: 0,
1445+
__queryID: 'test-query-id',
1446+
fake: 'data',
1447+
objectID: '1',
1448+
},
1449+
],
14121450
insightsMethod: 'clickedObjectIDsAfterSearch',
14131451
payload: {
14141452
eventName: 'Product Added',
@@ -1432,6 +1470,14 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
14321470
JSON.parse(atob(payload.substr('data-insights-event='.length)))
14331471
).toEqual({
14341472
eventType: 'conversion',
1473+
hits: [
1474+
{
1475+
__position: 1,
1476+
__queryID: 'test-query-id',
1477+
objectID: '2',
1478+
sample: 'infos',
1479+
},
1480+
],
14351481
insightsMethod: 'convertedObjectIDsAfterSearch',
14361482
payload: {
14371483
eventName: 'Product Ordered',

‎src/connectors/menu/__tests__/connectMenu-test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/menu/js/#co
12461246
1
12471247
);
12481248
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1249+
attribute: 'category',
12491250
eventType: 'click',
12501251
insightsMethod: 'clickedFilters',
12511252
payload: {

‎src/connectors/numeric-menu/__tests__/connectNumericMenu-test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
10491049
1
10501050
);
10511051
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1052+
attribute: 'numerics',
10521053
eventType: 'click',
10531054
insightsMethod: 'clickedFilters',
10541055
payload: {
@@ -1064,6 +1065,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/numeric-men
10641065
2
10651066
);
10661067
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1068+
attribute: 'numerics',
10671069
eventType: 'click',
10681070
insightsMethod: 'clickedFilters',
10691071
payload: {

‎src/connectors/numeric-menu/connectNumericMenu.ts

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ const createSendEvent = ({ instantSearchInstance, helper, attribute }) => (
133133
index: helper.getIndex(),
134134
filters,
135135
},
136+
attribute,
136137
});
137138
}
138139
};

‎src/connectors/range/__tests__/connectRange-test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
21842184
1
21852185
);
21862186
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
2187+
attribute: 'price',
21872188
eventType: 'click',
21882189
insightsMethod: 'clickedFilters',
21892190
payload: {
@@ -2219,6 +2220,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/range-input
22192220
2
22202221
);
22212222
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
2223+
attribute: 'price',
22222224
eventType: 'click',
22232225
insightsMethod: 'clickedFilters',
22242226
payload: {

‎src/connectors/range/connectRange.ts

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ const connectRange: ConnectRange = function connectRange(
244244
index: helper.getIndex(),
245245
filters,
246246
},
247+
attribute,
247248
});
248249
}
249250
};

‎src/connectors/rating-menu/__tests__/connectRatingMenu-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
803803
1
804804
);
805805
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
806+
attribute: 'swag',
806807
eventType: 'click',
807808
insightsMethod: 'clickedFilters',
808809
payload: {
@@ -835,6 +836,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/rating-menu
835836
2
836837
);
837838
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
839+
attribute: 'swag',
838840
eventType: 'click',
839841
insightsMethod: 'clickedFilters',
840842
payload: {

‎src/connectors/rating-menu/connectRatingMenu.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const createSendEvent = ({
4141
index: helper.getIndex(),
4242
filters: [`${attribute}>=${facetValue}`],
4343
},
44+
attribute,
4445
});
4546
}
4647
};

‎src/connectors/refinement-list/__tests__/connectRefinementList-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/refinement-
28602860
1
28612861
);
28622862
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
2863+
attribute: 'category',
28632864
eventType: 'click',
28642865
insightsMethod: 'clickedFilters',
28652866
payload: {

‎src/connectors/toggle-refinement/__tests__/connectToggleRefinement-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/toggle-refi
13221322
1
13231323
);
13241324
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
1325+
attribute: 'isShippingFree',
13251326
eventType: 'click',
13261327
insightsMethod: 'clickedFilters',
13271328
payload: {

‎src/connectors/toggle-refinement/connectToggleRefinement.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const createSendEvent = ({ instantSearchInstance, attribute, on, helper }) => (
3737
index: helper.getIndex(),
3838
filters: on.map(value => `${attribute}:${value}`),
3939
},
40+
attribute,
4041
});
4142
}
4243
};

‎src/lib/utils/__tests__/createSendEventForFacet-test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ If you want to send a custom payload, you can pass one object: sendEvent(customP
8282
1
8383
);
8484
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
85+
attribute: 'category',
8586
eventType: 'click',
8687
insightsMethod: 'clickedFilters',
8788
payload: {
@@ -100,6 +101,7 @@ If you want to send a custom payload, you can pass one object: sendEvent(customP
100101
1
101102
);
102103
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
104+
attribute: 'category',
103105
eventType: 'click',
104106
insightsMethod: 'clickedFilters',
105107
payload: {

‎src/lib/utils/__tests__/createSendEventForHits-test.ts

+56-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
12
import {
2-
createSendEventForHits,
33
createBindEventForHits,
4+
createSendEventForHits,
45
} from '../createSendEventForHits';
5-
import { createInstantSearch } from '../../../../test/mock/createInstantSearch';
66

77
const createTestEnvironment = () => {
88
const instantSearchInstance = createInstantSearch();
@@ -93,6 +93,13 @@ describe('createSendEventForHits', () => {
9393
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
9494
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
9595
eventType: 'view',
96+
hits: [
97+
{
98+
__position: 0,
99+
__queryID: 'test-query-id',
100+
objectID: 'obj0',
101+
},
102+
],
96103
insightsMethod: 'viewedObjectIDs',
97104
payload: {
98105
eventName: 'Hits Viewed',
@@ -109,6 +116,13 @@ describe('createSendEventForHits', () => {
109116
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
110117
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
111118
eventType: 'view',
119+
hits: [
120+
{
121+
__position: 0,
122+
__queryID: 'test-query-id',
123+
objectID: 'obj0',
124+
},
125+
],
112126
insightsMethod: 'viewedObjectIDs',
113127
payload: {
114128
eventName: 'Products Displayed',
@@ -125,6 +139,18 @@ describe('createSendEventForHits', () => {
125139
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
126140
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
127141
eventType: 'view',
142+
hits: [
143+
{
144+
__position: 0,
145+
__queryID: 'test-query-id',
146+
objectID: 'obj0',
147+
},
148+
{
149+
__position: 1,
150+
__queryID: 'test-query-id',
151+
objectID: 'obj1',
152+
},
153+
],
128154
insightsMethod: 'viewedObjectIDs',
129155
payload: {
130156
eventName: 'Hits Viewed',
@@ -141,6 +167,13 @@ describe('createSendEventForHits', () => {
141167
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
142168
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
143169
eventType: 'click',
170+
hits: [
171+
{
172+
__position: 0,
173+
__queryID: 'test-query-id',
174+
objectID: 'obj0',
175+
},
176+
],
144177
insightsMethod: 'clickedObjectIDsAfterSearch',
145178
payload: {
146179
eventName: 'Product Clicked',
@@ -159,6 +192,13 @@ describe('createSendEventForHits', () => {
159192
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledTimes(1);
160193
expect(instantSearchInstance.sendEventToInsights).toHaveBeenCalledWith({
161194
eventType: 'conversion',
195+
hits: [
196+
{
197+
__position: 0,
198+
__queryID: 'test-query-id',
199+
objectID: 'obj0',
200+
},
201+
],
162202
insightsMethod: 'convertedObjectIDsAfterSearch',
163203
payload: {
164204
eventName: 'Product Ordered',
@@ -197,6 +237,13 @@ describe('createBindEventForHits', () => {
197237
);
198238
expect(parsedPayload).toEqual({
199239
eventType: 'click',
240+
hits: [
241+
{
242+
__position: 0,
243+
__queryID: 'test-query-id',
244+
objectID: 'obj0',
245+
},
246+
],
200247
insightsMethod: 'clickedObjectIDsAfterSearch',
201248
payload: {
202249
eventName: 'Product Clicked',
@@ -216,6 +263,13 @@ describe('createBindEventForHits', () => {
216263
);
217264
expect(parsedPayload).toEqual({
218265
eventType: 'conversion',
266+
hits: [
267+
{
268+
__position: 0,
269+
__queryID: 'test-query-id',
270+
objectID: 'obj0',
271+
},
272+
],
219273
insightsMethod: 'convertedObjectIDsAfterSearch',
220274
payload: {
221275
eventName: 'Product Ordered',

‎src/lib/utils/createSendEventForFacet.ts

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function createSendEventForFacet({
4242
index: helper.getIndex(),
4343
filters: [`${attribute}:${facetValue}`],
4444
},
45+
attribute,
4546
});
4647
}
4748
} else if (__DEV__) {

‎src/lib/utils/createSendEventForHits.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { InstantSearch, Hit } from '../../types';
1+
import { InstantSearch, Hit, Hits, EscapedHits } from '../../types';
22
import { InsightsEvent } from '../../middlewares/createInsightsMiddleware';
33

44
type BuiltInSendEventForHits = (
55
eventType: string,
6-
hits: Hit | Hit[],
6+
hits: Hit | Hits,
77
eventName?: string
88
) => void;
99
type CustomSendEventForHits = (customPayload: any) => void;
1010
export type SendEventForHits = BuiltInSendEventForHits & CustomSendEventForHits;
1111

1212
type BuiltInBindEventForHits = (
1313
eventType: string,
14-
hits: Hit | Hit[],
14+
hits: Hit | Hits,
1515
eventName?: string
1616
) => string;
1717
type CustomBindEventForHits = (customPayload: any) => string;
@@ -34,7 +34,7 @@ const buildPayload: BuildPayload = ({
3434
return args[0];
3535
}
3636
const eventType: string = args[0];
37-
const hits: Hit | Hit[] = args[1];
37+
const hits: Hit | Hits | EscapedHits = args[1];
3838
const eventName: string | undefined = args[2];
3939
if (!hits) {
4040
if (__DEV__) {
@@ -60,7 +60,10 @@ const buildPayload: BuildPayload = ({
6060
return null;
6161
}
6262
}
63-
const hitsArray = Array.isArray(hits) ? hits : [hits];
63+
const hitsArray: Hits = Array.isArray(hits)
64+
? removeEscapedFromHits(hits)
65+
: [hits];
66+
6467
if (hitsArray.length === 0) {
6568
return null;
6669
}
@@ -78,6 +81,7 @@ const buildPayload: BuildPayload = ({
7881
index,
7982
objectIDs,
8083
},
84+
hits: hitsArray,
8185
};
8286
} else if (eventType === 'click') {
8387
return {
@@ -91,6 +95,7 @@ const buildPayload: BuildPayload = ({
9195
objectIDs,
9296
positions,
9397
},
98+
hits: hitsArray,
9499
};
95100
} else if (eventType === 'conversion') {
96101
return {
@@ -103,6 +108,7 @@ const buildPayload: BuildPayload = ({
103108
queryID,
104109
objectIDs,
105110
},
111+
hits: hitsArray,
106112
};
107113
} else if (__DEV__) {
108114
throw new Error(`eventType("${eventType}") is not supported.
@@ -113,6 +119,12 @@ const buildPayload: BuildPayload = ({
113119
}
114120
};
115121

122+
function removeEscapedFromHits(hits: Hits | EscapedHits): Hits {
123+
// this returns without `hits.__escaped`
124+
// and this way it doesn't mutate the original `hits`
125+
return hits.map(hit => hit);
126+
}
127+
116128
export function createSendEventForHits({
117129
instantSearchInstance,
118130
index,

‎src/middlewares/createInsightsMiddleware.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { InsightsClient, InsightsClientMethod, Middleware } from '../types';
1+
import {
2+
InsightsClient,
3+
InsightsClientMethod,
4+
Middleware,
5+
Hit,
6+
} from '../types';
27
import { getInsightsAnonymousUserTokenInternal } from '../helpers';
38
import { warning, noop, getAppIdAndApiKey, find } from '../lib/utils';
49

@@ -7,6 +12,8 @@ export type InsightsEvent = {
712
payload: any;
813
widgetType: string;
914
eventType: string; // 'view' | 'click' | 'conversion', but we're not restricting.
15+
hits?: Hit[];
16+
attribute?: string;
1017
};
1118

1219
export type InsightsProps = {

‎src/widgets/hits/__tests__/hits-integration-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ describe('hits', () => {
9292
expect(onEvent).toHaveBeenCalledWith(
9393
{
9494
eventType: 'view',
95+
hits: [
96+
{
97+
__position: 1,
98+
objectID: 'object-id0',
99+
title: 'title 1',
100+
},
101+
{
102+
__position: 2,
103+
objectID: 'object-id1',
104+
title: 'title 2',
105+
},
106+
],
95107
insightsMethod: 'viewedObjectIDs',
96108
payload: {
97109
eventName: 'Hits Viewed',
@@ -129,6 +141,14 @@ describe('hits', () => {
129141
expect(onEvent).toHaveBeenCalledTimes(2);
130142
expect(onEvent.mock.calls[onEvent.mock.calls.length - 1][0]).toEqual({
131143
eventType: 'click',
144+
hits: [
145+
{
146+
__hitIndex: 0,
147+
__position: 1,
148+
objectID: 'object-id0',
149+
title: 'title 1',
150+
},
151+
],
132152
insightsMethod: 'clickedObjectIDsAfterSearch',
133153
payload: {
134154
eventName: 'Item Clicked',
@@ -169,6 +189,14 @@ describe('hits', () => {
169189
expect(onEvent).toHaveBeenCalledTimes(2);
170190
expect(onEvent.mock.calls[onEvent.mock.calls.length - 1][0]).toEqual({
171191
eventType: 'conversion',
192+
hits: [
193+
{
194+
__hitIndex: 1,
195+
__position: 2,
196+
objectID: 'object-id1',
197+
title: 'title 2',
198+
},
199+
],
172200
insightsMethod: 'convertedObjectIDsAfterSearch',
173201
payload: {
174202
eventName: 'Product Ordered',

‎src/widgets/infinite-hits/__tests__/infinite-hits-integration-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ Object {
325325
expect(onEvent).toHaveBeenCalledWith(
326326
{
327327
eventType: 'view',
328+
hits: [
329+
{
330+
__position: 1,
331+
objectID: 'object-id0',
332+
title: 'title 1',
333+
},
334+
{
335+
__position: 2,
336+
objectID: 'object-id1',
337+
title: 'title 2',
338+
},
339+
],
328340
insightsMethod: 'viewedObjectIDs',
329341
payload: {
330342
eventName: 'Hits Viewed',
@@ -362,6 +374,14 @@ Object {
362374
expect(onEvent).toHaveBeenCalledTimes(2);
363375
expect(onEvent.mock.calls[onEvent.mock.calls.length - 1][0]).toEqual({
364376
eventType: 'click',
377+
hits: [
378+
{
379+
__hitIndex: 0,
380+
__position: 1,
381+
objectID: 'object-id0',
382+
title: 'title 1',
383+
},
384+
],
365385
insightsMethod: 'clickedObjectIDsAfterSearch',
366386
payload: {
367387
eventName: 'Item Clicked',
@@ -402,6 +422,14 @@ Object {
402422
expect(onEvent).toHaveBeenCalledTimes(2);
403423
expect(onEvent.mock.calls[onEvent.mock.calls.length - 1][0]).toEqual({
404424
eventType: 'conversion',
425+
hits: [
426+
{
427+
__hitIndex: 1,
428+
__position: 2,
429+
objectID: 'object-id1',
430+
title: 'title 2',
431+
},
432+
],
405433
insightsMethod: 'convertedObjectIDsAfterSearch',
406434
payload: {
407435
eventName: 'Product Ordered',

0 commit comments

Comments
 (0)
Please sign in to comment.