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): implement sendEvent in all the connectors (3/4) #4463

Merged
merged 22 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
97bb81a
update infiniteHits
eunjae-lee Jul 30, 2020
24a1232
modify createSendEventForHits to accept index instead of helper
eunjae-lee Jul 30, 2020
7e0faab
add sendEvent to connectAutocomplete
eunjae-lee Jul 30, 2020
039ff78
add sendEvent to connectGeoSearch
eunjae-lee Jul 30, 2020
56d20d3
add sendEvent to connectHierarchicalMenu
eunjae-lee Jul 30, 2020
f90b567
add sendEvent to connectBreadcrumb
eunjae-lee Jul 30, 2020
a290813
add sendEvent to connectMenu
eunjae-lee Jul 30, 2020
01870c5
add sendEvent to connectNumericMenu
eunjae-lee Jul 30, 2020
bec6aaf
extract getRefinedState in connectRange
eunjae-lee Jul 30, 2020
fe0b87c
add sendEvent to connectRange
eunjae-lee Jul 30, 2020
057ac6e
add sendEvent to connectRatingMenu
eunjae-lee Jul 31, 2020
70b1cc0
add sendEvent to connectToggleRefinement
eunjae-lee Jul 31, 2020
972409d
fix test error
eunjae-lee Jul 31, 2020
c97960e
fix export
eunjae-lee Jul 31, 2020
45665b3
fix type error
eunjae-lee Jul 31, 2020
30788e2
Merge branch 'feat/send-events' into feat/send-events-2
eunjae-lee Aug 24, 2020
5cf4020
use $$type instead of hard-code
eunjae-lee Aug 25, 2020
1ad06dd
remove sendEvent from connectBreadcrumb
eunjae-lee Aug 25, 2020
ce4f5f0
moved createSendEvent to the top level in the files
eunjae-lee Aug 25, 2020
ef2d7df
Merge branch 'feat/send-events' into feat/send-events-2
eunjae-lee Aug 27, 2020
5b89264
Merge branch 'feat/send-events' into feat/send-events-2
eunjae-lee Aug 31, 2020
27f3125
feat(insights): add tests (4/4) (#4464)
Sep 2, 2020
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
5 changes: 5 additions & 0 deletions src/components/InfiniteHits/InfiniteHits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Template from '../Template/Template';
import { SearchResults } from 'algoliasearch-helper';
import { Hits } from '../../types';
import { InfiniteHitsTemplates } from '../../widgets/infinite-hits/infinite-hits';
import { SendEventForHits, BindEventForHits } from '../../lib/utils';

type InfiniteHitsCSSClasses = {
root: string;
Expand All @@ -31,11 +32,14 @@ type InfiniteHitsProps = {
};
isFirstPage: boolean;
isLastPage: boolean;
sendEvent: SendEventForHits;
bindEvent: BindEventForHits;
};

const InfiniteHits = ({
results,
hits,
bindEvent,
hasShowPrevious,
showPrevious,
showMore,
Expand Down Expand Up @@ -86,6 +90,7 @@ const InfiniteHits = ({
...hit,
__hitIndex: position,
}}
bindEvent={bindEvent}
/>
))}
</ol>
Expand Down
15 changes: 15 additions & 0 deletions src/components/InfiniteHits/__tests__/InfiniteHits-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ describe('InfiniteHits', () => {
disabledLoadMore: 'disabledLoadMore',
};

const sendEvent = () => {};
const bindEvent = () => '';

describe('markup', () => {
it('should render <InfiniteHits /> on first page', () => {
const hits: Hits = [
Expand Down Expand Up @@ -50,6 +53,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down Expand Up @@ -88,6 +93,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down Expand Up @@ -115,6 +122,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down Expand Up @@ -142,6 +151,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down Expand Up @@ -180,6 +191,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down Expand Up @@ -223,6 +236,8 @@ describe('InfiniteHits', () => {
},
},
cssClasses,
sendEvent,
bindEvent,
};

const { container } = render(<InfiniteHits {...props} />);
Expand Down
16 changes: 16 additions & 0 deletions src/connectors/autocomplete/connectAutocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import escapeHits, { TAG_PLACEHOLDER } from '../../lib/escape-highlight';
import {
checkRendering,
createDocumentationMessageGenerator,
createSendEventForHits,
SendEventForHits,
noop,
warning,
} from '../../lib/utils';
Expand Down Expand Up @@ -46,6 +48,11 @@ export type AutocompleteRendererOptions = {
* The full results object from the Algolia API.
*/
results: SearchResults;

/**
* Send event to insights middleware
*/
sendEvent: SendEventForHits;
}>;

/**
Expand Down Expand Up @@ -127,11 +134,20 @@ search.addWidgets([
? escapeHits(scopedResult.results.hits)
: scopedResult.results.hits;

const sendEvent = createSendEventForHits({
instantSearchInstance,
index: scopedResult.results.index,
widgetType: 'ais.autocomplete',
});

sendEvent('view', scopedResult.results.hits);

return {
indexId: scopedResult.indexId,
indexName: scopedResult.results.index,
hits: scopedResult.results.hits,
results: scopedResult.results,
sendEvent,
};
});

Expand Down
19 changes: 19 additions & 0 deletions src/connectors/breadcrumb/connectBreadcrumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
checkRendering,
warning,
createDocumentationMessageGenerator,
createSendEventForFacet,
SendEventForFacet,
isEqual,
noop,
} from '../../lib/utils';
Expand Down Expand Up @@ -69,6 +71,11 @@ export type BreadcrumbRendererOptions = {
* True if refinement can be applied.
*/
canRefine: boolean;

/**
* Send event to insights middleware
*/
sendEvent: SendEventForFacet;
};

export type BreadcrumbConnector = Connector<
Expand Down Expand Up @@ -104,11 +111,19 @@ const connectBreadcrumb: BreadcrumbConnector = function connectBreadcrumb(
}

const [hierarchicalFacetName] = attributes;
let sendEvent;

return {
$$type: 'ais.breadcrumb',

init({ createURL, helper, instantSearchInstance }) {
sendEvent = createSendEventForFacet({
instantSearchInstance,
helper,
attribute: hierarchicalFacetName,
widgetType: 'ais.breadcrumb',
});

connectorState.createURL = facetValue => {
if (!facetValue) {
const breadcrumb = helper.getHierarchicalFacetBreadcrumb(
Expand Down Expand Up @@ -137,11 +152,13 @@ const connectBreadcrumb: BreadcrumbConnector = function connectBreadcrumb(
hierarchicalFacetName
);
if (breadcrumb.length > 0) {
sendEvent('click', breadcrumb[0]);
helper
.toggleRefinement(hierarchicalFacetName, breadcrumb[0])
.search();
}
} else {
sendEvent('click', facetValue);
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
helper.toggleRefinement(hierarchicalFacetName, facetValue).search();
}
};
Expand All @@ -153,6 +170,7 @@ const connectBreadcrumb: BreadcrumbConnector = function connectBreadcrumb(
instantSearchInstance,
items: [],
refine: connectorState.refine,
sendEvent,
widgetParams,
},
true
Expand All @@ -176,6 +194,7 @@ const connectBreadcrumb: BreadcrumbConnector = function connectBreadcrumb(
instantSearchInstance,
items,
refine: connectorState.refine,
sendEvent,
widgetParams,
},
false
Expand Down
Loading