Skip to content

Commit

Permalink
feat: make events default implementation compatible with composition …
Browse files Browse the repository at this point in the history
…API (#6569)

* feat: get index from results if it exists

* chore: update implementations dependant on createSendEventForHits

* chore: update tests

* fix: bundle size

* chore: use createSearchClient in test

* Update packages/instantsearch.js/src/connectors/rating-menu/connectRatingMenu.ts

Co-authored-by: Haroen Viaene <[email protected]>

---------

Co-authored-by: Haroen Viaene <[email protected]>
  • Loading branch information
e-krebs and Haroenv authored Feb 18, 2025
1 parent a5f9505 commit f43c40f
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
{
"path": "./packages/instantsearch.js/dist/instantsearch.production.min.js",
"maxSize": "84 kB"
"maxSize": "84.25 kB"
},
{
"path": "./packages/instantsearch.js/dist/instantsearch.development.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ search.addWidgets([
hits: firstIndexHits,
}),
]),
helper,
helper: algoliasearchHelper(searchClient, 'indexName0'),
},
{
indexId: 'indexId1',
Expand All @@ -762,7 +762,7 @@ search.addWidgets([
hits: secondIndexHits,
}),
]),
helper,
helper: algoliasearchHelper(searchClient, 'indexName1'),
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ search.addWidgets([

const sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => scopedResult.results?.index || '',
helper: scopedResult.helper,
widgetType: this.$$type,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default (function connectFrequentlyBoughtTogether<
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default (function connectGeoSearch<
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: $$type,
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/instantsearch.js/src/connectors/hits/connectHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ export default (function connectHits<TWidgetParams>(
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
}

if (!bindEvent) {
bindEvent = createBindEventForHits({
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
instantSearchInstance,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ export default (function connectInfiniteHits<
showMore = getShowMore(helper);
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
bindEvent = createBindEventForHits({
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
instantSearchInstance,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default (function connectLookingSimilar<
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const createSendEvent: CreateSendEvent =
eventModifier,
payload: {
eventName,
index: helper.getIndex(),
index: helper.lastResults?.index || helper.state.index,
filters: [`${attribute}>=${facetValue}`],
},
attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default (function connectRelatedProducts<
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const createSendEvent = ({
eventModifier,
payload: {
eventName,
index: helper.getIndex(),
index: helper.lastResults?.index || helper.state.index,
filters: on.map((value) => `${attribute}:${value}`),
},
attribute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export default (function connectTrendingItems<
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType: this.$$type,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
*/
/** @jsx h */

import { createSearchClient } from '@instantsearch/mocks';
import { render, fireEvent } from '@testing-library/preact';
import algoliasearchHelper from 'algoliasearch-helper';
import { h } from 'preact';

import { createInstantSearch } from '../../../../test/createInstantSearch';
Expand Down Expand Up @@ -128,7 +130,7 @@ describe('createInsightsEventHandler', () => {
const payload = serializePayload(
_buildEventPayloadsForHits({
widgetType: 'ais.hits',
getIndex: () => 'instant_search',
helper: algoliasearchHelper(createSearchClient(), 'instant_search'),
instantSearchInstance: createInstantSearch(),
methodName: 'bindEvent',
args: ['click', { objectID: '1', __position: 1 }, 'Hit Clicked'],
Expand Down Expand Up @@ -172,7 +174,7 @@ describe('createInsightsEventHandler', () => {
const payload = serializePayload(
_buildEventPayloadsForHits({
widgetType: 'ais.hits',
getIndex: () => 'instant_search',
helper: algoliasearchHelper(createSearchClient(), 'instant_search'),
instantSearchInstance: createInstantSearch(),
methodName: 'bindEvent',
args: ['click', { objectID: '1', __position: 1 }, 'Hit Clicked'],
Expand Down Expand Up @@ -246,7 +248,7 @@ describe('createInsightsEventHandler', () => {
const modernPayload = serializePayload(
_buildEventPayloadsForHits({
widgetType: 'ais.hits',
getIndex: () => 'instant_search',
helper: algoliasearchHelper(createSearchClient(), 'instant_search'),
instantSearchInstance: createInstantSearch(),
methodName: 'bindEvent',
args: ['click', { objectID: '1', __position: 1 }, 'Product Clicked'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ const createTestEnvironment = ({
};
const sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
helper,
widgetType,
});
const bindEvent = createBindEventForHits({
getIndex: () => helper.getIndex(),
helper,
widgetType,
instantSearchInstance,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function createSendEventForFacet({
eventModifier,
payload: {
eventName,
index: helper.getIndex(),
index: helper.lastResults?.index || helper.state.index,
filters: [`${attribute}:${facetValue}`],
...additionalData,
},
Expand Down
23 changes: 12 additions & 11 deletions packages/instantsearch.js/src/lib/utils/createSendEventForHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { serializePayload } from './serializer';

import type { InsightsEvent } from '../../middlewares/createInsightsMiddleware';
import type { InstantSearch, Hit, EscapedHits } from '../../types';
import type { AlgoliaSearchHelper } from 'algoliasearch-helper';

type BuiltInSendEventForHits = (
eventType: string,
Expand Down Expand Up @@ -30,14 +31,14 @@ function chunk<TItem>(arr: TItem[], chunkSize: number = 20): TItem[][] {
}

export function _buildEventPayloadsForHits({
getIndex,
helper,
widgetType,
methodName,
args,
instantSearchInstance,
}: {
widgetType: string;
getIndex: () => string;
helper: AlgoliaSearchHelper;
methodName: 'sendEvent' | 'bindEvent';
args: any[];
instantSearchInstance: InstantSearch;
Expand Down Expand Up @@ -101,7 +102,7 @@ export function _buildEventPayloadsForHits({
eventType,
payload: {
eventName: eventName || 'Hits Viewed',
index: getIndex(),
index: helper.lastResults?.index || helper.state.index,
objectIDs: objectIDsByChunk[i],
...additionalData,
},
Expand All @@ -117,7 +118,7 @@ export function _buildEventPayloadsForHits({
eventType,
payload: {
eventName: eventName || 'Hit Clicked',
index: getIndex(),
index: helper.lastResults?.index || helper.state.index,
queryID,
objectIDs: objectIDsByChunk[i],
positions: positionsByChunk[i],
Expand All @@ -135,7 +136,7 @@ export function _buildEventPayloadsForHits({
eventType,
payload: {
eventName: eventName || 'Hit Converted',
index: getIndex(),
index: helper.lastResults?.index || helper.state.index,
queryID,
objectIDs: objectIDsByChunk[i],
...additionalData,
Expand All @@ -155,11 +156,11 @@ export function _buildEventPayloadsForHits({

export function createSendEventForHits({
instantSearchInstance,
getIndex,
helper,
widgetType,
}: {
instantSearchInstance: InstantSearch;
getIndex: () => string;
helper: AlgoliaSearchHelper;
widgetType: string;
}): SendEventForHits {
let sentEvents: Record<InsightsEvent['eventType'], boolean> = {};
Expand All @@ -168,7 +169,7 @@ export function createSendEventForHits({
const sendEventForHits: SendEventForHits = (...args: any[]) => {
const payloads = _buildEventPayloadsForHits({
widgetType,
getIndex,
helper,
methodName: 'sendEvent',
args,
instantSearchInstance,
Expand Down Expand Up @@ -196,18 +197,18 @@ export function createSendEventForHits({
}

export function createBindEventForHits({
getIndex,
helper,
widgetType,
instantSearchInstance,
}: {
getIndex: () => string;
helper: AlgoliaSearchHelper;
widgetType: string;
instantSearchInstance: InstantSearch;
}): BindEventForHits {
const bindEventForHits: BindEventForHits = (...args: any[]) => {
const payloads = _buildEventPayloadsForHits({
widgetType,
getIndex,
helper,
methodName: 'bindEvent',
args,
instantSearchInstance,
Expand Down

0 comments on commit f43c40f

Please sign in to comment.