Sometimes you might want to trigger an event every time a user selects a facet filter, for example if you want to track this event in your own analytics tool.
You can get all of the applied filters for a search with the render event from your search:
/** * Will run every time a search is run you can use this function * to capture search data */ search.on('render', () => { // Get the search helper (docs are here https://community.algolia.com/algoliasearch-helper-js/reference.html) let helper = search.helper; // Get the query used for the last search console.log('query used'); console.log(helper.lastResults.query); // Get the number of results for the last search console.log('number of results'); console.log(helper.lastResults.nbHits); // Get the refinements used in the last search results console.log('refinements used'); console.log(helper.lastResults.getRefinements()); // Get the current refinement on brand console.log('refinements currently set for Brand'); console.log(helper.getRefinements('brand')); });
You can then use this to work out which filters have been applied, as well as access other useful information about the search.
Here is an example Codesandbox that demonstrates this: https://codesandbox.io/s/on-search-event-53tyx
You can also get the selected refinement when it is selected using the transformItems event from the RefinementList widget: https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/#widget-param-transformitems
transformItems(items) { items.forEach(element => { // print out all the selected facets when a facet is clicked if (element.isRefined) { console.log("this element is selected:") console.log(element); } }); return items; },
Here is an example Codesandbox that demonstrates this: https://codesandbox.io/s/get-selected-facets-when-a-facet-is-selected-vwrog?file=/src/app.js:691-988