Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(recommend): add support for event collection in recommend #6523

Merged
merged 15 commits into from
Jan 15, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
RecommendItemComponentProps,
RecordWithObjectID,
Renderer,
SendEventForHits,
} from '../types';

export type CarouselProps<
Expand All @@ -28,6 +29,7 @@ export type CarouselProps<
nextIconComponent?: () => JSX.Element;
classNames?: Partial<CarouselClassNames>;
translations?: Partial<CarouselTranslations>;
sendEvent: SendEventForHits;
};

export type CarouselClassNames = {
Expand Down Expand Up @@ -135,6 +137,7 @@ export function createCarouselComponent({ createElement, Fragment }: Renderer) {
nextIconComponent: NextIconComponent = NextIconDefaultComponent,
items,
translations: userTranslations,
sendEvent,
...props
} = userProps;

Expand Down Expand Up @@ -235,6 +238,12 @@ export function createCarouselComponent({ createElement, Fragment }: Renderer) {
className={cx(cssClasses.item)}
aria-roledescription="slide"
aria-label={`${index + 1} of ${items.length}`}
onClick={() => {
sendEvent('click:internal', item, 'Item Clicked');
}}
onAuxClick={() => {
sendEvent('click:internal', item, 'Item Clicked');
}}
>
<ItemComponent item={item} />
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('Carousel', () => {
test('renders items', () => {
const { container } = render(
<CarouselWithRefs
sendEvent={jest.fn()}
items={[
{
objectID: '1',
Expand Down Expand Up @@ -138,6 +139,7 @@ describe('Carousel', () => {
test('renders custom "Previous" and "Next" components', () => {
const { container } = render(
<CarouselWithRefs
sendEvent={jest.fn()}
items={[
{
objectID: '1',
Expand Down Expand Up @@ -215,6 +217,7 @@ describe('Carousel', () => {
test('accepts custom translations', () => {
const { container } = render(
<CarouselWithRefs
sendEvent={jest.fn()}
items={[{ objectID: '1', __position: 1 }]}
itemComponent={ItemComponent}
translations={{
Expand Down Expand Up @@ -248,6 +251,7 @@ describe('Carousel', () => {
test('forwards `div` props to the root element', () => {
const { container } = render(
<CarouselWithRefs
sendEvent={jest.fn()}
items={[{ objectID: '1', __position: 1 }]}
itemComponent={ItemComponent}
hidden={true}
Expand All @@ -262,6 +266,7 @@ describe('Carousel', () => {
test('accepts custom class names', () => {
const { container } = render(
<CarouselWithRefs
sendEvent={jest.fn()}
items={[{ objectID: '1', __position: 1 }]}
itemComponent={ItemComponent}
classNames={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export function createListComponent({ createElement }: Renderer) {
<li
key={item.objectID}
className={classNames.item}
onClick={sendEvent}
onAuxClick={sendEvent}
onClick={() => {
sendEvent('click:internal', item, 'Item Clicked');
}}
onAuxClick={() => {
sendEvent('click:internal', item, 'Item Clicked');
}}
>
<ItemComponent item={item} />
</li>
Expand Down
3 changes: 2 additions & 1 deletion packages/instantsearch-ui-components/src/types/Recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export type RecommendInnerComponentProps<TObject> = {

export type RecordWithObjectID<TObject = Record<string, unknown>> = TObject & {
objectID: string;
// @TODO: once events are implemented, this type needs `__position` and `__queryID`
__position: number;
__queryID?: string;
};

export type RecommendItemComponentProps<TObject> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type { SendEventForHits } from '../../lib/utils';
import type {
Connector,
TransformItems,
Expand All @@ -30,6 +34,11 @@ export type FrequentlyBoughtTogetherRenderState<
* The matched recommendations from Algolia API.
*/
items: Array<AlgoliaHit<THit>>;

/**
* Sends an event to the Insights middleware.
*/
sendEvent: SendEventForHits;
};

export type FrequentlyBoughtTogetherConnectorParams<
Expand Down Expand Up @@ -118,6 +127,8 @@ export default (function connectFrequentlyBoughtTogether<
throw new Error(withUsage('The `objectIDs` option is required.'));
}

let sendEvent: SendEventForHits;

return {
dependsOn: 'recommend',
$$type: 'ais.frequentlyBoughtTogether',
Expand Down Expand Up @@ -148,23 +159,45 @@ export default (function connectFrequentlyBoughtTogether<
return renderState;
},

getWidgetRenderState({ results }) {
getWidgetRenderState({ results, helper, instantSearchInstance }) {
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
});
}
if (results === null || results === undefined) {
return { items: [], widgetParams };
return { items: [], widgetParams, sendEvent };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

const itemsWithAbsolutePosition = addAbsolutePosition(
results.hits,
0,
1
);

const itemsWithAbsolutePositionAndQueryID = addQueryID(
itemsWithAbsolutePosition,
results.queryID
);

const transformedItems = transformItems(
results.hits as Array<AlgoliaHit<THit>>,
itemsWithAbsolutePositionAndQueryID,
{
results: results as RecommendResponse<AlgoliaHit<THit>>,
}
);

return { items: transformedItems, widgetParams };
return {
items: transformedItems,
widgetParams,
sendEvent,
};
},

dispose({ recommendState }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type { SendEventForHits } from '../../lib/utils';
import type {
Connector,
TransformItems,
Expand All @@ -30,6 +34,10 @@ export type LookingSimilarRenderState<
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;
/**
* Sends an event to the Insights middleware.
*/
sendEvent: SendEventForHits;
};

export type LookingSimilarConnectorParams<
Expand Down Expand Up @@ -121,6 +129,8 @@ export default (function connectLookingSimilar<
throw new Error(withUsage('The `objectIDs` option is required.'));
}

let sendEvent: SendEventForHits;

return {
dependsOn: 'recommend',
$$type: 'ais.lookingSimilar',
Expand Down Expand Up @@ -151,20 +161,44 @@ export default (function connectLookingSimilar<
return renderState;
},

getWidgetRenderState({ results }) {
getWidgetRenderState({ results, helper, instantSearchInstance }) {
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
});
}
if (results === null || results === undefined) {
return { items: [], widgetParams };
return { items: [], widgetParams, sendEvent };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

return {
items: transformItems(results.hits as Array<AlgoliaHit<THit>>, {
const itemsWithAbsolutePosition = addAbsolutePosition(
results.hits,
0,
1
);

const itemsWithAbsolutePositionAndQueryID = addQueryID(
itemsWithAbsolutePosition,
results.queryID
);

const transformedItems = transformItems(
itemsWithAbsolutePositionAndQueryID,
{
results: results as RecommendResponse<AlgoliaHit<THit>>,
}),
}
);

return {
items: transformedItems,
widgetParams,
sendEvent,
};
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type { SendEventForHits } from '../../lib/utils';
import type {
Connector,
TransformItems,
Expand All @@ -30,6 +34,11 @@ export type RelatedProductsRenderState<
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;

/**
* Sends an event to the Insights middleware.
*/
sendEvent: SendEventForHits;
};

export type RelatedProductsConnectorParams<
Expand Down Expand Up @@ -121,6 +130,8 @@ export default (function connectRelatedProducts<
throw new Error(withUsage('The `objectIDs` option is required.'));
}

let sendEvent: SendEventForHits;

return {
dependsOn: 'recommend',
$$type: 'ais.relatedProducts',
Expand Down Expand Up @@ -151,20 +162,44 @@ export default (function connectRelatedProducts<
return renderState;
},

getWidgetRenderState({ results }) {
getWidgetRenderState({ results, helper, instantSearchInstance }) {
if (!sendEvent) {
sendEvent = createSendEventForHits({
instantSearchInstance,
getIndex: () => helper.getIndex(),
widgetType: this.$$type,
});
}
if (results === null || results === undefined) {
return { items: [], widgetParams };
return { items: [], widgetParams, sendEvent };
}

if (escapeHTML && results.hits.length > 0) {
results.hits = escapeHits(results.hits);
}

return {
items: transformItems(results.hits as Array<AlgoliaHit<THit>>, {
const itemsWithAbsolutePosition = addAbsolutePosition(
results.hits,
0,
1
);

const itemsWithAbsolutePositionAndQueryID = addQueryID(
itemsWithAbsolutePosition,
results.queryID
);

const transformedItems = transformItems(
itemsWithAbsolutePositionAndQueryID,
{
results: results as RecommendResponse<AlgoliaHit<THit>>,
}),
}
);

return {
items: transformedItems,
widgetParams,
sendEvent,
};
},

Expand Down
Loading