Skip to content

Commit

Permalink
fix(sendEvent): split > 20 objects in multiple calls (#4841)
Browse files Browse the repository at this point in the history
* fix(sendEvent): split > 20 objects in multiple calls

DX-2379

* bindEvent allows multiple now too
  • Loading branch information
Haroenv authored Aug 24, 2021
1 parent 047acd0 commit 44574bc
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 182 deletions.
74 changes: 39 additions & 35 deletions src/connectors/hits/__tests__/connectHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,26 +846,28 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual({
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
).toEqual([
{
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
widgetType: 'ais.hits',
},
widgetType: 'ais.hits',
});
]);
});

it('returns a payload for conversion event', () => {
Expand All @@ -876,25 +878,27 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/hits/js/#co
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual({
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
).toEqual([
{
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
widgetType: 'ais.hits',
},
widgetType: 'ais.hits',
});
]);
});
});
});
Expand Down
74 changes: 39 additions & 35 deletions src/connectors/infinite-hits/__tests__/connectInfiniteHits-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,26 +1468,28 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual({
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
).toEqual([
{
eventType: 'click',
hits: [
{
__position: 0,
__queryID: 'test-query-id',
fake: 'data',
objectID: '1',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
},
],
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Product Added',
index: '',
objectIDs: ['1'],
positions: [0],
queryID: 'test-query-id',
widgetType: 'ais.infiniteHits',
},
widgetType: 'ais.infiniteHits',
});
]);
});

it('returns a payload for conversion event', () => {
Expand All @@ -1498,25 +1500,27 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/infinite-hi
expect(payload.startsWith('data-insights-event=')).toBe(true);
expect(
deserializePayload(payload.substr('data-insights-event='.length))
).toEqual({
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
).toEqual([
{
eventType: 'conversion',
hits: [
{
__position: 1,
__queryID: 'test-query-id',
objectID: '2',
sample: 'infos',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
},
],
insightsMethod: 'convertedObjectIDsAfterSearch',
payload: {
eventName: 'Product Ordered',
index: '',
objectIDs: ['2'],
queryID: 'test-query-id',
widgetType: 'ais.infiniteHits',
},
widgetType: 'ais.infiniteHits',
});
]);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function readDataAttributes(domElement: HTMLElement): {
}

try {
const payload: Partial<InsightsClientPayload> =
deserializePayload(serializedPayload);
const payload =
deserializePayload<Partial<InsightsClientPayload>>(serializedPayload);
return { method, payload };
} catch (error) {
throw new Error(
Expand Down
9 changes: 4 additions & 5 deletions src/lib/insights/listener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const findInsightsTarget = (
return element;
};

type ParseInsightsEvent = (element: HTMLElement) => InsightsEvent;

const parseInsightsEvent: ParseInsightsEvent = (element) => {
const parseInsightsEvent = (element: HTMLElement): InsightsEvent[] => {
const serializedPayload = element.getAttribute('data-insights-event');

if (typeof serializedPayload !== 'string') {
Expand All @@ -39,7 +37,7 @@ const parseInsightsEvent: ParseInsightsEvent = (element) => {
}

try {
return deserializePayload(serializedPayload) as InsightsEvent;
return deserializePayload(serializedPayload);
} catch (error) {
throw new Error(
'The insights middleware was unable to parse `data-insights-event`.'
Expand All @@ -59,7 +57,8 @@ const insightsListener = (BaseComponent: any) => {
);
if (targetWithEvent) {
const payload = parseInsightsEvent(targetWithEvent);
props.sendEvent(payload);

payload.forEach((single) => props.sendEvent!(single));
}
}

Expand Down
Loading

0 comments on commit 44574bc

Please sign in to comment.