Files
web/apps/scandic-web/utils/tracking/myStay.ts
2025-05-14 15:10:18 +00:00

221 lines
5.6 KiB
TypeScript

import { trackEvent } from "./base"
import type { SelectedAncillary } from "@/types/components/myPages/myStay/ancillaries"
import { CurrencyEnum } from "@/types/enums/currency"
import type { PackageSchema } from "@/types/trpc/routers/booking/confirmation"
import type { CreditCard } from "@/types/user"
export function trackCancelStay(hotelId: string, bnr: string) {
trackEvent({
event: "BookingCancellations",
hotelInfo: {
hotelId: hotelId,
bnr: bnr,
},
})
}
export function trackMyStayPageLink(ctaName: string) {
trackEvent({
event: "confirmationPageLinks",
cta: {
name: ctaName,
},
})
}
type LateArrivalGuarantee = "mandatory" | "yes" | "no" | "na"
export function trackGlaSaveCardAttempt(
hotelId: string,
savedCreditCard: CreditCard | undefined,
lateArrivalGuarantee: LateArrivalGuarantee
) {
trackEvent({
event: "glaCardSaveAttempt",
hotelInfo: {
hotelId,
lateArrivalGuarantee,
guaranteedProduct: "room",
},
paymentInfo: {
status: "glacardsaveattempt",
type: savedCreditCard?.cardType,
},
})
}
export function buildAncillaries(
packages: {
code: string
quantity: number
comment?: string
}[],
selectedAncillary: SelectedAncillary | null | undefined,
deliveryTime: string | string[] | null | undefined
) {
return packages.map((pkg) => {
const payedWithCard = pkg.code === selectedAncillary?.id
const payedWithPoints = pkg.code === selectedAncillary?.loyaltyCode
return {
productId: pkg.code,
productUnits: pkg.quantity,
productDeliveryTime: deliveryTime,
productName: selectedAncillary?.title,
productCategory: selectedAncillary?.categoryName,
...(payedWithCard && {
productPrice: (selectedAncillary?.price.total ?? 0) * pkg.quantity,
currency: selectedAncillary?.price.currency,
}),
...(payedWithPoints && {
productPoints: (selectedAncillary?.points ?? 0) * pkg.quantity,
}),
}
})
}
export function trackGlaAncillaryAttempt(
savedCreditCard: CreditCard | undefined,
packages: {
code: string
quantity: number
comment: string | undefined
}[],
selectedAncillary: SelectedAncillary | null,
deliveryTime: string | undefined
) {
trackEvent({
event: "GuaranteeAttemptAncillary",
paymentInfo: {
status: "glacardsaveattempt",
type: savedCreditCard?.cardType,
},
ancillaries: buildAncillaries(packages, selectedAncillary, deliveryTime),
hotelInfo: {
hotelId: selectedAncillary?.hotelId,
lateArrivalGuarantee: "yes",
guaranteedProduct: "room + ancillary",
},
})
}
export function trackAncillarySuccess(
confirmationNumber: string,
packages: {
code: string
quantity: number
comment?: string
}[],
deliveryTime: string | null | undefined,
guaranteedProduct: string,
selectedAncillary: SelectedAncillary | null,
cardType?: string,
roomTypeCode?: string
) {
trackEvent({
event: "AncillarySuccess",
hotelInfo: {
bnr: confirmationNumber,
roomTypeCode: roomTypeCode,
lateArrivalGuarantee: "yes",
guaranteedProduct: guaranteedProduct,
},
paymentInfo: {
status: "glacardsaveconfirmed",
type: cardType,
},
ancillaries: buildAncillaries(packages, selectedAncillary, deliveryTime),
})
}
export function trackAncillaryFailed(
packages: {
code: string
quantity: number
comment?: string
}[],
deliveryTime: string | null | undefined,
selectedAncillary: SelectedAncillary | null
) {
trackEvent({
event: "GuaranteeFailAncillary",
ancillaries: buildAncillaries(packages, selectedAncillary, deliveryTime),
hotelInfo: {
hotelId: selectedAncillary?.hotelId,
lateArrivalGuarantee: "yes",
guaranteedProduct: "ancillary",
},
})
}
export function trackViewAncillary(ancillary: SelectedAncillary) {
trackEvent({
event: "viewAncillary",
ancillaries: [
{
hotelId: ancillary.hotelId,
productId: ancillary.id,
productName: ancillary.title,
productCategory: ancillary.categoryName,
},
],
})
}
export function trackRemoveAncillary(
ancillary: PackageSchema,
hotelId: string,
deliveryTime?: string
) {
const isPoints = ancillary.currency === CurrencyEnum.POINTS
trackEvent({
event: "removeAncillary",
ancillaries: [
{
hotelId,
productId: ancillary.code,
productPrice: isPoints ? 0 : ancillary.totalPrice,
productPoints: isPoints ? ancillary.totalPrice : 0,
productUnits: ancillary.totalUnit,
productType: ancillary.type,
productDeliveryTime: deliveryTime,
},
],
})
}
export function trackAddAncillary(
ancillary: SelectedAncillary | null,
quantityWithCard: number | null,
quantityWithPoints: number | null
) {
const ancillaries = []
if ((quantityWithCard ?? 0) > 0) {
ancillaries.push({
hotelId: ancillary?.hotelId,
productId: ancillary?.id,
productName: ancillary?.title,
productUnits: quantityWithCard,
productPrice: (ancillary?.price.total ?? 0) * (quantityWithCard ?? 0),
currency: ancillary?.price.currency,
productCategory: ancillary?.categoryName,
})
}
if ((quantityWithPoints ?? 0) > 0) {
ancillaries.push({
hotelId: ancillary?.hotelId,
productId: ancillary?.loyaltyCode,
productName: ancillary?.title,
productUnits: quantityWithPoints,
productPoints: (ancillary?.points ?? 0) * (quantityWithPoints ?? 0),
productCategory: ancillary?.categoryName,
})
}
trackEvent({
event: "addAncillary",
ancillaries,
})
}