Skip to content

Commit

Permalink
feat(insights): update default version to support `authenticatedUserT…
Browse files Browse the repository at this point in the history
…oken` (#1225)
  • Loading branch information
dhayab authored Dec 19, 2023
1 parent b7606d5 commit 3e4c180
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@algolia/client-search": "4.16.0",
"algoliasearch": "4.16.0",
"preact": "10.13.2",
"search-insights": "2.6.0"
"search-insights": "2.13.0"
},
"devDependencies": {
"parcel": "2.8.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/query-suggestions-with-hits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@algolia/client-search": "4.16.0",
"algoliasearch": "4.16.0",
"preact": "10.13.2",
"search-insights": "2.6.0"
"search-insights": "2.13.0"
},
"devDependencies": {
"parcel": "2.8.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/reshape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"algoliasearch": "4.16.0",
"preact": "10.13.2",
"ramda": "0.27.1",
"search-insights": "2.6.0"
"search-insights": "2.13.0"
},
"devDependencies": {
"@algolia/autocomplete-core": "1.12.2",
Expand Down
2 changes: 1 addition & 1 deletion examples/tags-in-searchbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"algoliasearch": "4.16.0",
"preact": "10.13.2",
"ramda": "0.27.1",
"search-insights": "2.6.0"
"search-insights": "2.13.0"
},
"devDependencies": {
"parcel": "2.8.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/tags-with-hits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"algoliasearch": "4.16.0",
"preact": "10.13.2",
"ramda": "0.27.1",
"search-insights": "2.6.0"
"search-insights": "2.13.0"
},
"devDependencies": {
"parcel": "2.8.3"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"rollup-plugin-license": "2.9.1",
"rollup-plugin-terser": "7.0.2",
"shipjs": "0.26.1",
"search-insights": "2.6.0",
"search-insights": "2.13.0",
"start-server-and-test": "1.15.2",
"stylelint": "13.13.1",
"stylelint-a11y": "1.2.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('createAlgoliaInsightsPlugin', () => {
expect(document.body).toMatchInlineSnapshot(`
<body>
<script
src="https://cdn.jsdelivr.net/npm/search-insights@2.6.0/dist/search-insights.min.js"
src="https://cdn.jsdelivr.net/npm/search-insights@2.13.0/dist/search-insights.min.js"
/>
<form>
<input />
Expand All @@ -306,7 +306,7 @@ describe('createAlgoliaInsightsPlugin', () => {
`);
expect((window as any).AlgoliaAnalyticsObject).toBe('aa');
expect((window as any).aa).toEqual(expect.any(Function));
expect((window as any).aa.version).toBe('2.6.0');
expect((window as any).aa.version).toBe('2.13.0');
});

it('notifies when the script fails to be added', () => {
Expand Down Expand Up @@ -867,7 +867,7 @@ describe('createAlgoliaInsightsPlugin', () => {
test('sends a `clickedObjectIDsAfterSearch` event with additional parameters if client supports it', async () => {
const insightsClient = jest.fn();
// @ts-ignore
insightsClient.version = '2.6.0';
insightsClient.version = '2.13.0';
const insightsPlugin = createAlgoliaInsightsPlugin({ insightsClient });

const { inputElement } = createPlayground(createAutocomplete, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from './types';

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

type SendViewedObjectIDsParams = {
Expand Down Expand Up @@ -170,14 +170,16 @@ export function createAlgoliaInsightsPlugin(
return {
name: 'aa.algoliaInsightsPlugin',
subscribe({ setContext, onSelect, onActive }) {
function setInsightsContext(userToken?: string) {
function setInsightsContext(userToken?: string | number) {
setContext({
algoliaInsightsPlugin: {
__algoliaSearchParameters: {
...(__autocomplete_clickAnalytics
? { clickAnalytics: true }
: {}),
...(userToken ? { userToken } : {}),
...(userToken
? { userToken: normalizeUserToken(userToken) }
: {}),
},
insights,
},
Expand Down Expand Up @@ -304,3 +306,11 @@ function loadInsights(environment: typeof window) {
console.error(errorMessage);
}
}

/**
* While `search-insights` supports both string and number user tokens,
* the Search API only accepts strings. This function normalizes the user token.
*/
function normalizeUserToken(userToken: string | number): string {
return typeof userToken === 'number' ? userToken.toString() : userToken;
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17380,10 +17380,10 @@ schema-utils@^3.0.0, schema-utils@^3.1.1:
ajv "^6.12.5"
ajv-keywords "^3.5.2"

search-insights@2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.6.0.tgz#bb8771a73b83c4a0f1f207c2f64fea01acd3e7d0"
integrity sha512-vU2/fJ+h/Mkm/DJOe+EaM5cafJv/1rRTZpGJTuFPf/Q5LjzgMDsqPdSaZsAe+GAWHHsfsu+rQSAn6c8IGtBEVw==
search-insights@2.13.0:
version "2.13.0"
resolved "https://registry.yarnpkg.com/search-insights/-/search-insights-2.13.0.tgz#a79fdcf4b5dad2fba8975b06f2ebc37a865032b7"
integrity sha512-Orrsjf9trHHxFRuo9/rzm0KIWmgzE8RMlZMzuhZOJ01Rnz3D0YBAe+V6473t6/H6c7irs6Lt48brULAiRWb3Vw==

search-insights@^2.1.0:
version "2.2.1"
Expand Down

0 comments on commit 3e4c180

Please sign in to comment.