Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(insights): accept initParams for insightsClient #4608

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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