How To
Recs Fallback Query
1min
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 docid 4lofui8shvdj88swa1yzg 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 docid\ o03qerb3f sy0bhba64af for more details on the functions used in the above example