Skip to content

Commit

Permalink
feat(insights): accept initParams for insightsClient
Browse files Browse the repository at this point in the history
  • Loading branch information
eunjae-lee committed Dec 15, 2020
1 parent 6550885 commit c152d2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/middlewares/__tests__/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ describe('insights', () => {
});
});

it('passes initParams to insightsClient', () => {
const { insightsClient, instantSearchInstance } = createTestEnvironment();
createInsightsMiddleware({
insightsClient,
insightsInitParams: {
useCookie: false,
region: 'de',
},
})({ instantSearchInstance });

expect(insightsClient).toHaveBeenLastCalledWith('init', {
apiKey: 'myApiKey',
appId: 'myAppId',
region: 'de',
useCookie: false,
});
});

it('does not throw when an event is sent right after the creation in UMD', () => {
const algoliaAnalytics = createAlgoliaAnalytics();
const {
Expand Down
11 changes: 9 additions & 2 deletions src/middlewares/createInsightsMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export type InsightsEvent = {

export type InsightsProps = {
insightsClient: null | InsightsClient;
insightsInitParams?: {
userHasOptedOut?: boolean;
useCookie?: boolean;
cookieDuration?: number;
region?: 'de' | 'us';
};
onEvent?: (
event: InsightsEvent,
insightsClient: null | InsightsClient
Expand All @@ -20,7 +26,8 @@ export type InsightsProps = {
export type CreateInsightsMiddleware = (props: InsightsProps) => Middleware;

export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
const { insightsClient: _insightsClient, onEvent } = props || {};
const { insightsClient: _insightsClient, insightsInitParams, onEvent } =
props || {};
if (_insightsClient !== null && !_insightsClient) {
if (__DEV__) {
throw new Error(
Expand Down Expand Up @@ -67,7 +74,7 @@ export const createInsightsMiddleware: CreateInsightsMiddleware = props => {
// Otherwise, the `init` call might override it with anonymous user token.
userTokenBeforeInit = userToken;
});
insightsClient('init', { appId, apiKey });
insightsClient('init', { appId, apiKey, ...insightsInitParams });

return {
onStateChange() {},
Expand Down

0 comments on commit c152d2c

Please sign in to comment.