--- title: Integrate a recommendation interface in a web page slug: '1934' canonical_url: https://docs.coveo.com/en/1934/ collection: javascript-search-framework source_format: adoc --- # Integrate a recommendation interface in a web page [jsui-new Coveo JavaScript Search Framework v1.667.24] When you want to take advantage of a [Coveo Machine Learning (Coveo ML)](https://docs.coveo.com/en/188/) [Content Recommendation (CR)](https://docs.coveo.com/en/1016/) [model](https://docs.coveo.com/en/1012/) on a site, you should integrate a [Coveo JavaScript Search Framework](https://docs.coveo.com/en/187/) recommendation interface into each page in which you want to display recommendations (see [Deploy Content Recommendations (CR)](https://docs.coveo.com/en/1886/)).

To integrate a recommendation interface in a web page

* [Import the required JavaScript Search Framework resources](https://docs.coveo.com/en/2075/). * Configure a search endpoint for the recommendation interface (see the [`SearchEndpoint.configureCloudV2Endpoint`](https://coveo.github.io/search-ui/classes/searchendpoint.html#configurecloudv2endpoint) method). * Initialize the recommendation interface (see the [`initRecommendation`](https://coveo.github.io/search-ui/globals.html#initrecommendation) top-level function). * Configure the recommendation interface in the page markup (see the [`Recommendation`](https://coveo.github.io/search-ui/components/recommendation.html) component). The following sample markup shows how to integrate a recommendation interface. ```html
RECOMMENDATION_INTERFACE_TITLE
``` Where: * `` The unique identifier of the target [Coveo organization](https://docs.coveo.com/en/185/) (for example, `mycoveocloudvorganizationg8tp8wu3`). * `` A [public API key](https://docs.coveo.com/en/1718#api-key-templates) or a valid [search token](https://docs.coveo.com/en/1346/) if the page requires user authentication (see [Search token authentication](https://docs.coveo.com/en/56/)). See also [Choose and implement a search authentication method](https://docs.coveo.com/en/1369/). * `` A CSS selector that matches the HTML element with the `CoveoRecommendation` class in the search page markup. For example, if the target element has the `id="recommendation"` attribute, `#recommendation` would be a good selector. * `` The name of the [query pipeline](https://docs.coveo.com/en/180/) to enforce for each [query](https://docs.coveo.com/en/231/) originating from the recommendation interface (for example, `techDocsRecommendations`). This should be set to the name of the [query pipeline which contains the CR model](https://docs.coveo.com/en/1886#Model) that you want to use. > **Note** > > For the sake of simplicity, the above code sample suggests that you [enforce the query pipeline routing](https://docs.coveo.com/en/1666#search-interface-enforced-routing) directly in the markup configuration of your recommendation interface. > > A more flexible approach would be to omit the `data-pipeline` attribute in the `Recommendation` component markup configuration, and instead use the [condition](https://docs.coveo.com/en/2793/)-based [query pipeline routing mechanism](https://docs.coveo.com/en/1666#condition-based-routing-recommended). * `` > **Leading practice** > > Setting the recommendation interface `searchHub` to a distinctive and meaningful value is useful for reporting. The name of the [origin level 1](https://docs.coveo.com/en/1337/) to use when sending [events](https://docs.coveo.com/en/260/) from the recommendation interface. This value also defines the [search hub](https://docs.coveo.com/en/1342/) to use for all queries originating from the recommendation interface. > **Note** > > CR models don't take the `searchHub` query parameter into account. > However, each model can be configured to only learn from search and click events that match certain filters (for example, if the events have `CommunitySearch` as their `originLevel1`). > > UA view events don't have an `originLevel1`, but they can still be filtered according to their [`contentType`](https://docs.coveo.com/en/1744/). * `RECOMMENDATION_INTERFACE_TITLE` The title to display for the recommendation interface (for example, **People Also Viewed**). > **Notes** > > * For a Coveo organization in the HIPAA environment, you would include a third argument when calling the `configureCloudV2Endpoint` to set your search endpoint URI to `+https://searchhipaa.cloud.coveo.com/rest/search+`: > > ```javascript const endpointUri = "https://searchhipaa.cloud.coveo.com/rest/search"; Coveo.SearchEndpoint.configureCloudV2Endpoint(namespace.organizationId, namespace.accessToken, endpointUri); ``` > > You would also set the [`endpoint`](https://coveo.github.io/search-ui/components/analytics.html#options.endpoint) option of your [`Analytics`](https://coveo.github.io/search-ui/components/analytics.html) component to `+https://analyticshipaa.cloud.coveo.com/rest/ua+`: > > ```html
``` > > * If you're already initializing a search endpoint in a web page (for example, for a standalone search box), you don't need to initialize a new endpoint for your recommendation interface. ## Complete JavaScript Search Framework code sample You've configured a CR model in the `techDocRecommendations` query pipeline of the `mycoveocloudvorganizationg8tp8wu3` organization. You include code similar to the following in the pages of your technical documentation website to track users' action history, [send view events](https://docs.coveo.com/en/1818#send-view-events), and render a **People Also Viewed** recommendation interface. ```html Customize the Acme Widget
People Also Viewed
``` ## Request backup recommendations In a [secured search](https://docs.coveo.com/en/1863/) context, the current user may not be permitted to see all of the [items](https://docs.coveo.com/en/210/) which are requested by a CR model. **Example** You create a recommendation interface whose output may include items from a [source that indexes permissions](https://docs.coveo.com/en/1779#same-users-and-groups-as-in-your-content-system). In the markup configuration of your `Recommendation` component, you set the [`data-results-per-page`](https://coveo.github.io/search-ui/components/searchinterface.html#options.resultsperpage) attribute to `5`. Whenever your recommendation interface sends a query, the CR model in the target query pipeline generates a [disjunction query expression (`dq`)](https://docs.coveo.com/en/190/) which requests the five items that it deems the most contextually relevant. However, because the CR model isn't aware of [security identities](https://docs.coveo.com/en/240/) or [effective permissions](https://docs.coveo.com/en/194/), it may request five items which the current user isn't permitted to see. In this case, the [index](https://docs.coveo.com/en/204/) won't return any results. Consequently, the recommendation interface either displays an empty result list, or remains entirely invisible. To reduce the chances of a recommendation interface yielding less than the desired number of results, you may want to request more items from the CR model than the specified `data-results-per-page` value. This can be done by using [`customQueryParameters`](https://docs.coveo.com/en/l25f0432#customqueryparameters-jvalue-object) when [associating your CR model with a query pipeline](https://docs.coveo.com/en/l25f0432/). This lets you specify [additional parameters](https://docs.coveo.com/en/13#operation/searchUsingPost-mlParameters) to send to Coveo ML on all queries. For example, you could request ten recommendations from the model, but only request five results from the index by using the `num` [`mlParameters`](https://docs.coveo.com/en/13#operation/searchUsingPost-mlParameters). ```json { "position": 1, "modelId": "XXXXXX_eventrecommendation_XXXXXXXX_XXXX_XXXX_XXXX_XXXXXXXXXXXX", "condition": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "modelEngine": "eventrecommendation", "rankingModifier": 1000, "cacheMaximumAge": "PT10S", "exclusive": true, "customQueryParameters": { "num": }, "useAdvancedConfiguration": false } ``` Where you replace `` with the number of recommendations that you want to request. ## Enable content recommendations padding When configuring a search endpoint for a recommendation interface, you may want your CR model to pad its output with popular or trending items, up to the desired number of results. Doing so can be useful if you want your recommendation interface to display the desired number of results even when the target ML model has not yet learned from enough UA data, or when the user's action history is empty. This can be done by using [`customQueryParameters`](https://docs.coveo.com/en/l25f0432#customqueryparameters-jvalue-object) when [associating your CR model with a query pipeline](https://docs.coveo.com/en/l25f0432/). This lets you specify [additional parameters](https://docs.coveo.com/en/13#operation/searchUsingPost-mlParameters) to send to Coveo ML on all queries, such as the `padding` [`mlParameters`](https://docs.coveo.com/en/13#operation/searchUsingPost-mlParameters). ```json { "position": 1, "modelId": "XXXXXX_eventrecommendation_XXXXXXXX_XXXX_XXXX_XXXX_XXXXXXXXXXXX", "condition": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "modelEngine": "eventrecommendation", "rankingModifier": 1000, "cacheMaximumAge": "PT10S", "exclusive": true, "customQueryParameters": { "padding": "" }, "useAdvancedConfiguration": false } ``` Where you replace `` with the `padding` value that you want to use (this can be either `popular` or `trending`). ## Leverage user context data in a recommendation interface If you send user [context](https://docs.coveo.com/en/1345/) [custom data](https://docs.coveo.com/en/1341/) [in your view events](https://docs.coveo.com/en/1818#user-context), you should ensure that every query sent from your recommendation interface [includes the current user context](https://docs.coveo.com/en/399/). Otherwise, recorded user context data will have no impact on the output of your CR model. ```javascript const recommendationElement = Coveo.$$(document).find(""); // Add user context to recommendation interface queries and UA events. Coveo.$$(recommendationElement).on("buildingQuery", function(e, args) { args.queryBuilder.addContext({ "": "", "": "", // ... "": "" }); }); Coveo.initRecommendation(recommendationElement); ``` ## Initializing an embedded recommendation interface When you include a `Recommendation` component inside a `SearchInterface` component (that is, in a full search page), the `initRecommendation` function call requires a second argument which references the main search interface element. ```javascript const recommendationElement = Coveo.$$(document).find(""); const searchInterfaceElement = Coveo.$$(document).find(""); Coveo.initRecommendation(recommendationElement, searchInterfaceElement); ```