From 41efb3a7b3c71b4f3a6f7f3409ad43b4bc594ac8 Mon Sep 17 00:00:00 2001 From: Anton Gunnarsson Date: Wed, 6 Aug 2025 08:35:48 +0000 Subject: [PATCH] Merged in feat/sw-3207-refactor-select-hotel-tracking (pull request #2587) feat(SW-3207): Refactor select-hotel tracking * Refactor select-hotel tracking Approved-by: Bianca Widstam --- .../(standard)/alternative-hotels/page.tsx | 73 ++++++++-------- .../(standard)/select-hotel/page.tsx | 62 +++++++------- .../SelectHotelMapContainer.tsx | 44 +++++----- .../SelectHotel/SelectHotelMap/tracking.ts | 77 ----------------- .../HotelReservation/SelectHotel/tracking.ts | 83 ++++++++++++------- .../SelectRate/Tracking/index.tsx | 23 ++--- .../SelectRate/Tracking/tracking.ts | 50 +++++++---- .../HotelReservation/SelectRate/index.tsx | 18 ++-- apps/scandic-web/utils/hotelSearchDetails.ts | 28 ------- 9 files changed, 184 insertions(+), 274 deletions(-) delete mode 100644 apps/scandic-web/components/HotelReservation/SelectHotel/SelectHotelMap/tracking.ts diff --git a/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/alternative-hotels/page.tsx b/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/alternative-hotels/page.tsx index ef67a2f8d..9310f1a3e 100644 --- a/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/alternative-hotels/page.tsx +++ b/apps/scandic-web/app/[lang]/(live)/(public)/hotelreservation/(standard)/alternative-hotels/page.tsx @@ -11,7 +11,7 @@ import { FamilyAndFriendsCodes } from "@/constants/booking" import FnFNotAllowedAlert from "@/components/HotelReservation/FnFNotAllowedAlert/FnFNotAllowedAlert" import SelectHotel from "@/components/HotelReservation/SelectHotel" import { getHotels } from "@/components/HotelReservation/SelectHotel/helpers" -import { getTracking } from "@/components/HotelReservation/SelectHotel/tracking" +import { getSelectHotelTracking } from "@/components/HotelReservation/SelectHotel/tracking" import TrackingSDK from "@/components/TrackingSDK" import { getIntl } from "@/i18n" import { getHotelSearchDetails } from "@/utils/hotelSearchDetails" @@ -38,18 +38,10 @@ export default async function AlternativeHotelsPage( return notFound() } - const { - adultsInRoom, - bookingCode, - childrenInRoom, - city, - cityIdentifier, - hotel: isAlternativeFor, - noOfRooms, - redemption, - } = searchDetails - - if (bookingCode && FamilyAndFriendsCodes.includes(bookingCode)) { + if ( + booking.bookingCode && + FamilyAndFriendsCodes.includes(booking.bookingCode) + ) { const cookieStore = await cookies() const isInvalidFNF = cookieStore.get("sc")?.value !== "1" @@ -64,22 +56,22 @@ export default async function AlternativeHotelsPage( fromDate: booking.fromDate, toDate: booking.toDate, rooms: booking.rooms, - isAlternativeFor, - bookingCode, - city, - redemption: !!redemption, + isAlternativeFor: searchDetails.hotel, + bookingCode: booking.bookingCode, + city: searchDetails.city, + redemption: !!searchDetails.redemption, }) const arrivalDate = new Date(booking.fromDate) const departureDate = new Date(booking.toDate) - const isRedemptionAvailability = redemption + const isRedemptionAvailability = searchDetails.redemption ? hotels.some( (hotel) => hotel.availability.productType?.redemptions?.length ) : false - const isBookingCodeRateAvailable = bookingCode + const isBookingCodeRateAvailable = booking.bookingCode ? hotels.some( (hotel) => hotel.availability.bookingCode && @@ -87,24 +79,29 @@ export default async function AlternativeHotelsPage( ) : false - const { hotelsTrackingData, pageTrackingData } = getTracking( - params.lang, - !!isAlternativeFor, + const { hotelsTrackingData, pageTrackingData } = getSelectHotelTracking({ + lang: params.lang, + pageId: searchDetails.hotel ? "alternative-hotels" : "select-hotel", + pageName: searchDetails.hotel + ? "hotelreservation|alternative-hotels" + : "hotelreservation|select-hotel", + siteSections: searchDetails.hotel + ? "hotelreservation|alternative-hotels" + : "hotelreservation|select-hotel", arrivalDate, departureDate, - adultsInRoom, - childrenInRoom, - hotels?.length ?? 0, - booking.hotelId, - noOfRooms, - hotels?.[0]?.hotel.address.country, - hotels?.[0]?.hotel.address.city, - cityIdentifier, - bookingCode, + rooms: booking.rooms, + hotelsResult: hotels?.length ?? 0, + searchTerm: searchDetails.hotel + ? booking.hotelId + : searchDetails.cityIdentifier, + country: hotels?.[0]?.hotel.address.country, + hotelCity: hotels?.[0]?.hotel.address.city, + bookingCode: booking.bookingCode, isBookingCodeRateAvailable, - redemption, - isRedemptionAvailability - ) + isRedemption: searchDetails.redemption, + isRedemptionAvailable: isRedemptionAvailability, + }) const mapHref = alternativeHotelsMap(params.lang) @@ -114,17 +111,17 @@ export default async function AlternativeHotelsPage( defaultMessage: "Alternatives for {value}", }, { - value: isAlternativeFor.name, + value: searchDetails.hotel.name, } ) const suspenseKey = stringify(searchParams) return ( <> hotel.availability.productType?.redemptions?.length ) : false - const isBookingCodeRateAvailable = bookingCode + const isBookingCodeRateAvailable = booking.bookingCode ? hotels.some( (hotel) => hotel.availability.bookingCode && @@ -77,31 +69,33 @@ export default async function SelectHotelPage( ) : false - const { hotelsTrackingData, pageTrackingData } = getTracking( - params.lang, - false, + const arrivalDate = new Date(booking.fromDate) + const departureDate = new Date(booking.toDate) + + const { hotelsTrackingData, pageTrackingData } = getSelectHotelTracking({ + rooms: booking.rooms, + lang: params.lang, + pageId: "select-hotel", + pageName: "hotelreservation|select-hotel", + siteSections: "hotelreservation|select-hotel", arrivalDate, departureDate, - adultsInRoom, - childrenInRoom, - hotels?.length ?? 0, - booking.hotelId, - noOfRooms, - hotels?.[0]?.hotel.address.country, - hotels?.[0]?.hotel.address.city, - cityIdentifier, - bookingCode, + hotelsResult: hotels?.length ?? 0, + searchTerm: booking.hotelId, + country: hotels?.[0]?.hotel.address.country, + hotelCity: hotels?.[0]?.hotel.address.city, + bookingCode: booking.bookingCode, isBookingCodeRateAvailable, - redemption, - isRedemptionAvailability - ) + isRedemption: redemption, + isRedemptionAvailable: isRedemptionAvailability, + }) const mapHref = selectHotelMap(params.lang) const suspenseKey = stringify(searchParams) return ( <> hotel.availability.bookingCode) : false - const { hotelsTrackingData, pageTrackingData } = getTracking( + const { hotelsTrackingData, pageTrackingData } = getSelectHotelTracking({ lang, - !!isAlternativeFor, - !!isAlternativeHotels, + pageId: isAlternativeFor ? "alternative-hotels" : "select-hotel", + pageName: isAlternativeHotels + ? "hotelreservation|alternative-hotels|mapview" + : "hotelreservation|select-hotel|mapview", + siteSections: isAlternativeHotels + ? "hotelreservation|altervative-hotels|mapview" + : "hotelreservation|select-hotel|mapview", arrivalDate, departureDate, - adultsInRoom, - childrenInRoom, - hotels.length, - booking.hotelId, - noOfRooms, - hotels?.[0]?.hotel.address.country, - hotels?.[0]?.hotel.address.city, - cityIdentifier, - bookingCode, + rooms: booking.rooms, + hotelsResult: hotels.length, + searchTerm: isAlternativeFor ? booking.hotelId : cityIdentifier, + country: hotels?.[0]?.hotel.address.country, + hotelCity: hotels?.[0]?.hotel.address.city, + bookingCode: booking.bookingCode, isBookingCodeRateAvailable, - redemption, - isRedemptionAvailability - ) + isRedemption: redemption, + isRedemptionAvailable: isRedemptionAvailability, + }) return ( <> @@ -106,7 +104,7 @@ export async function SelectHotelMapContainer({ hotels={hotels} filterList={filterList} cityCoordinates={cityCoordinates} - bookingCode={bookingCode ?? ""} + bookingCode={booking.bookingCode} isBookingCodeRateAvailable={isBookingCodeRateAvailable} isAlternativeHotels={isAlternativeHotels} /> diff --git a/apps/scandic-web/components/HotelReservation/SelectHotel/SelectHotelMap/tracking.ts b/apps/scandic-web/components/HotelReservation/SelectHotel/SelectHotelMap/tracking.ts deleted file mode 100644 index 593ec78c7..000000000 --- a/apps/scandic-web/components/HotelReservation/SelectHotel/SelectHotelMap/tracking.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { differenceInCalendarDays, format, isWeekend } from "date-fns" - -import { ChildBedMapEnum } from "@scandic-hotels/trpc/enums/childBedMapEnum" - -import type { Lang } from "@scandic-hotels/common/constants/language" - -import { - TrackingChannelEnum, - type TrackingSDKHotelInfo, - type TrackingSDKPageData, -} from "@/types/components/tracking" -import type { ChildrenInRoom } from "@/utils/hotelSearchDetails" - -export function getTracking( - lang: Lang, - isAlternativeFor: boolean, - isAlternativeHotels: boolean, - arrivalDate: Date, - departureDate: Date, - adultsInRoom: number[], - childrenInRoom: ChildrenInRoom, - hotelsResult: number, - hotelId: string | undefined, - noOfRooms: number, - country: string | undefined, - hotelCity: string | undefined, - paramCity: string | undefined, - bookingCode?: string, - isBookingCodeRateAvailable?: boolean, - isRedemption?: boolean, - isRedemptionAvailability?: boolean -) { - const pageTrackingData: TrackingSDKPageData = { - channel: TrackingChannelEnum["hotelreservation"], - domainLanguage: lang, - pageId: isAlternativeFor ? "alternative-hotels" : "select-hotel", - pageName: isAlternativeHotels - ? "hotelreservation|alternative-hotels|mapview" - : "hotelreservation|select-hotel|mapview", - pageType: "bookinghotelsmapviewpage", - siteSections: isAlternativeHotels - ? "hotelreservation|altervative-hotels|mapview" - : "hotelreservation|select-hotel|mapview", - siteVersion: "new-web", - } - - const hotelsTrackingData: TrackingSDKHotelInfo = { - ageOfChildren: childrenInRoom - ?.map((c) => c?.map((k) => k.age).join(",") ?? "") - .join("|"), - arrivalDate: format(arrivalDate, "yyyy-MM-dd"), - availableResults: hotelsResult, - bookingCode: bookingCode ? bookingCode : "n/a", - bookingCodeAvailability: isBookingCodeRateAvailable ? "true" : "false", - 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), - leadTime: differenceInCalendarDays(arrivalDate, new Date()), - noOfAdults: adultsInRoom.join(","), - noOfChildren: childrenInRoom?.map((kids) => kids?.length ?? 0).join(","), - noOfRooms, - region: hotelCity, - rewardNight: isRedemption ? "yes" : "no", - rewardNightAvailability: isRedemptionAvailability ? "true" : "false", - searchTerm: isAlternativeFor ? hotelId : (paramCity as string), - searchType: "destination", - } - - return { - hotelsTrackingData, - pageTrackingData, - } -} diff --git a/apps/scandic-web/components/HotelReservation/SelectHotel/tracking.ts b/apps/scandic-web/components/HotelReservation/SelectHotel/tracking.ts index 66a340fb6..e26706985 100644 --- a/apps/scandic-web/components/HotelReservation/SelectHotel/tracking.ts +++ b/apps/scandic-web/components/HotelReservation/SelectHotel/tracking.ts @@ -4,6 +4,7 @@ import { ChildBedMapEnum } from "@scandic-hotels/trpc/enums/childBedMapEnum" import type { Lang } from "@scandic-hotels/common/constants/language" +import type { SelectHotelBooking } from "@/types/components/hotelReservation/selectHotel/selectHotel" import { TrackingChannelEnum, type TrackingSDKHotelInfo, @@ -11,44 +12,68 @@ import { } from "@/types/components/tracking" import type { ChildrenInRoom } from "@/utils/hotelSearchDetails" -export function getTracking( - lang: Lang, - isAlternativeFor: boolean, - arrivalDate: Date, - departureDate: Date, - adultsInRoom: number[], - childrenInRoom: ChildrenInRoom, - hotelsResult: number, - hotelId: string | undefined, - noOfRooms: number, - country: string | undefined, - hotelCity: string | undefined, - paramCity: string | undefined, - bookingCode?: string, +type SelectHotelTrackingInput = { + lang: Lang + pageId: string + pageName: string + siteSections: string + arrivalDate: Date + departureDate: Date + rooms: SelectHotelBooking["rooms"] + hotelsResult: number + country: string | undefined + hotelCity: string | undefined + bookingCode?: string + searchTerm?: string + isBookingCodeRateAvailable?: boolean + isRedemption?: boolean + isRedemptionAvailable?: boolean +} + +export function getSelectHotelTracking({ + lang, + pageId, + pageName, + siteSections, + arrivalDate, + departureDate, + rooms, + hotelsResult, + country, + hotelCity, + searchTerm, + bookingCode, isBookingCodeRateAvailable = false, - isRedemption?: boolean, - isRedemptionAvailable = false -) { + isRedemption = false, + isRedemptionAvailable = false, +}: SelectHotelTrackingInput) { const pageTrackingData: TrackingSDKPageData = { channel: TrackingChannelEnum["hotelreservation"], domainLanguage: lang, - pageId: isAlternativeFor ? "alternative-hotels" : "select-hotel", - pageName: isAlternativeFor - ? "hotelreservation|alternative-hotels" - : "hotelreservation|select-hotel", + pageId, + pageName, pageType: "bookinghotelspage", - siteSections: isAlternativeFor - ? "hotelreservation|alternative-hotels" - : "hotelreservation|select-hotel", + siteSections, 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"), availableResults: hotelsResult, + bookingCode: bookingCode ?? "n/a", + bookingCodeAvailability: bookingCode + ? isBookingCodeRateAvailable.toString() + : undefined, bookingTypeofDay: isWeekend(arrivalDate) ? "weekend" : "weekday", childBedPreference: childrenInRoom ?.map((c) => c?.map((k) => ChildBedMapEnum[k.bed]).join(",") ?? "") @@ -59,16 +84,12 @@ export function getTracking( leadTime: differenceInCalendarDays(arrivalDate, new Date()), noOfAdults: adultsInRoom.join(","), noOfChildren: childrenInRoom?.map((kids) => kids?.length ?? 0).join(","), - noOfRooms, + noOfRooms: rooms?.length ?? 0, region: hotelCity, - searchTerm: isAlternativeFor ? hotelId : (paramCity as string), - searchType: "destination", - bookingCode: bookingCode ?? "n/a", - bookingCodeAvailability: bookingCode - ? isBookingCodeRateAvailable.toString() - : undefined, rewardNight: isRedemption ? "yes" : "no", rewardNightAvailability: isRedemptionAvailable.toString(), + searchTerm, + searchType: "destination", } return { diff --git a/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/index.tsx b/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/index.tsx index 70e8926ba..7dc0f8afe 100644 --- a/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/index.tsx +++ b/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/index.tsx @@ -13,24 +13,16 @@ import TrackingSDK from "@/components/TrackingSDK" import useLang from "@/hooks/useLang" import { getValidDates } from "../getValidDates" -import { getTracking } from "./tracking" - -import type { ChildrenInRoom } from "@/utils/hotelSearchDetails" +import { getSelectRateTracking } from "./tracking" export default function Tracking({ - adultsInRoom, - childrenInRoom, hotelId, hotelName, - noOfRooms, country, city, }: { - adultsInRoom: number[] - childrenInRoom: ChildrenInRoom hotelId: string hotelName: string - noOfRooms: number country: string city: string }) { @@ -47,22 +39,19 @@ export default function Tracking({ const arrivalDate = fromDate.toDate() const departureDate = toDate.toDate() - const { hotelsTrackingData, pageTrackingData } = getTracking( + const { hotelsTrackingData, pageTrackingData } = getSelectRateTracking({ lang, arrivalDate, departureDate, - adultsInRoom, - childrenInRoom, hotelId, hotelName, - noOfRooms, country, - city, + hotelCity: city, paramCity, bookingCode, - searchType === SEARCH_TYPE_REDEMPTION, - rooms - ) + isRedemption: searchType === SEARCH_TYPE_REDEMPTION, + rooms, + }) return ( diff --git a/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/tracking.ts b/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/tracking.ts index e1b1bff95..01c535cb9 100644 --- a/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/tracking.ts +++ b/apps/scandic-web/components/HotelReservation/SelectRate/Tracking/tracking.ts @@ -13,22 +13,33 @@ import { } from "@/types/components/tracking" import type { ChildrenInRoom } from "@/utils/hotelSearchDetails" -export function getTracking( - lang: Lang, - arrivalDate: Date, - departureDate: Date, - adultsInRoom: number[], - childrenInRoom: ChildrenInRoom, - hotelId: string, - hotelName: string, - noOfRooms: number, - country: string | undefined, - hotelCity: string | undefined, - paramCity: string | undefined, - bookingCode?: string, - isRedemption?: boolean, +type SelectRateTrackingInput = { + lang: Lang + arrivalDate: Date + departureDate: Date + hotelId: string + hotelName: string + country: string | undefined + hotelCity: string | undefined + paramCity: string | undefined + bookingCode?: string + isRedemption?: boolean rooms?: Room[] -) { +} + +export function getSelectRateTracking({ + lang, + arrivalDate, + departureDate, + hotelId, + hotelName, + country, + hotelCity, + paramCity, + bookingCode, + isRedemption = false, + rooms = [], +}: SelectRateTrackingInput) { const pageTrackingData: TrackingSDKPageData = { channel: TrackingChannelEnum.hotelreservation, domainLanguage: lang, @@ -39,6 +50,13 @@ export function getTracking( 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(",") ?? "") @@ -55,7 +73,7 @@ export function getTracking( leadTime: differenceInCalendarDays(arrivalDate, new Date()), noOfAdults: adultsInRoom.join(","), noOfChildren: childrenInRoom?.map((kids) => kids?.length ?? 0).join(","), - noOfRooms, + noOfRooms: rooms?.length ?? 0, region: hotelCity, searchTerm: paramCity ?? hotelName, searchType: "hotel", diff --git a/apps/scandic-web/components/HotelReservation/SelectRate/index.tsx b/apps/scandic-web/components/HotelReservation/SelectRate/index.tsx index 9c1e240f5..fa792b372 100644 --- a/apps/scandic-web/components/HotelReservation/SelectRate/index.tsx +++ b/apps/scandic-web/components/HotelReservation/SelectRate/index.tsx @@ -28,11 +28,8 @@ export default async function SelectRatePage({ return notFound() } - const { adultsInRoom, childrenInRoom, hotel, noOfRooms, bookingCode } = - searchDetails - const hotelData = await getHotel({ - hotelId: hotel.id, + hotelId: searchDetails.hotel.id, isCardOnlyPayment: false, language: lang, }) @@ -42,10 +39,14 @@ export default async function SelectRatePage({ } let isInValidFNF = false - if (bookingCode && FamilyAndFriendsCodes.includes(bookingCode)) { + if ( + booking.bookingCode && + FamilyAndFriendsCodes.includes(booking.bookingCode) + ) { const cookieStore = await cookies() isInValidFNF = cookieStore.get("sc")?.value !== "1" } + return ( <> @@ -61,11 +62,8 @@ export default async function SelectRatePage({ )} diff --git a/apps/scandic-web/utils/hotelSearchDetails.ts b/apps/scandic-web/utils/hotelSearchDetails.ts index 0f4ef703e..3ea5f43a5 100644 --- a/apps/scandic-web/utils/hotelSearchDetails.ts +++ b/apps/scandic-web/utils/hotelSearchDetails.ts @@ -2,7 +2,6 @@ import { notFound } from "next/navigation" import { safeTry } from "@scandic-hotels/common/utils/safeTry" import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking" -import { generateChildrenString } from "@scandic-hotels/trpc/routers/hotels/helpers" import { type HotelLocation, isHotelLocation, @@ -15,17 +14,11 @@ import type { BookingSearchType } from "@scandic-hotels/booking-flow/searchType" import type { Child } from "@scandic-hotels/trpc/types/child" export type ChildrenInRoom = (Child[] | null)[] | null -export type ChildrenInRoomString = (string | null)[] | null interface HotelSearchDetails { - adultsInRoom: number[] - bookingCode?: string - childrenInRoom: ChildrenInRoom - childrenInRoomString: ChildrenInRoomString city: Location | null cityIdentifier: string | undefined hotel: HotelLocation | null - noOfRooms: number redemption?: boolean } @@ -37,7 +30,6 @@ export async function getHotelSearchDetails( adults: number childrenInRoom?: Child[] }[] - bookingCode?: string searchType?: BookingSearchType }, isAlternativeHotels?: boolean @@ -76,30 +68,10 @@ export async function getHotelSearchDetails( if (!city && !hotel) return notFound() if (isAlternativeHotels && (!city || !hotel)) return notFound() - let adultsInRoom: number[] = [] - let childrenInRoom: ChildrenInRoom = null - let childrenInRoomString: ChildrenInRoomString = null - - const { rooms } = params - - if (rooms?.length) { - adultsInRoom = rooms.map((room) => room.adults ?? 0) - - childrenInRoom = rooms.map((room) => room.childrenInRoom ?? null) - childrenInRoomString = rooms.map((room) => - room.childrenInRoom ? generateChildrenString(room.childrenInRoom) : null - ) - } - return { - adultsInRoom, - bookingCode: params.bookingCode ?? undefined, - childrenInRoom, - childrenInRoomString, city, cityIdentifier, hotel, - noOfRooms: rooms?.length ?? 0, redemption: params.searchType === SEARCH_TYPE_REDEMPTION, } }