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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +4,10 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
SendEventForHits,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type {
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,6 +4,10 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
SendEventForHits,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type {
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,6 +4,10 @@ import {
noop,
escapeHits,
TAG_PLACEHOLDER,
SendEventForHits,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type {
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import {
escapeHits,
TAG_PLACEHOLDER,
getObjectType,
SendEventForHits,
createSendEventForHits,
addAbsolutePosition,
addQueryID,
} from '../../lib/utils';

import type {
Expand All @@ -31,6 +35,11 @@ export type TrendingItemsRenderState<
* The matched recommendations from the Algolia API.
*/
items: Array<AlgoliaHit<THit>>;

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

export type TrendingItemsConnectorParams<
Expand Down Expand Up @@ -141,6 +150,8 @@ export default (function connectTrendingItems<
);
}

let sendEvent: SendEventForHits;

return {
dependsOn: 'recommend',
$$type: 'ais.trendingItems',
Expand Down Expand Up @@ -171,20 +182,44 @@ export default (function connectTrendingItems<
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