eventTracking.ts
3.82 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { AxiosStatic } from "axios";
import {
EventTrackingController,
type EventTrackingBatchItem,
type EventTrackingBatchSaveBody,
type EventTrackingBusinessKeyVO,
type EventTrackingParamVO,
} from "@/api/EventTrackingController";
function voidBatch(axios: AxiosStatic, events: EventTrackingBatchItem[]): void {
if (!events.length) {
return;
}
const body: EventTrackingBatchSaveBody = { events };
void EventTrackingController.batchSave(axios, body).catch((e: unknown) => {
console.error("[eventTracking/batchSave]", e);
});
}
function strParam(
key: string,
value: string | number | boolean | null,
paramType: EventTrackingParamVO["paramType"] = "STRING"
): EventTrackingParamVO {
return { paramKey: key, paramValue: value as string | number | boolean | null, paramType };
}
function workOrderKeys(orderId: number): EventTrackingBusinessKeyVO[] {
return [{ businessType: "WORK_ORDER", businessKey: String(orderId) }];
}
/** 与 GET coupon/popup 的 `popupType` 一致;上报为 NUMBER */
function couponPopupTypeNumber(popupType: number): number {
if (typeof popupType === "number" && Number.isFinite(popupType)) {
return popupType;
}
const n = Number(popupType);
return Number.isFinite(n) ? n : 0;
}
/**
* 首页弹窗:`popup_type`(NUMBER);`coupon_id`(STRING) 仅一条,多券用英文逗号拼接。
*/
function couponPopupCommonParams(
popupType: number,
couponIds: readonly string[]
): EventTrackingParamVO[] {
const pt = couponPopupTypeNumber(popupType);
const ids = [...couponIds].map((id) => (id ?? "").trim()).filter((id) => id.length > 0);
return [
strParam("popup_type", pt, "NUMBER"),
strParam("coupon_id", ids.length > 0 ? ids.join(",") : "", "STRING"),
];
}
/** 首页权益弹窗主按钮(文档 Coupon_Popup_Click) */
export function trackCouponPopupClick(
axios: AxiosStatic,
popupType: number,
couponIds: readonly string[]
): void {
voidBatch(axios, [
{
eventCode: "coupon_popup_click",
params: couponPopupCommonParams(popupType, couponIds),
},
]);
}
/** 蒙层 / 右上角关闭;文档 Coupon_Popup_Close → `eventCode` 小写下划线;参数与 click 一致 */
export function trackCouponPopupClose(
axios: AxiosStatic,
popupType: number,
couponIds: readonly string[]
): void {
voidBatch(axios, [
{
eventCode: "coupon_popup_close",
params: couponPopupCommonParams(popupType, couponIds),
},
]);
}
/** 优惠卡券页「使用说明」展开 */
export function trackCouponRuleClick(axios: AxiosStatic, couponId: string): void {
voidBatch(axios, [
{
eventCode: "coupon_rule_click",
remark: "/module/order/CouponPage",
params: [strParam("coupon_id", couponId)],
},
]);
}
/** 待签署订单选券底栏切换(相对打开弹窗前的选择发生变化) */
export function trackOrderCouponChangeClick(
axios: AxiosStatic,
orderId: number,
selectedCouponId: string
): void {
voidBatch(axios, [
{
eventCode: "order_coupon_change_click",
businessKeys: workOrderKeys(orderId),
params: [strParam("selected_coupon_id", selectedCouponId)],
},
]);
}
/** 立即使用:核销接口成功回调 */
export function trackOrderPaySubmitClick(
axios: AxiosStatic,
orderId: number,
usedCouponId: string,
actualDiscountAmt: number
): void {
voidBatch(axios, [
{
eventCode: "order_pay_submit_click",
businessKeys: workOrderKeys(orderId),
params: [
strParam("used_coupon_id", usedCouponId),
strParam("actual_discount_amt", actualDiscountAmt, "NUMBER"),
],
},
]);
}