Recs Fallback Query
In some cases, it can be desirable to apply a "fallback" query to Smart Recommendation banners where the applied logic might equate to no results found.
For example: when first installing Smart Recommendations "Checkout/Cart" logic for "Also bought Together" banners, there may not be enough initial data to qualify a result.
Any of the API options for Smart Recommendations can be applied to the fallback query.
Example
In the example below, a "Trending Products" (RECS_TRENDING) query replaces the target Recommendations banner if no results are found.
Note: Apply the target klevu-recs banner id to the klevu_recsFallBackId variable.
Note: Also adjust the templateData.metadata.title to match the intent of the fallback query.
var klevu_recsFallbackId = "< ADD YOUR TARGET KLEVU-RECS ID HERE >";
var klevu_recsFallback = false;
/* Observer for record queries */
function klevu_processRECSRecordQueries(recordQueries, recsKey)
{
var recsQuery = recordQueries[0];
/* apply only to the specified banner recsKey */
if (recsKey == klevu_recsFallbackId) {
klevu.setObjectPath(recsQuery, "settings.fallbackQueryId", "myFallbackQuery");
var myFallbackQuery = {
"id": "myFallbackQuery",
"isFallbackQuery": true,
"fallbackWhenCountLessThan": 1,
"typeOfRequest":"RECS_TRENDING",
"settings":{"typeOfRecords":["KLEVU_PRODUCT"],"limit":10}
}
recordQueries[1] = myFallbackQuery;
}
}
/* Observer for query results */
function klevu_processRECSQueryResults(queryResults, recsKey) {
/* apply only to the specified banner recsKey */
if (recsKey == klevu_recsFallbackId) {
if (queryResults[0].meta.totalResultsFound == 0) {
klevu_recsFallback = true;
/* replace main results with fallback results */
queryResults[0] = queryResults[1];
queryResults[0].id = "klevuRECSItemList";
}
}
}
/* Observer to process template data */
function klevu_processRECSTemplateData(templateData, recsKey) {
/* apply only to the specified banner recsKey */
if (recsKey == klevu_recsFallbackId && klevu_recsFallback) {
/* replace the title as desired (based on Fallback intent) */
templateData.metadata.title = 'Trending Products (as Replacement)';
}
}See : Overview for more details on the functions used in the above example.