Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
feat(docsearch): track docsearch-react UA
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Jul 9, 2020
1 parent eb400f2 commit 2c280e2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
4 changes: 1 addition & 3 deletions packages/autocomplete-preset-algolia/src/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ interface GetAlgoliaSourceParams {

function getAlgoliaSource({ searchClient, queries }: GetAlgoliaSourceParams) {
if (typeof (searchClient as Client).addAlgoliaAgent === 'function') {
(searchClient as Client).addAlgoliaAgent(
`autocomplete-core (${__VERSION__})`
);
(searchClient as Client).addAlgoliaAgent('autocomplete-core', __VERSION__);
}

return searchClient.search(
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-react/src/getAlgoliaHits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface GetAlgoliaSourceParams {

export function getAlgoliaHits(params: GetAlgoliaSourceParams) {
if (typeof params.searchClient.addAlgoliaAgent === 'function') {
params.searchClient.addAlgoliaAgent(`autocomplete-react (${__VERSION__})`);
params.searchClient.addAlgoliaAgent('autocomplete-react', __VERSION__);
}

return originalGetAlgoliaHits(params);
Expand Down
2 changes: 1 addition & 1 deletion packages/autocomplete-react/src/getAlgoliaResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface GetAlgoliaSourceParams {

export function getAlgoliaResults(params: GetAlgoliaSourceParams) {
if (typeof params.searchClient.addAlgoliaAgent === 'function') {
params.searchClient.addAlgoliaAgent(`autocomplete-react (${__VERSION__})`);
params.searchClient.addAlgoliaAgent('autocomplete-react', __VERSION__);
}

return originalGetAlgoliaResults(params);
Expand Down
2 changes: 1 addition & 1 deletion packages/docsearch-js/src/docsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function docsearch(props) {
<DocSearch
{...props}
transformSearchClient={(searchClient) => {
searchClient.addAlgoliaAgent(`docsearch.js (${version})`);
searchClient.addAlgoliaAgent('docsearch.js', version);

return props.transformSearchClient
? props.transformSearchClient(searchClient)
Expand Down
13 changes: 12 additions & 1 deletion packages/docsearch-react/src/useSearchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ export function useSearchClient(
): SearchClient {
const searchClient = React.useMemo(() => {
const client = algoliasearch(appId, apiKey);
client.addAlgoliaAgent(`docsearch (${version})`);
client.addAlgoliaAgent('docsearch', version);

// Since DocSearch.js relies on DocSearch React with an alias to Preact,
// we cannot add the `docsearch-react` user agent by default, otherwise
// it would also be sent on a DocSearch.js integration.
// We therefore only add the `docsearch-react` user agent if `docsearch.js`
// is not present.
if (
/docsearch.js \(.*\)/.test(client.transporter.userAgent.value) === false
) {
client.addAlgoliaAgent('docsearch-react', version);
}

return transformSearchClient(client);
}, [appId, apiKey, transformSearchClient]);
Expand Down
9 changes: 3 additions & 6 deletions packages/website/src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { useHistory } from '@docusaurus/router';
import Link from '@docusaurus/Link';
import Head from '@docusaurus/Head';
import {
DocSearchButton,
useDocSearchKeyboardEvents,
version as docSearchVersion,
} from '@docsearch/react';
import { DocSearchButton, useDocSearchKeyboardEvents } from '@docsearch/react';

let DocSearchModal = null;

Expand Down Expand Up @@ -42,7 +38,8 @@ function transformItems(items) {
}

function transformSearchClient(searchClient) {
searchClient.addAlgoliaAgent(`docsearch-docusaurus (${docSearchVersion})`);
// @TODO: attach Docusaurus version to user agent
searchClient.addAlgoliaAgent('docusaurus');

return searchClient;
}
Expand Down

0 comments on commit 2c280e2

Please sign in to comment.