/
sendMerchandisingViewEvent.ts
57 lines (49 loc) · 1.53 KB
/
sendMerchandisingViewEvent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { KlevuFetchModifer } from "../index.js"
import { KlevuEvents } from "../../events/index.js"
import { extractActiveFilters } from "../../utils/extractActiveFilters.js"
import { KlevuV1CategoryProductsView } from "../../events/eventRequests.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: string,
override?: Partial<KlevuV1CategoryProductsView>
): KlevuFetchModifer {
return {
klevuModifierId: "sendMerchandisingViewEvent",
onResult: (res, f) => {
if (!f.params) {
return res
}
const { id, abtest } = f.params
if (f.klevuFunctionId !== "categoryMerchandising" || !id) {
return res
}
const categoryFunction = f.queries?.find((q) =>
Boolean(q.settings?.query?.categoryPath)
)
const category =
categoryFunction?.settings?.query?.categoryPath ?? "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?.abTestId,
abTestVariantId: abtest?.abTestVariantId,
activeFilters: extractActiveFilters(queryResult),
override,
})
return res
},
}
}