Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion examples/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"react": "17.0.2",
"react-dom": "17.0.2",
"react-router-dom": "^6.17.0",
"search-insights": "2.4.0"
"search-insights": "2.17.3"
},
"devDependencies": {
"@babel/core": "7.21.4",
Expand Down
9 changes: 5 additions & 4 deletions examples/demo/src/components/ComparisonChartItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import './style.css';
import { InsightsClient } from 'search-insights';

import { indexName } from '../../config';
import { ProductHit, ProductReviews } from '../../types';
import { ButtonComponent } from '../common';

Expand Down Expand Up @@ -56,10 +55,12 @@ export const ComparisonChartItem: React.FC<ChartItemProps<ProductHit>> = ({
event.preventDefault();

onSelect(item);
insights('clickedObjectIDs', {
insights('clickedObjectIDsAfterSearch', {
objectIDs: [item.objectID],
eventName: 'Product Clicked',
index: indexName,
index: item.__indexName,
positions: [item.__position],
queryID: item.__queryID,
});
}}
>
Expand All @@ -78,7 +79,7 @@ export const ComparisonChartItem: React.FC<ChartItemProps<ProductHit>> = ({
insights('convertedObjectIDsAfterSearch', {
eventName: 'Product Added To Cart',
objectIDs: [item.objectID],
index: indexName,
index: item.__indexName,
queryID: item.__queryID,
});
}}
Expand Down
10 changes: 1 addition & 9 deletions examples/demo/src/components/Facet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { TrendingFacetHit } from '@algolia/recommend';
import React from 'react';
import { InsightsClient } from 'search-insights';

import './style.css';

type FacetProps = {
hit: TrendingFacetHit;
indexName: string;
insights: InsightsClient;
onSelect(hit: TrendingFacetHit): void;
};

export function Facet({ hit, onSelect, indexName, insights }: FacetProps) {
export function Facet({ hit, onSelect }: FacetProps) {
return (
<div
className="Hit"
onClick={(event) => {
event.preventDefault();

onSelect(hit);
insights('clickedFilters', {
index: indexName,
eventName: 'Facet Clicked',
filters: [`${hit.facetName}:${hit.facetValue}`],
});
}}
>
<div className="Facet-Content">
Expand Down
9 changes: 5 additions & 4 deletions examples/demo/src/components/Hit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { InsightsClient } from 'search-insights';

import { indexName } from '../../config';
import { ProductHit } from '../../types';
import './style.css';
import { ButtonComponent } from '../common';
Expand All @@ -21,10 +20,12 @@ export function Hit({ hit, onSelect, insights }: HitProps) {
event.preventDefault();

onSelect(hit);
insights('clickedObjectIDs', {
insights('clickedObjectIDsAfterSearch', {
objectIDs: [hit.objectID],
eventName: 'Product Clicked',
index: indexName,
index: hit.__indexName,
positions: [hit.__position],
queryID: hit.__queryID,
});
}}
>
Expand All @@ -45,7 +46,7 @@ export function Hit({ hit, onSelect, insights }: HitProps) {
insights('convertedObjectIDsAfterSearch', {
eventName: 'Product Added To Cart',
objectIDs: [hit.objectID],
index: indexName,
index: hit.__indexName,
queryID: hit.__queryID,
});
}}
Expand Down
5 changes: 4 additions & 1 deletion examples/demo/src/routes/HomePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export const HomePage: React.FC = () => {
itemComponent={({ item }) => (
<Facet
hit={item}
insights={insights}
onSelect={(facetHits) => {
const isSameValue =
selectedFacetValue &&
Expand Down Expand Up @@ -70,13 +69,17 @@ export const HomePage: React.FC = () => {
selectedFacetValue ? `in ${selectedFacetValue.facetValue}` : ''
}`,
}}
queryParameters={{
clickAnalytics: true,
}}
/>
<RecommendedForYou<ProductHit>
recommendClient={recommendClient}
indexName={indexName}
maxRecommendations={15}
queryParameters={{
userToken: 'likes-gender-men',
clickAnalytics: true,
}}
itemComponent={({ item }) => (
<Hit
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/types/ProductHit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ProductReviews = {
};

type WithInsights<THit> = THit & {
__position: string;
__position: number;
__indexName: string;
__queryID: string;
};
Expand Down
4 changes: 3 additions & 1 deletion examples/js-demo/RelatedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export function RelatedItem({ item, insights, onSelect }: RelatedItemProps) {
onClick={(event) => {
event.preventDefault();
onSelect(item);
insights('clickedObjectIDs', {
insights('clickedObjectIDsAfterSearch', {
objectIDs: [item.objectID],
eventName: 'Product Clicked',
index: item.__indexName,
positions: [item.__position],
queryID: item.__queryID,
});
}}
>
Expand Down
2 changes: 1 addition & 1 deletion examples/js-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@babel/runtime": "7.21.0",
"algoliasearch": "4.17.0",
"preact": "10.13.2",
"search-insights": "2.4.0"
"search-insights": "2.17.3"
},
"devDependencies": {
"@babel/core": "7.21.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/js-demo/types/ProductHit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type ProductRecord = {
};

type WithInsights<THit> = THit & {
__position: string;
__position: number;
__indexName: string;
__queryID: string;
};
Expand Down
17 changes: 13 additions & 4 deletions packages/recommend-core/src/getBatchRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {

import { TrendingFacetHit, ProductRecord } from './types';
import { mapByScoreToRecommendations, mapToRecommendations } from './utils';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { version } from './version';

export type BatchKeyPair = {
Expand Down Expand Up @@ -55,9 +57,11 @@ export async function getBatchRecommendations<TObject>({
const { model } = JSON.parse(keyPair.key);

allChunks += keyPair.value;
const { maxRecommendations, transformItems = (x) => x } = queries[
prevChunks
];
const {
indexName,
maxRecommendations,
transformItems = (x) => x,
} = queries[prevChunks];
const splitResult = response?.results?.slice(prevChunks, allChunks);
prevChunks += keyPair.value;

Expand All @@ -71,13 +75,18 @@ export async function getBatchRecommendations<TObject>({
recommendations = mapByScoreToRecommendations<ProductRecord<TObject>>({
maxRecommendations,
hits: splitResult.map((res) => res.hits).flat(),
indexName,
queryID: splitResult.at(0)?.queryID,
});
} else {
recommendations = mapToRecommendations<ProductRecord<TObject>>({
maxRecommendations,
hits: splitResult.map((res) => res.hits),
nrOfObjs: keyPair.value,
});
queryIDs: splitResult.map((res) => res.queryID),
})
.map((hit) => addIndexName(hit, indexName))
.map((hit, idx) => addAbsolutePosition(hit, idx));
}
recommendations = transformItems(recommendations);
results[keyPair.key] = { recommendations, trendingFacets };
Expand Down
8 changes: 8 additions & 0 deletions packages/recommend-core/src/getFrequentlyBoughtTogether.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { FrequentlyBoughtTogetherQuery } from '@algolia/recommend';
import { RecommendationsProps } from './getRecommendations';
import { ProductRecord } from './types';
import { mapToRecommendations } from './utils';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { version } from './version';

export type GetFrequentlyBoughtTogetherProps<
Expand Down Expand Up @@ -40,7 +42,13 @@ export function getFrequentlyBoughtTogether<TObject>({
maxRecommendations,
hits: response.results.map((result) => result.hits),
nrOfObjs: objectIDs.length,
queryIDs: response.results.map((res) => res.queryID),
})
)
.then((hits) =>
hits
.map((hit) => addIndexName(hit, indexName))
.map((hit, idx) => addAbsolutePosition(hit, idx))
)
.then((hits) => ({ recommendations: transformItems(hits) }));
}
8 changes: 8 additions & 0 deletions packages/recommend-core/src/getLookingSimilar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { LookingSimilarQuery } from '@algolia/recommend';
import { RecommendationsProps } from './getRecommendations';
import { ProductRecord } from './types';
import { mapToRecommendations } from './utils';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { version } from './version';

export type GetLookingSimilarProps<TObject> = RecommendationsProps<TObject> &
Expand Down Expand Up @@ -40,7 +42,13 @@ export function getLookingSimilar<TObject>({
maxRecommendations,
hits: response.results.map((result) => result.hits),
nrOfObjs: objectIDs.length,
queryIDs: response.results.map((res) => res.queryID),
})
)
.then((hits) =>
hits
.map((hit) => addIndexName(hit, indexName))
.map((hit, idx) => addAbsolutePosition(hit, idx))
)
.then((hits) => ({ recommendations: transformItems(hits) }));
}
8 changes: 8 additions & 0 deletions packages/recommend-core/src/getRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { RecommendClient, RecommendationsQuery } from '@algolia/recommend';

import { ProductRecord, RecordWithObjectID } from './types';
import { mapToRecommendations } from './utils';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { version } from './version';

export type RecommendationsProps<TObject> = {
Expand Down Expand Up @@ -66,7 +68,13 @@ export function getRecommendations<TObject>({
maxRecommendations,
hits: response.results.map((result) => result.hits),
nrOfObjs: objectIDs.length,
queryIDs: response.results.map((res) => res.queryID),
})
)
.then((hits) =>
hits
.map((hit) => addIndexName(hit, indexName))
.map((hit, idx) => addAbsolutePosition(hit, idx))
)
.then((hits) => ({ recommendations: transformItems(hits) }));
}
14 changes: 12 additions & 2 deletions packages/recommend-core/src/getRecommendedForYou.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Hit } from '@algolia/client-search';
import { RecommendClient, RecommendedForYouParams } from '@algolia/recommend';

import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { addQueryID } from './utils/addQueryID';
import { version } from './version';

export type GetRecommendedForYouProps<TObject> = {
Expand Down Expand Up @@ -36,9 +39,16 @@ export function getRecommendedForYou<TObject>({

return recommendClient
.getRecommendedForYou<TObject>(queries)
.then((hits) => ({
.then((response) => ({
recommendations: transformItems(
hits.results.map((result) => result.hits).flat()
response.results
.map((result) =>
result.hits
.map((hit) => addIndexName(hit, indexName))
.map((hit) => addQueryID(hit, result.queryID))
)
.flat()
.map((hit, idx) => addAbsolutePosition(hit, idx))
),
}));
}
8 changes: 8 additions & 0 deletions packages/recommend-core/src/getRelatedProducts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { RelatedProductsQuery } from '@algolia/recommend';
import { RecommendationsProps } from './getRecommendations';
import { ProductRecord } from './types';
import { mapToRecommendations } from './utils';
import { addAbsolutePosition } from './utils/addAbsolutePosition';
import { addIndexName } from './utils/addIndexName';
import { version } from './version';

export type GetRelatedProductsProps<TObject> = RecommendationsProps<TObject> &
Expand Down Expand Up @@ -39,8 +41,14 @@ export function getRelatedProducts<TObject>({
mapToRecommendations<ProductRecord<TObject>>({
maxRecommendations,
hits: response.results.map((result) => result.hits),
queryIDs: response.results.map((result) => result.queryID),
nrOfObjs: objectIDs.length,
})
)
.then((hits) =>
hits
.map((hit) => addIndexName(hit, indexName))
.map((hit, idx) => addAbsolutePosition(hit, idx))
)
.then((hits) => ({ recommendations: transformItems(hits) }));
}
1 change: 1 addition & 0 deletions packages/recommend-core/src/getTrendingFacets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function getTrendingFacets({
mapByScoreToRecommendations<TrendingFacetHit>({
maxRecommendations,
hits: response.results.map((result) => result.hits).flat(),
indexName,
})
)
.then((hits) => ({ recommendations: transformItems(hits) }));
Expand Down
2 changes: 2 additions & 0 deletions packages/recommend-core/src/getTrendingItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function getTrendingItems<TObject>({
'objectID',
response.results.map((result) => result.hits).flat()
),
indexName,
queryID: response.results?.at(0)?.queryID,
})
)
.then((hits) => ({ recommendations: transformItems(hits) }));
Expand Down
3 changes: 3 additions & 0 deletions packages/recommend-core/src/types/ProductRecord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export type ProductRecord<TObject> = TObject & {
objectID: string;
_score?: number;
__queryID?: string;
__position?: number;
__indexName?: string;
};
3 changes: 3 additions & 0 deletions packages/recommend-core/src/types/RecordWithObjectID.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export type RecordWithObjectID<TObject = {}> = TObject & {
objectID: string;
__queryID?: string;
__position?: number;
__indexName?: string;
};
6 changes: 6 additions & 0 deletions packages/recommend-core/src/utils/addAbsolutePosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function addAbsolutePosition<THit>(hit: THit, index: number): THit {
return {
...hit,
__position: index + 1,
};
}
6 changes: 6 additions & 0 deletions packages/recommend-core/src/utils/addIndexName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function addIndexName<THit>(hit: THit, indexName: string): THit {
return {
...hit,
__indexName: indexName,
};
}
10 changes: 10 additions & 0 deletions packages/recommend-core/src/utils/addQueryID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function addQueryID<THit>(hit: THit, queryID?: string): THit {
if (!queryID) {
return hit;
}

return {
...hit,
__queryID: queryID,
};
}
Loading