Content recommendations

Note

CR models depend on view events and the user’s action history. To display recommendations with Quantic, you must have configured a CR model and logged the required usage analytics events via the PageViewTracker component.

Implementation example

The following Lightning Web Component (LWC) example shows how you can implement a recommendation interface using the quantic-recommendation-interface and quantic-recommendation-list components.

HTML

<template>
    <div class="search__container slds-p-around_medium"
         onregisterresulttemplates={handleResultTemplateRegistration}> 1
        <c-quantic-recommendation-interface 2
            engine-id={engineId}
            pipeline={pipeline}
            search-hub={searchHub}>
            <c-quantic-recommendation-list 3
                engine-id={engineId}
                number-of-recommendations={numberOfRecommendations}
                recommendations-per-row={recommendationsPerRow}
                label={label}
                variant={variant}
                fields-to-include={fieldsToInclude}
                recommendation={recommendation}
            >
            </c-quantic-recommendation-list>
        </c-quantic-recommendation-interface>
    </div>
</template>
1 Optionally, register your own result templates to customize the appearance of your recommendation results, the same way you would in your main search interface.
2 The quantic-recommendation-interface component is the parent of all other Quantic components in a recommendation interface.
3 quantic-recommendation-list displays recommendations by applying the options provided to it.

JavaScript

import {LightningElement, api} from 'lwc';
// @ts-ignore
import caseTemplate from './resultTemplates/caseResultTemplate.html'; 1

export default class ExampleQuanticRecommendationsList extends LightningElement {
  @api engineId = 'quantic-recommendation-list-engine'; 2
  @api pipeline = 'mypipeline'; 3
  @api searchHub = 'mysearchhub';
  @api numberOfRecommendations = 3;
  @api recommendationsPerRow = 1;
  @api label = "Recommended for you";
  @api variant = "grid";
  @api fieldsToInclude = "objecttype,sfstatus,sfcasestatus,sfcasenumber"; 4
  @api recommendation = "recommendation"; 5

  handleResultTemplateRegistration(event) { 6
    event.stopPropagation();

    const resultTemplatesManager = event.detail;

    const isCase = CoveoHeadless.ResultTemplatesHelpers.fieldMustMatch(
      'objecttype',
      ['Case']
    );

    resultTemplatesManager.registerTemplates(
      {
        content: caseTemplate,
        conditions: [isCase],
        fields: ['sfstatus', 'sfcasestatus', 'sfcasenumber'],
      }
    );
  }
}
1 Import your custom result templates, if any.
2 The engineId is the unique identifier of the Headless engine that powers the recommendation interface. We recommend that you set a different engine from the one powering your search interface.
3 The searchHub and pipeline properties are required to correctly make requests to the Coveo Search API and usage analytics Write API. Although we recommend that you enforce these properties through the search token, you should still set them in your component to ensure that it works as expected.
4 The fieldsToInclude property is a comma-separated list of fields to include in the recommendation results. These fields are used to build the result templates and to display the recommendations.
5 The recommendation identifier used by Coveo ML to scope the recommendation interface.
6 Define result templates the same way you would in your main search interface.

XML

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>57.0</apiVersion>
    <isExposed>true</isExposed>
    <targets> 1
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
  </targets>
  <targetConfigs> 2
      <targetConfig targets="lightning__RecordPage, lightning__AppPage, lightning__HomePage, lightningCommunity__Page, lightningCommunity__Default">
          <property name="numberOfRecommendations" type="Integer" default="3" label="Enter the number of recommendations in total"/>
          <property name="recommendationsPerRow" type="Integer" default="1" label="Enter the number of recommendations per row"/>
      </targetConfig>
  </targetConfigs>
</LightningComponentBundle>
1 Set the targets of your LWC.
2 Optionally, expose certain parameters through the Salesforce Experience Builder.

Generate search tokens for your component

We recommend that you create a search token provider dedicated to your recommendation component. Enforce the search hub through this provider to securely ensure that the component always has the required context.

  1. If you’ve installed and configured Coveo for Salesforce v4 or a later package in your Salesforce organization, it’s typically enough to copy the CoveoTokenProvider Apex class, along with its meta.xml file, into your Salesforce DX project. This class can automatically fetch your Salesforce users' information to generate search tokens for them. This is the best option for most use cases.

    If you haven’t installed Coveo for Salesforce v4 or a later package, or if the CoveoTokenProvider Apex class doesn’t fit your needs, you’ll have to implement your own search token provider.

    Tip

    You can find an example in the Quantic project repository.

  2. Once you’ve copied or created your search token provider Apex class, you need to modify the Headless engine configuration used by the recommendation controller so that it calls your Apex class. You can modify the RecommendationsController file at force-app/quantic/classes. Locate the following line:

    return SampleTokenProvider.getHeadlessConfiguration();

    Modify it so that it leverages your search token provider instead:

    return YOUR_RECOMMENDATION_TOKEN_PROVIDER.getHeadlessConfiguration();

    where you replace YOUR_RECOMMENDATION_TOKEN_PROVIDER with the name of the search token provider Apex class that you created for your recommendation interface.

  3. After you deploy your project, your changes will be effective in your Salesforce organization.

Experiment with different variants and dimensions

The quantic-recommendation-list component supports different variants to display recommendations. Experiment with them to judge which one best fits your use case.

grid

The grid variant displays recommendations in a grid layout, with the numberOfRecommendations and number of recommendationsPerRow of your choice.

Examples
  • numberOfRecommendations set to 3 and recommendationsPerRow set to 1

    Quantic recommendations in a narrow grid
  • numberOfRecommendations set to 9 and recommendationsPerRow set to 3

    Quantic recommendations in a wide grid

The carousel variant displays recommendations in a carousel layout, with the numberOfRecommendations and number of recommendationsPerRow of your choice.

Example

numberOfRecommendations set to 9 and recommendationsPerRow set to 3

Quantic recommendations in a carousel