UNPKG

@klevu/core

Version:

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

93 lines (92 loc) 4.38 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * Automatically sends search event to Klaviyo on search, recommendations and category navigation. * * @param params * @returns */ export function klaviyo(params = { productAttributes: [ "id", "name", "url", "sku", "imageUrl", "price", "salePrice", ], productLimit: 10, language: "en", }) { return { klevuModifierId: "kleviyo", ssrOnResultFE: true, onResult: (response, query) => { var _a; // make sure that Klaviyo array is available if (!globalThis._learnq) { globalThis._learnq = globalThis._learnq || []; } if (![ "search", "categoryMerchandising", "recommendation", "kmcRecommendation", ].includes(query.klevuFunctionId)) { console.error("Klevu Klaviyo modifier only works with search, categoryMerchandising, recommendation and kmcRecommendation functions"); return; } for (const q of (_a = query.queries) !== null && _a !== void 0 ? _a : []) { // if there is a fallback use that const fallbackId = `${q.id}-fallback`; const res = response.queryExists(fallbackId) ? response.queriesById(`${q.id}-fallback`) : response.queriesById(q.id); if (!res) { continue; } const products = res === null || res === void 0 ? void 0 : res.records.slice(0, params.productLimit); if (!products || products.length == 0) { continue; } let annotations; res.hooks.push((clickParams) => __awaiter(this, void 0, void 0, function* () { var _b, _c, _d, _e; if (!annotations) { annotations = yield ((_b = res.annotationsById) === null || _b === void 0 ? void 0 : _b.call(res, products[0].id, params.language)); } const clickedProduct = res.records.find((p) => p.id === clickParams.productId); (_c = globalThis._learnq) === null || _c === void 0 ? void 0 : _c.push([ "track", `Klevu ${clickParams.type}`, { "Search Term": res.meta.searchedTerm, "Search Term(full)": (_d = annotations === null || annotations === void 0 ? void 0 : annotations.annotations) === null || _d === void 0 ? void 0 : _d.fullTerm, Subjects: (_e = annotations === null || annotations === void 0 ? void 0 : annotations.annotations) === null || _e === void 0 ? void 0 : _e.subjects, "Total Number of Product Results": res.meta.totalResultsFound, "Product Clicked": clickedProduct && params.productAttributes.reduce((acc, curr) => { acc[curr] = clickedProduct[curr]; return acc; }, {}), "Search Results": products.map((p) => { return params.productAttributes.reduce((acc, curr) => { acc[curr] = p[curr]; return acc; }, {}); }), }, ]); })); } }, }; }