Files
web/packages/booking-flow/lib/components/SelectRate/Tracking/tracking.ts
Bianca Widstam 76c353058b Merged in fix/BOOK-459-tracking-search (pull request #2984)
fix(BOOK-459): update searchTerm to city or hotelname

* fix(BOOK-459): update searchTerm to city or hotelname


Approved-by: Erik Tiekstra
2025-10-22 14:34:59 +00:00

108 lines
3.0 KiB
TypeScript

import { differenceInCalendarDays, format, isWeekend } from "date-fns"
import { trackEvent } from "@scandic-hotels/tracking/base"
import {
type LowestRoomPriceEvent,
TrackingChannelEnum,
type TrackingSDKHotelInfo,
type TrackingSDKPageData,
} from "@scandic-hotels/tracking/types"
import { ChildBedMapEnum } from "@scandic-hotels/trpc/enums/childBedMapEnum"
import type { Lang } from "@scandic-hotels/common/constants/language"
import type { Child } from "@scandic-hotels/trpc/types/child"
import type { Room } from "../../../types/components/selectRate/selectRate"
type ChildrenInRoom = (Child[] | null)[] | null
type SelectRateTrackingInput = {
lang: Lang
arrivalDate: Date
departureDate: Date
hotelId: string
hotelName: string
country: string | undefined
hotelCity: string | undefined
bookingCode?: string
isRedemption?: boolean
specialRoomType?: string
rooms?: Room[]
}
export function getSelectRateTracking({
lang,
arrivalDate,
departureDate,
hotelId,
hotelName,
country,
hotelCity,
bookingCode,
isRedemption = false,
specialRoomType,
rooms = [],
}: SelectRateTrackingInput) {
const pageTrackingData: TrackingSDKPageData = {
channel: TrackingChannelEnum.hotelreservation,
domainLanguage: lang,
pageId: "select-rate",
pageName: "hotelreservation|select-rate",
pageType: "bookingroomsandratespage",
siteSections: "hotelreservation|select-rate",
siteVersion: "new-web",
}
let adultsInRoom: number[] = []
let childrenInRoom: ChildrenInRoom = null
if (rooms?.length) {
adultsInRoom = rooms.map((room) => room.adults ?? 0)
childrenInRoom = rooms.map((room) => room.childrenInRoom ?? null)
}
const hotelsTrackingData: TrackingSDKHotelInfo = {
ageOfChildren: childrenInRoom
?.map((c) => c?.map((k) => k.age).join(",") ?? "")
.join("|"),
arrivalDate: format(arrivalDate, "yyyy-MM-dd"),
bookingTypeofDay: isWeekend(arrivalDate) ? "weekend" : "weekday",
childBedPreference: childrenInRoom
?.map((c) => c?.map((k) => ChildBedMapEnum[k.bed]).join(",") ?? "")
.join("|"),
country,
departureDate: format(departureDate, "yyyy-MM-dd"),
duration: differenceInCalendarDays(departureDate, arrivalDate),
hotelID: hotelId,
leadTime: differenceInCalendarDays(arrivalDate, new Date()),
noOfAdults: adultsInRoom.join(","),
noOfChildren: childrenInRoom?.map((kids) => kids?.length ?? 0).join(","),
noOfRooms: rooms?.length ?? 0,
region: hotelCity,
searchTerm: hotelName,
searchType: "hotel",
bookingCode: bookingCode ?? "n/a",
rewardNight: isRedemption ? "yes" : "no",
specialRoomType,
}
return {
hotelsTrackingData,
pageTrackingData,
}
}
export function trackLowestRoomPrice(event: LowestRoomPriceEvent) {
trackEvent({
event: "lowestRoomPrice",
hotelInfo: {
hotelId: event.hotelId,
arrivalDate: event.arrivalDate,
departureDate: event.departureDate,
},
viewItemInfo: {
lowestPrice: event.lowestPrice,
currency: event.currency,
},
})
}