Skip to content

Commit 0a0ae2b

Browse files
author
Eunjae Lee
authored
feat(insights): accept initParams for insightsClient (#4608)
1 parent 7502744 commit 0a0ae2b

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/middlewares/__tests__/createInsightsMiddleware.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ describe('insights', () => {
104104
});
105105
});
106106

107+
it('passes initParams to insightsClient', () => {
108+
const { insightsClient, instantSearchInstance } = createTestEnvironment();
109+
createInsightsMiddleware({
110+
insightsClient,
111+
insightsInitParams: {
112+
useCookie: false,
113+
region: 'de',
114+
},
115+
})({ instantSearchInstance });
116+
117+
expect(insightsClient).toHaveBeenLastCalledWith('init', {
118+
apiKey: 'myApiKey',
119+
appId: 'myAppId',
120+
region: 'de',
121+
useCookie: false,
122+
});
123+
});
124+
107125
it('does not throw when an event is sent right after the creation in UMD', () => {
108126
const algoliaAnalytics = createAlgoliaAnalytics();
109127
const {

src/middlewares/createInsightsMiddleware.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export type InsightsEvent = {
1111

1212
export type InsightsProps = {
1313
insightsClient: null | InsightsClient;
14+
insightsInitParams?: {
15+
userHasOptedOut?: boolean;
16+
useCookie?: boolean;
17+
cookieDuration?: number;
18+
region?: 'de' | 'us';
19+
};
1420
onEvent?: (
1521
event: InsightsEvent,
1622
insightsClient: null | InsightsClient
@@ -20,7 +26,8 @@ export type InsightsProps = {
2026
export type CreateInsightsMiddleware = (props: InsightsProps) => Middleware;
2127

2228
export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
23-
const { insightsClient: _insightsClient, onEvent } = props || {};
29+
const { insightsClient: _insightsClient, insightsInitParams, onEvent } =
30+
props || {};
2431
if (_insightsClient !== null && !_insightsClient) {
2532
if (__DEV__) {
2633
throw new Error(
@@ -67,7 +74,7 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
6774
// Otherwise, the `init` call might override it with anonymous user token.
6875
userTokenBeforeInit = userToken;
6976
});
70-
insightsClient('init', { appId, apiKey });
77+
insightsClient('init', { appId, apiKey, ...insightsInitParams });
7178

7279
return {
7380
onStateChange() {},

0 commit comments

Comments
 (0)