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 (api): Adds ability for click insights events to be triggered #1916

Merged
merged 8 commits into from Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bundlesize.config.json
Expand Up @@ -6,11 +6,11 @@
},
{
"path": "packages/docsearch-react/dist/umd/index.js",
"maxSize": "22.47 kB"
"maxSize": "22.57 kB"
},
{
"path": "packages/docsearch-js/dist/umd/index.js",
"maxSize": "30.33 kB"
"maxSize": "30.43 kB"
}
]
}
25 changes: 23 additions & 2 deletions packages/docsearch-react/src/DocSearchModal.tsx
@@ -1,4 +1,3 @@
import type { AutocompleteState } from '@algolia/autocomplete-core';
import { createAutocomplete } from '@algolia/autocomplete-core';
import React from 'react';

Expand All @@ -14,6 +13,7 @@ import { SearchBox } from './SearchBox';
import { createStoredSearches } from './stored-searches';
import type {
DocSearchHit,
DocSearchState,
InternalDocSearchHit,
StoredDocSearchHit,
} from './types';
Expand All @@ -27,6 +27,7 @@ import {
removeHighlightTags,
isModifierEvent,
} from './utils';
import { buildInsightsClickParams } from './utils/buildInsightsClickParams';

export type ModalTranslations = Partial<{
searchBox: SearchBoxTranslations;
Expand Down Expand Up @@ -66,7 +67,7 @@ export function DocSearchModal({
...screenStateTranslations
} = translations;
const [state, setState] = React.useState<
AutocompleteState<InternalDocSearchHit>
DocSearchState<InternalDocSearchHit>
>({
query: '',
collections: [],
Expand Down Expand Up @@ -130,6 +131,23 @@ export function DocSearchModal({
[favoriteSearches, recentSearches, disableUserPersonalization]
);

const sendItemClickEvent = React.useCallback(
(item: InternalDocSearchHit) => {
if (!state.context.algoliaInsightsPlugin || !item.__autocomplete_id)
return;

const insightsClickParams = buildInsightsClickParams(
item,
item.__autocomplete_id
);

state.context.algoliaInsightsPlugin.insights.clickedObjectIDsAfterSearch(
insightsClickParams
);
},
[state]
8bittitan marked this conversation as resolved.
Show resolved Hide resolved
);

const autocomplete = React.useMemo(
() =>
createAutocomplete<
Expand Down Expand Up @@ -476,6 +494,9 @@ export function DocSearchModal({
translations={screenStateTranslations}
getMissingResultsUrl={getMissingResultsUrl}
onItemClick={(item, event) => {
// If insights is active, send insights click event
sendItemClickEvent(item);

saveRecentSearch(item);
if (!isModifierEvent(event)) {
onClose();
Expand Down
6 changes: 5 additions & 1 deletion packages/docsearch-react/src/Hit.tsx
Expand Up @@ -8,5 +8,9 @@ interface HitProps {
}

export function Hit({ hit, children }: HitProps) {
return <a href={hit.url}>{children}</a>;
return (
<a href={hit.url} onClick={(e) => e.preventDefault()}>
8bittitan marked this conversation as resolved.
Show resolved Hide resolved
8bittitan marked this conversation as resolved.
Show resolved Hide resolved
{children}
</a>
);
}
1 change: 1 addition & 0 deletions packages/docsearch-react/src/types/DocSearchHit.ts
Expand Up @@ -84,4 +84,5 @@ export declare type DocSearchHit = {
appId: string;
apiKey: string;
};
__autocomplete_id?: number;
};
17 changes: 17 additions & 0 deletions packages/docsearch-react/src/types/DocSearchState.ts
@@ -0,0 +1,17 @@
import type {
AutocompleteContext,
AutocompleteInsightsApi,
AutocompleteState,
BaseItem,
} from '@algolia/autocomplete-core';

interface DocSearchContext extends AutocompleteContext {
algoliaInsightsPlugin?: {
insights: AutocompleteInsightsApi;
};
}

export interface DocSearchState<TItem extends BaseItem>
extends AutocompleteState<TItem> {
context: DocSearchContext;
}
1 change: 1 addition & 0 deletions packages/docsearch-react/src/types/index.ts
@@ -1,3 +1,4 @@
export * from './DocSearchHit';
export * from './DocSearchState';
export * from './InternalDocSearchHit';
export * from './StoredDocSearchHit';
22 changes: 22 additions & 0 deletions packages/docsearch-react/src/utils/buildInsightsClickParams.ts
@@ -0,0 +1,22 @@
import type { AlgoliaInsightsHit } from '@algolia/autocomplete-core';
import type {
InsightsParamsWithItems,
ClickedObjectIDsAfterSearchParams,
} from '@algolia/autocomplete-plugin-algolia-insights';

import type { InternalDocSearchHit } from '../types';

export function buildInsightsClickParams(
8bittitan marked this conversation as resolved.
Show resolved Hide resolved
item: InternalDocSearchHit,
itemPosition: number
): InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams> {
const insightsItem = item as AlgoliaInsightsHit;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we run into issues with this assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, if we have an insights plugin active the needed attributes are added to the item. All AlgoliaInsightsHit type adds is better typings for the __autocomplete_* attributes.


return {
eventName: 'Item Selected',
index: insightsItem.__autocomplete_indexName,
items: [insightsItem],
positions: [itemPosition],
queryID: insightsItem.__autocomplete_queryID,
};
}