Saturday 15 May 2021

Coveo Custom Popular Searches Rendering

 In a recent Coveo implantation I worked on, our client asked for a home page rendering that should the most popular searches, so we need to get the common keywords that have been used in our global search page, and when user click on these words, the client will be redirected to the global search results page with the result pre-filtered using the clicked keyword, in this post I'll show you how I did that and please comment or email me if you have a better way of doing it. 




As you probably know, Coveo for Sitecore track user activities like searches the users perform in your search page, all what you need to do is to add tracking/analytics Coveo renderings like, [Coveo for Sitecore Analytics] and [Coveo User Context] renderings. 

Once you have these added to you search page, you can check the OOTB analytics reports in Coveo or create your own reports based on the existing OTTB templates and you can see the most common/popular keywords that have been used in your page, see the following screenshot:


 
while investigating how we can get these popular keywords from Coveo I noticed if the query suggestion feature when using global search box, the box will display the most common keywords and start the suggestion/auto complete based on these words, if you check the Network tab, you can see that there is an API call to [coveo/rest/search/v2/querySuggest] all what you need to do an ajax call to this API and pass the correct search hub value and you will get the related query suggestions / popular keywords. 

If you're not familiar with Search Hub, following is the definition from Coveo documentation:

A search hub is a Search API query parameter whose value is a descriptive name of the search interface from which the query originates, allowing the creation of a query pipeline optimized for a specific search interface. It also makes it possible to create Usage Analytics Reports in which data is filtered or grouped according to this information.

As example each search page you have in your site can have a different search hub value (string value) which you can use as filter in different places like the query pipeline and machine learning models. 


Now, here is an example of a JS code that will call the Coveo API and get the auto suggestions and render it to an HTML element:



The last piece I want to explain here, is the render of the keywords as anchor, then a small piece of JS code that will redirect the user to the search page with parameter [q] as the user search keyword, see the following example:



Hope this helps someone, please feel free to comment or send me an email if you have any questions or another approach to accomplish this. 


No comments:

Post a Comment