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

fix(insights): send extra parameters only for applicable versions #5558

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('insights', () => {
expect(document.body).toMatchInlineSnapshot(`
<body>
<script
src="https://cdn.jsdelivr.net/npm/search-insights@2.3.0/dist/search-insights.min.js"
src="https://cdn.jsdelivr.net/npm/search-insights@2.4.0/dist/search-insights.min.js"
/>
</body>
`);
Expand Down Expand Up @@ -220,7 +220,7 @@ describe('insights', () => {
expect(document.body).toMatchInlineSnapshot(`
<body>
<script
src="https://cdn.jsdelivr.net/npm/search-insights@2.3.0/dist/search-insights.min.js"
src="https://cdn.jsdelivr.net/npm/search-insights@2.4.0/dist/search-insights.min.js"
/>
</body>
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

import type {
InsightsClient,
InsightsClientMethod,
InsightsMethodMap,
InternalMiddleware,
Hit,
} from '../types';
Expand All @@ -18,9 +18,20 @@ import type {
PlainSearchParameters,
} from 'algoliasearch-helper';

export type InsightsEvent = {
insightsMethod?: InsightsClientMethod;
payload: any;
// not every method is actually allowed to be called with the insights middleware
export type InsightsMethod =
| 'clickedObjectIDsAfterSearch'
| 'clickedObjectIDs'
| 'clickedFilters'
| 'convertedObjectIDsAfterSearch'
| 'convertedObjectIDs'
| 'convertedFilters'
| 'viewedObjectIDs'
| 'viewedFilters';

export type InsightsEvent<TMethod extends InsightsMethod = InsightsMethod> = {
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
insightsMethod?: TMethod;
payload: InsightsMethodMap[TMethod][0];
widgetType: string;
eventType: string; // 'view' | 'click' | 'conversion', but we're not restricting.
hits?: Hit[];
Expand All @@ -46,8 +57,13 @@ export type InsightsProps<
$$internal?: boolean;
};

const ALGOLIA_INSIGHTS_SRC =
'https://cdn.jsdelivr.net/npm/search-insights@2.3.0/dist/search-insights.min.js';
const ALGOLIA_INSIGHTS_VERSION = '2.4.0';
const ALGOLIA_INSIGHTS_SRC = `https://cdn.jsdelivr.net/npm/search-insights@${ALGOLIA_INSIGHTS_VERSION}/dist/search-insights.min.js`;

export type InsightsClientWithGlobals = InsightsClient & {
shouldAddScript?: boolean;
version?: string;
dhayab marked this conversation as resolved.
Show resolved Hide resolved
};

export type CreateInsightsMiddleware = typeof createInsightsMiddleware;

Expand All @@ -61,8 +77,7 @@ export function createInsightsMiddleware<
$$internal = false,
} = props;

let insightsClient: InsightsClient & { shouldAddScript?: boolean } =
_insightsClient || noop;
let insightsClient: InsightsClientWithGlobals = _insightsClient || noop;

if (_insightsClient !== null && !_insightsClient) {
safelyRunOnBrowser(({ window }: { window: any }) => {
Expand All @@ -81,6 +96,7 @@ export function createInsightsMiddleware<
}
window[pointer].queue.push(args);
};
window[pointer].version = ALGOLIA_INSIGHTS_VERSION;
}

insightsClient = window[pointer];
Expand Down Expand Up @@ -227,15 +243,29 @@ export function createInsightsMiddleware<
immediate: true,
});

// @ts-ignore
const insightsClientWithLocalCredentials = (method, payload) => {
return insightsClient(method, payload, {
headers: {
'X-Algolia-Application-Id': appId,
'X-Algolia-API-Key': apiKey,
},
});
};
type InsightsClientWithLocalCredentials = <
TMethod extends InsightsMethod
>(
method: TMethod,
payload: InsightsMethodMap[TMethod][0]
) => void;

let insightsClientWithLocalCredentials =
insightsClient as InsightsClientWithLocalCredentials;

if (isModernInsightsClient(insightsClient)) {
insightsClientWithLocalCredentials = (method, payload) => {
const extraParams = {
headers: {
'X-Algolia-Application-Id': appId,
'X-Algolia-API-Key': apiKey,
},
};

// @ts-ignore we are calling this only when we know that the client actually is correct
return insightsClient(method, payload, extraParams);
};
}

instantSearchInstance.sendEventToInsights = (event: InsightsEvent) => {
if (onEvent) {
Expand Down Expand Up @@ -280,3 +310,15 @@ See documentation: https://www.algolia.com/doc/guides/building-search-ui/going-f
};
};
}

function isModernInsightsClient(client: InsightsClientWithGlobals): boolean {
const [major, minor] = (client.version || '').split('.').map(Number);

/* eslint-disable @typescript-eslint/naming-convention */
const v3 = major >= 3;
const v2_4 = major === 2 && minor >= 4;
const v1_10 = major === 1 && minor >= 10;
/* eslint-enable @typescript-eslint/naming-convention */

return v3 || v2_4 || v1_10;
}
3 changes: 2 additions & 1 deletion packages/instantsearch.js/src/types/insights.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
InsightsMethodMap,
InsightsMethodMap as _InsightsMethodMap,
InsightsClient as _InsightsClient,
} from 'search-insights';

Expand All @@ -11,6 +11,7 @@ export type {
OnUserTokenChange as InsightsOnUserTokenChange,
} from 'search-insights';

export type InsightsMethodMap = _InsightsMethodMap;
export type InsightsClientMethod = keyof InsightsMethodMap;

export type InsightsClientPayload = {
Expand Down