eventTracking.ts 3.82 KB
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"),
            ],
        },
    ]);
}