UNPKG

@klevu/core

Version:

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

44 lines (43 loc) 2.05 kB
import { KlevuEvents } from "../../events/index.js"; import { extractActiveFilters } from "../../utils/extractActiveFilters.js"; /** * This modifier should be used with merchandising query. It sends * automatically correct event data to Klevu * * @category Modifier * @param title Title of the category page viewed * @returns */ export function sendMerchandisingViewEvent(title, override) { return { klevuModifierId: "sendMerchandisingViewEvent", ssrOnResultFE: true, onResult: (res, f) => { var _a, _b, _c, _d; if (!f.params) { return res; } const { id, abtest } = f.params; if (f.klevuFunctionId !== "categoryMerchandising" || !id) { return res; } const categoryFunction = (_a = f.queries) === null || _a === void 0 ? void 0 : _a.find((q) => { var _a, _b; return Boolean((_b = (_a = q.settings) === null || _a === void 0 ? void 0 : _a.query) === null || _b === void 0 ? void 0 : _b.categoryPath); }); const category = (_d = (_c = (_b = categoryFunction === null || categoryFunction === void 0 ? void 0 : categoryFunction.settings) === null || _b === void 0 ? void 0 : _b.query) === null || _c === void 0 ? void 0 : _c.categoryPath) !== null && _d !== void 0 ? _d : "unknown"; const queryResult = res.queriesById(id); if (!queryResult) { return res; } KlevuEvents.categoryMerchandisingView({ categoryTitle: title, klevuCategory: category, products: queryResult.records, pageStartsFrom: queryResult.meta.offset, abTestId: abtest === null || abtest === void 0 ? void 0 : abtest.abTestId, abTestVariantId: abtest === null || abtest === void 0 ? void 0 : abtest.abTestVariantId, activeFilters: extractActiveFilters(queryResult), override, }); return res; }, }; }