Skip to content

Commit 3bc61d5

Browse files
docs(examples): add conversion event in products
1 parent 0032f38 commit 3bc61d5

File tree

7 files changed

+60
-15
lines changed

7 files changed

+60
-15
lines changed

examples/js/app.tsx

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {
44
getAlgoliaHits,
55
snippetHit,
66
} from '@algolia/autocomplete-js';
7-
import { createAlgoliaInsightsPlugin } from '@algolia/autocomplete-plugin-algolia-insights';
7+
import {
8+
AutocompleteInsightsApi,
9+
createAlgoliaInsightsPlugin,
10+
} from '@algolia/autocomplete-plugin-algolia-insights';
811
import { createQuerySuggestionsPlugin } from '@algolia/autocomplete-plugin-query-suggestions';
912
import { createLocalStorageRecentSearchesPlugin } from '@algolia/autocomplete-plugin-recent-searches';
1013
import { Hit } from '@algolia/client-search';
@@ -20,6 +23,8 @@ type Product = {
2023
name: string;
2124
image: string;
2225
description: string;
26+
__autocomplete_indexName: string;
27+
__autocomplete_queryID: string;
2328
};
2429
type ProductHit = Hit<Product>;
2530

@@ -55,7 +60,7 @@ autocomplete({
5560
recentSearchesPlugin,
5661
querySuggestionsPlugin,
5762
],
58-
getSources({ query }) {
63+
getSources({ query, state }) {
5964
if (!query) {
6065
return [];
6166
}
@@ -89,7 +94,12 @@ autocomplete({
8994
);
9095
},
9196
item({ item }) {
92-
return <ProductItem hit={item} />;
97+
return (
98+
<ProductItem
99+
hit={item}
100+
insights={state.context.algoliaInsightsPlugin.insights}
101+
/>
102+
);
93103
},
94104
empty() {
95105
return (
@@ -108,9 +118,10 @@ autocomplete({
108118

109119
type ProductItemProps = {
110120
hit: ProductHit;
121+
insights: AutocompleteInsightsApi;
111122
};
112123

113-
function ProductItem({ hit }: ProductItemProps) {
124+
function ProductItem({ hit, insights }: ProductItemProps) {
114125
return (
115126
<Fragment>
116127
<div className="aa-ItemIcon">
@@ -134,6 +145,37 @@ function ProductItem({ hit }: ProductItemProps) {
134145
<path d="M18.984 6.984h2.016v6h-15.188l3.609 3.609-1.406 1.406-6-6 6-6 1.406 1.406-3.609 3.609h13.172v-4.031z"></path>
135146
</svg>
136147
</button>
148+
<button
149+
className="aa-ItemActionButton"
150+
type="button"
151+
title="Add to cart"
152+
onClick={(event) => {
153+
event.preventDefault();
154+
event.stopPropagation();
155+
156+
insights.convertedObjectIDsAfterSearch({
157+
eventName: 'Added to cart',
158+
index: hit.__autocomplete_indexName,
159+
objectIDs: [hit.objectID],
160+
queryID: hit.__autocomplete_queryID,
161+
});
162+
}}
163+
>
164+
<svg
165+
fill="none"
166+
viewBox="0 0 24 24"
167+
stroke="currentColor"
168+
width="20"
169+
height="20"
170+
>
171+
<path
172+
strokeLinecap="round"
173+
strokeLinejoin="round"
174+
strokeWidth="2"
175+
d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"
176+
/>
177+
</svg>
178+
</button>
137179
</div>
138180
</Fragment>
139181
);

packages/autocomplete-plugin-algolia-insights/src/createAlgoliaInsightsPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createViewedEvents } from './createViewedEvents';
1010
import { isAlgoliaInsightsHit } from './isAlgoliaInsightsHit';
1111
import {
1212
AlgoliaInsightsHit,
13-
InsightsApi,
13+
AutocompleteInsightsApi,
1414
InsightsClient,
1515
OnActiveParams,
1616
OnItemsChangeParams,
@@ -22,7 +22,7 @@ const VIEW_EVENT_DELAY = 400;
2222
type SendViewedObjectIDsParams = {
2323
onItemsChange(params: OnItemsChangeParams): void;
2424
items: AlgoliaInsightsHit[];
25-
insights: InsightsApi;
25+
insights: AutocompleteInsightsApi;
2626
state: AutocompleteState<any>;
2727
};
2828

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { InsightsApi } from './types';
1+
import { AutocompleteInsightsApi } from './types';
22

33
declare module '@algolia/autocomplete-core' {
44
export interface AutocompleteContext {
55
algoliaInsightsPlugin: {
6-
insights: InsightsApi;
6+
insights: AutocompleteInsightsApi;
77
};
88
}
99
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export type { AutocompleteInsightsApi } from './types/AutocompleteInsightsApi';
12
export * from './createAlgoliaInsightsPlugin';

packages/autocomplete-plugin-algolia-insights/src/types/InsightsApi.ts renamed to packages/autocomplete-plugin-algolia-insights/src/types/AutocompleteInsightsApi.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { createSearchInsightsApi } from '../createSearchInsightsApi';
22

3-
export type InsightsApi = ReturnType<typeof createSearchInsightsApi>;
3+
export type AutocompleteInsightsApi = ReturnType<
4+
typeof createSearchInsightsApi
5+
>;
46

57
export type ClickedObjectIDsAfterSearchParams = {
68
eventName: string;

packages/autocomplete-plugin-algolia-insights/src/types/EventParams.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { AutocompleteState } from '@algolia/autocomplete-core';
33
import {
44
ClickedObjectIDsAfterSearchParams,
55
ViewedObjectIDsParams,
6-
} from './InsightsApi';
6+
} from './AutocompleteInsightsApi';
77

8-
import { AlgoliaInsightsHit, InsightsApi } from '.';
8+
import { AlgoliaInsightsHit, AutocompleteInsightsApi } from '.';
99

1010
export type OnSelectParams = {
11-
insights: InsightsApi;
11+
insights: AutocompleteInsightsApi;
1212
insightsEvents: ClickedObjectIDsAfterSearchParams[];
1313
item: AlgoliaInsightsHit;
1414
state: AutocompleteState<any>;
@@ -18,7 +18,7 @@ export type OnSelectParams = {
1818
export type OnActiveParams = OnSelectParams;
1919

2020
export type OnItemsChangeParams = {
21-
insights: InsightsApi;
21+
insights: AutocompleteInsightsApi;
2222
insightsEvents: ViewedObjectIDsParams[];
2323
state: AutocompleteState<any>;
2424
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from './AlgoliaInsightsHit';
2-
export * from './InsightsApi';
3-
export * from './InsightsClient';
2+
export * from './AutocompleteInsightsApi';
43
export * from './EventParams';
4+
export * from './InsightsClient';

0 commit comments

Comments
 (0)