insights
import { createInsightsMiddleware } from 'instantsearch.js/es/middlewares'; createInsightsMiddleware({ insightsClient: null | InsightsClient, insightsInitParams?: object, onEvent?: (event: InsightsEvent, aa: null | InsightsClient) => void, });
About this widget
The createInsightsMiddleware
creates an insights middleware to help you achieve the following:
- Set the
userToken
for insights purposes (Click Analytics, Personalization, etc.) - Automatically send events from built-in widgets. You can turn this off if needed
- Send events from your own custom widgets
Requirements
- Use
search-insights
v1.6.2 or later.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { createInsightsMiddleware } from 'instantsearch.js/es/middlewares';
import { useConnector } from 'react-instantsearch-hooks-web';
import { useEffect } from 'react';
const connectMiddleware = (renderFn, unmountFn) => (widgetParams) => ({
init(initOptions) {
renderFn(
{
...this.getWidgetRenderState(initOptions),
instantSearchInstance: initOptions.instantSearchInstance,
},
true
);
},
render(renderOptions) {
const renderState = this.getWidgetRenderState(renderOptions);
renderFn(
{
...renderState,
instantSearchInstance: renderOptions.instantSearchInstance,
},
false
);
},
getWidgetRenderState(renderOptions) {
return {
use: renderOptions.instantSearchInstance.use,
unuse: renderOptions.instantSearchInstance.unuse,
widgetParams,
};
},
dispose() {
unmountFn();
},
});
function Insights() {
const { use, unuse } = useConnector(connectMiddleware);
useEffect(() => {
const middleware = createInsightsMiddleware({
insightsClient,
insightsInitParams,
onEvent,
});
use(middleware);
return () => unuse(middleware);
}, []);
return null;
}
Options
insightsClient
|
type: null|InsightsClient
Required
The middleware leverages |
||
Copy
|
|||
insightsInitParams
|
type: object
Optional
When given, the With With To learn more about the parameters of |
||
Copy
|
|||
onEvent
|
type: (event: InsightsEvent, aa: null | InsightsClient) => void
default: undefined
Optional
By default, the middleware sends events to Algolia using the provided If you want to use The
|
||
Copy
|
Custom events
Connectors
|
Many of the InstantSearch connectors expose the Here’s a list of connectors that expose |
||
Copy
|