Storefront Development

Storefront Development
Store FrameworkB2B setup
Segmenting the search result

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.

{"base64":"  ","img":{"width":1024,"height":576,"type":"gif","mime":"image/gif","wUnits":"px","hUnits":"px","length":3271379,"url":""}}

Step by step

To create segmented search results, we'll create a new VTEX IO app from the boilerplate and customize it to establish our own segmentation rules.

  1. Clone the app into your machine:
git clone
  1. Open the search-segment-resolver project in any code editor of your preference.

  2. Go to the manifest.json file and replace the vendor value with the name of your VTEX account.

  3. Go to the node/resolvers folder and open the searchSegment.ts file.

    The segmentSearch function is responsible for providing a JSON array of facets. For example, if you want to segment the search by the shoes category, the segmentSearch functio returns [{"key": "category-1", "value": "shoes"}].

  4. Replace the searchSegment definition with your own segmentation rules. Take the following example in which we segmented the search result to filter by the 123collection for emails and by the 456 collection otherwise:

export const queries = { searchSegment: async (ctx: any, args: SearchSegmentInput, __: Context) => { const userEmail = args.userEmail const domain = userEmail.split('@')[1] return domain === '' ? [{ key: 'productClusterIds', value: '123' }] : [{ key: 'productClusterIds', value: '456' }] }, }

Notice that the searchSegment function receives the args variable, which has the SearchSegmentInput type:

interface SearchSegmentInput { // User email userEmail?: string // Whether the user is authenticated or not. isAuthenticated?: boolean // Array of selected facets (optionally you can control it by the session itself) selectedFacets?: SelectedFacet[] }
  1. Create a development workspace and link your app to test if your segmentation rules are working as expected.
  2. Once you finish your tests, follow all the necessary steps to make your app publicly available before promoting it to master.
Photo of the contributor
Photo of the contributor
+ 2 contributors
Was this helpful?
Suggest edits (Github)
B2B setup
« Previous
Access Control List
Next »
Photo of the contributor
Photo of the contributor
+ 2 contributors
On this page