UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

82 lines (81 loc) 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendRecommendationViewEvent = void 0; const index_js_1 = require("../../events/index.js"); const KlevuTypeOfRequest_js_1 = require("../../models/KlevuTypeOfRequest.js"); const recommendationTypeOfRequests = [ KlevuTypeOfRequest_js_1.KlevuTypeOfRequest.AlsoBought, KlevuTypeOfRequest_js_1.KlevuTypeOfRequest.AlsoViewed, KlevuTypeOfRequest_js_1.KlevuTypeOfRequest.NewArrivals, KlevuTypeOfRequest_js_1.KlevuTypeOfRequest.SimilarProducts, KlevuTypeOfRequest_js_1.KlevuTypeOfRequest.Trending, ]; /** * This modifier should be used with all recommendation requests. It sends * correct event data to klevu backend on recommendation view * * @category Modifier * @returns */ function sendRecommendationViewEvent(eventData, override, isMobile) { return { klevuModifierId: "sendMerchandisingViewEvent", ssrOnResultFE: true, onResult: (res, f) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; // is used with kmcRecommendation query if (f.klevuFunctionId === "kmcRecommendation") { let products; if (res.queryExists((_c = (_b = (_a = f.queries) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : "")) { products = (_f = res.queriesById((_e = (_d = f.queries) === null || _d === void 0 ? void 0 : _d[0].id) !== null && _e !== void 0 ? _e : "")) === null || _f === void 0 ? void 0 : _f.records; } const kmcData = (_g = f.params) === null || _g === void 0 ? void 0 : _g.kmcConfig; if (!kmcData) { console.warn("Problem with kmcData fetching. Can't send recommendationViewEvent"); return res; } if (kmcData.staticContent && kmcData.staticContent.length !== 0) { const resolution = isMobile ? "mobile" : "desktop"; const image = (_h = kmcData.staticContent[0].image.find((image) => image.resolution === resolution)) !== null && _h !== void 0 ? _h : kmcData.staticContent[0].image[0]; const bannerInfo = { resolution: resolution, index: 1, banner_alt_tag: image.altTag, banner_image_url: image.url, content_type: "image", }; index_js_1.KlevuEvents.recommendationView({ recommendationMetadata: Object.assign({}, kmcData.metadata), bannerInfo, }); } else { index_js_1.KlevuEvents.recommendationView({ recommendationMetadata: Object.assign({}, kmcData.metadata), products, override, }); } return res; } if (!eventData) { throw new Error("Need to provide eventData parameter for custom view events"); } const queries = (_j = f.queries) === null || _j === void 0 ? void 0 : _j.filter((q) => recommendationTypeOfRequests.includes(q.typeOfRequest)); if (!queries || (queries === null || queries === void 0 ? void 0 : queries.length) == 0) { return res; } const products = (_k = res.queriesById(queries[0].id)) === null || _k === void 0 ? void 0 : _k.records; if (!products) { console.warn("No products in result. Can't send recommendationViewEvent"); return res; } index_js_1.KlevuEvents.recommendationView({ recommendationMetadata: eventData, products, }); return res; }, }; } exports.sendRecommendationViewEvent = sendRecommendationViewEvent;