UNPKG

@klevu/core

Version:

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

296 lines (295 loc) 15.9 kB
import { KlevuConfig, } from "../index.js"; import { KlevuLastClickedProducts } from "../store/lastClickedProducts.js"; import { KlevuLastSearches } from "../store/lastSearches.js"; import { USER_SESSION_ID_STORAGE_KEY } from "../resolvers/usersession.js"; import { KlevuStorage } from "../utils/storage.js"; import { KlevuEventV1CategoryProductClick, KlevuEventV1CategoryView, KlevuEventV1ProductTracking, KlevuEventV1Search, KlevuEventV2, KlevuEventV2CheckedOutProducts, KlevuEventV1BannerClick, } from "./eventRequests.js"; import { KlevuIpResolver } from "../resolvers/ipresolver.js"; /** * @category KlevuEvents */ export class KlevuEvents { /** * Tell Klevu what products where bought by the user * * @param items Items user bought * @property amount count of bought products * @property product KlevuProduct that is being bought * @property variantId optional variantId that is being bought * @property override optional override any settings of sent data * */ static buy({ user, items, }) { const userData = KlevuIpResolver.getDefault().getUserData(); const data = { event: "order_purchase", event_version: "1.0.0", event_apikey: KlevuConfig.getDefault().apiKey, user_profile: Object.assign(Object.assign({}, user), { ip_address_v4: userData.ipv4, ip_address_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid }), event_data: { items: items.map((i) => { var _a, _b, _c, _d, _e, _f; return (Object.assign({ currency: (_a = i.product.currency) !== null && _a !== void 0 ? _a : "USD", item_group_id: (_b = i.product.itemGroupId) !== null && _b !== void 0 ? _b : i.product.id, item_id: i.product.id, item_name: (_c = i.product.name) !== null && _c !== void 0 ? _c : "unknown", item_variant_id: (_d = i.variantId) !== null && _d !== void 0 ? _d : i.product.id, unit_price: parseFloat((_e = i.product.salePrice) !== null && _e !== void 0 ? _e : "0"), order_id: i.orderId, order_line_id: i.orderLineId, units: i.amount }, ((_f = i.override) !== null && _f !== void 0 ? _f : {}))); }), }, }; KlevuEventV2CheckedOutProducts(data); } /** * When recommendation banner is shown in the page * * @param recommendation Metadata of what recommendation is shown * @param products List of all products that are shown * @param bannerInfo Information about the static banner that is shown * @param override Ability to override any analytical keys in low level */ static recommendationView({ recommendationMetadata, products, bannerInfo, override = {}, }) { const userData = KlevuIpResolver.getDefault().getUserData(); const data = { event: "view_recs_list", event_version: "1.0.0", event_apikey: KlevuConfig.getDefault().apiKey, user_profile: { sessionId: KlevuStorage.getItem(USER_SESSION_ID_STORAGE_KEY) || undefined, ip_address_v4: userData.ipv4, ip_address_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }, event_data: { list_id: recommendationMetadata.recsKey, list_logic: recommendationMetadata.logic, list_name: recommendationMetadata.title, action: recommendationMetadata.action, spot_id: recommendationMetadata.spotKey, spot_name: recommendationMetadata.spotName, segment_name: recommendationMetadata.segmentName || undefined, segment_id: recommendationMetadata.segmentKey || undefined, static_content: bannerInfo && [bannerInfo], items: products && products.map((p, index) => { var _a, _b; if (!p.id) { throw new Error("Cannot send event without product id"); } return { index: index + 1, item_id: p.id, item_group_id: p.itemGroupId || p.id, item_name: (_a = p.name) !== null && _a !== void 0 ? _a : "unknown", item_variant_id: p.variantId || p.id, price: (_b = p.price) !== null && _b !== void 0 ? _b : "0", currency: p.currency, item_brand: p.brand, item_category: p.category, }; }), }, }; KlevuEventV2([ Object.assign(Object.assign({}, data), override), ]); } /** * When product has been clicked in the recommendation banner * * @param recommendationMetadata Metadata of what recommendation is clicked * @param bannerInfo Information about the static banner that is shown * @param product Which product is clicked in the list * @param productIndexInList What is the index of the product in the list. Starting from 1 * @param override Ability to override any analytical keys in low level */ static recommendationClick({ recommendationMetadata, product, bannerInfo, productIndexInList, variantId, override = {}, }) { var _a, _b; if (product) { KlevuLastClickedProducts.click(product.id, product); } const userData = KlevuIpResolver.getDefault().getUserData(); const data = { event: "select_recs_list", event_version: "1.0.0", event_apikey: KlevuConfig.getDefault().apiKey, user_profile: { sessionId: KlevuStorage.getItem(USER_SESSION_ID_STORAGE_KEY) || undefined, ip_address_v4: userData.ipv4, ip_address_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }, event_data: { list_id: recommendationMetadata.recsKey, list_logic: recommendationMetadata.logic, list_name: recommendationMetadata.title, action: recommendationMetadata.action, spot_id: recommendationMetadata.spotKey, spot_name: recommendationMetadata.spotName, static_content: bannerInfo && [bannerInfo], segment_name: recommendationMetadata.segmentName || undefined, segment_id: recommendationMetadata.segmentKey || undefined, items: product && [ { index: productIndexInList !== null && productIndexInList !== void 0 ? productIndexInList : 1, item_id: product.id, item_group_id: product.itemGroupId || product.id, item_name: (_a = product.name) !== null && _a !== void 0 ? _a : "unknown", item_variant_id: variantId || product.id, price: (_b = product.salePrice) !== null && _b !== void 0 ? _b : "0", currency: product.currency, item_brand: product.brand, item_category: product.category, }, ], }, }; KlevuEventV2([ Object.assign(Object.assign({}, data), override), ]); } /** * When product is clicked. Do not use this for recommendations * * @param searchTerm * @param product */ static searchProductClick({ product, searchTerm, variantId, activeFilters, tags, override = {}, }) { var _a, _b; if (!product.id) { throw new Error("Cannot send event without product id"); } KlevuLastClickedProducts.click(product.id, product); const userData = KlevuIpResolver.getDefault().getUserData(); const data = { klevu_apiKey: KlevuConfig.getDefault().apiKey, klevu_type: "clicked", klevu_keywords: searchTerm, klevu_productGroupId: product.itemGroupId || product.id, klevu_productId: product.id, klevu_productName: (_a = product.name) !== null && _a !== void 0 ? _a : "unknown", klevu_productUrl: (_b = product.url) !== null && _b !== void 0 ? _b : "", klevu_productVariantId: variantId || product.id, klevu_activeFilters: activeFilters, tags: tags || [], klevu_shopperIP_v4: userData.ipv4, klevu_shopperIP_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }; KlevuEventV1ProductTracking(Object.assign(Object.assign({}, data), override)); } /** * What user has last searched. This is important for Klevu to function * properly. Use `sendSearchEvent()` modifier with search query to send results * * @param term What was searched * @param totalResults Total number of results (can be found in result meta) * @param typeOfSearch Type of search used (can be found in result meta) * @param override Ability to override any analytical keys in low level */ static search({ term, totalResults, typeOfSearch, activeFilters, tags, override = {}, }) { KlevuLastSearches.save(term); const userData = KlevuIpResolver.getDefault().getUserData(); const data = { klevu_apiKey: KlevuConfig.getDefault().apiKey, klevu_term: term, klevu_totalResults: totalResults, klevu_typeOfQuery: typeOfSearch, klevu_activeFilters: activeFilters, klevu_shopperIP_v4: userData.ipv4, klevu_shopperIP_v6: userData.ipv6, tags: tags || [], data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }; KlevuEventV1Search(Object.assign(Object.assign({}, data), override)); } /** * * @param categoryTitle This is the name of the category being visited. For example, Stackable Rings. The name should not include parent categories. * @param klevuCategory This is the complete hierarchy of the category being visited. For example, Jewellery;Rings;Stackable Rings. Please note the use of a semicolon as the separator between a parent and a child category. * @param products Products in the view * @param pageStartsFrom Offset of the first product being shown on this page. For example, if you are displaying 30 products per page and if a customer is on the 2nd page, the value here should be 30. If on the 3rd page, it will be 60. * @param abTestId The AB test id currently running * @param abTestVariantId Id of AB test variant * @param activeFilters The string version of active filters applied to the query that got the products. * @param override Ability to override any analytical keys in low level */ static categoryMerchandisingView({ categoryTitle, klevuCategory, products, pageStartsFrom, abTestId, abTestVariantId, activeFilters, override = {}, }) { const userData = KlevuIpResolver.getDefault().getUserData(); const data = { klevu_apiKey: KlevuConfig.getDefault().apiKey, klevu_categoryName: categoryTitle, klevu_categoryPath: klevuCategory, klevu_productIds: products.map((p) => p.id).join(","), klevu_pageStartsFrom: pageStartsFrom, klevu_activeFilters: activeFilters, klevu_abTestId: abTestId, klevu_abTestVariantId: abTestVariantId, klevu_shopperIP_v4: userData.ipv4, klevu_shopperIP_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }; KlevuEventV1CategoryView(Object.assign(Object.assign({}, data), override)); } /** * * @param product Product clicked * @param categoryTitle This is the name of the category being visited. For example, Stackable Rings. The name should not include parent categories. * @param klevuCategory This is the complete hierarchy of the category being visited. For example, Jewellery;Rings;Stackable Rings. Please note the use of a semicolon as the separator between a parent and a child category. * @param variantId This is the child/variant ID of the clicked product. eg. 12345. For compound products with a parent and multiple child/variant products, this is the ID of the specific variant. * @param productPosition Position of the product on the category page when it was clicked. For example, the value would be 0 if it is the first product on the first page. The value will be 30, if it is the first product on the 2nd page with 30 products being displayed per page. * @param activeFilters The string version of active filters applied to the query that got the products. * @param override Ability to override any analytical keys in low level */ static categoryMerchandisingProductClick({ product, categoryTitle, klevuCategory, variantId, productPosition, abTestId, abTestVariantId, activeFilters, override = {}, }) { if (!product.id) { throw new Error("Cannot send event without product id"); } KlevuLastClickedProducts.click(product.id, product); const userData = KlevuIpResolver.getDefault().getUserData(); const data = { klevu_apiKey: KlevuConfig.getDefault().apiKey, klevu_categoryName: categoryTitle, klevu_productGroupId: product.itemGroupId || product.id, klevu_productVariantId: variantId || product.id, klevu_categoryPath: klevuCategory, klevu_productId: product.id, klevu_productName: product.name, klevu_productUrl: product.url, klevu_salePrice: product.salePrice, klevu_productSku: product.sku, klevu_productPosition: productPosition, klevu_abTestId: abTestId, klevu_abTestVariantId: abTestVariantId, klevu_activeFilters: activeFilters, klevu_shopperIP_v4: userData.ipv4, klevu_shopperIP_v6: userData.ipv6, data_protection: KlevuConfig.getDefault().useConsent && KlevuConfig.getDefault().consentGiven, klevu_uuid: userData.uuid, }; KlevuEventV1CategoryProductClick(Object.assign(Object.assign({}, data), override)); } /** * When user clicks on the banner that is in quicksearch or on search landing page */ static imageBannerClick({ term, bannerId, bannerName, imageUrl, targetUrl, override = {}, }) { const data = { klevu_apiKey: KlevuConfig.getDefault().apiKey, type: "banner", klevu_request: "click", klevu_term: term, klevu_bannerId: bannerId, klevu_bannerName: bannerName, klevu_image: imageUrl, klevu_target: targetUrl, }; KlevuEventV1BannerClick(Object.assign(Object.assign({}, data), override)); } }