In some situations, especially in B2B accounts, you may want to present custom search results for each of your customers. To this end, this tutorial will guide you on how to segment the search result page of your store.
If you are using the B2B Suite solution, the configuration described in this guide is not necessary, because the B2B Organizations app allows product collections to be assigned to organizations.
Take the following example in which different results for the same search are obtained based on the customer's email.
Step by step
To create segmented search results, we'll create a new VTEX IO app from the vtex.search-segment-resolver
boilerplate and customize it to establish our own segmentation rules.
- Clone the
vtex.search-segment-resolver
app into your machine:
_10git clone https://github.com/vtex-apps/search-segment-resolver
-
Open the
search-segment-resolver
project in any code editor of your preference. -
Go to the
manifest.json
file and replace thevendor
value with the name of your VTEX account. -
Go to the
node/resolvers
folder and open thesearchSegment.ts
file.The
segmentSearch
function is responsible for providing a JSON array of facets. For example, if you want to segment the search by theshoes
category, thesegmentSearch
functio returns[{"key": "category-1", "value": "shoes"}]
. -
Replace the
searchSegment
definition with your own segmentation rules. Take the following example in which we segmented the search result to filter by the123
collection forvtex.com.br
emails and by the456
collection otherwise:
_10export const queries = {_10 searchSegment: async (ctx: any, args: SearchSegmentInput, __: Context) => {_10 const userEmail = args.userEmail_10 const domain = userEmail.split('@')[1]_10_10 return domain === 'vtex.com.br' ? [{ key: 'productClusterIds', value: '123' }] : [{ key: 'productClusterIds', value: '456' }]_10 },_10}
Notice that the searchSegment
function receives the args
variable, which has the SearchSegmentInput
type:
_10interface SearchSegmentInput {_10 // User email_10 userEmail?: string_10 // Whether the user is authenticated or not._10 isAuthenticated?: boolean_10 // Array of selected facets (optionally you can control it by the session itself)_10 selectedFacets?: SelectedFacet[]_10}
- Create a development workspace and link your app to test if your segmentation rules are working as expected.
- Once you finish your tests, follow all the necessary steps to make your app publicly available before promoting it to master.