From 25ae3682107d227b37d9d4ede2f4f96c2360be37 Mon Sep 17 00:00:00 2001 From: Tobias Johansson Date: Tue, 6 May 2025 11:15:52 +0000 Subject: [PATCH] Merged in fix/SW-1353-and-SW-2497-tracking-fixes (pull request #1977) fix: tracking fixes SW-1353 and SW-2497 * fix: tracking fixes * fix: remove console log and rename variable Approved-by: Michael Zetterberg Approved-by: Arvid Norlin --- .../BookingConfirmation/Tracking/index.tsx | 28 +++++++++++-------- .../BookingConfirmation/index.tsx | 2 +- .../EnterDetails/Tracking/tracking.ts | 24 ++++++++++------ apps/scandic-web/stores/tracking.ts | 25 +++++++++++------ 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/apps/scandic-web/components/HotelReservation/BookingConfirmation/Tracking/index.tsx b/apps/scandic-web/components/HotelReservation/BookingConfirmation/Tracking/index.tsx index 31561c8fb..a0a422066 100644 --- a/apps/scandic-web/components/HotelReservation/BookingConfirmation/Tracking/index.tsx +++ b/apps/scandic-web/components/HotelReservation/BookingConfirmation/Tracking/index.tsx @@ -14,26 +14,24 @@ import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmat export default function Tracking({ bookingConfirmation, + refId, }: { bookingConfirmation: BookingConfirmation + refId: string }) { const lang = useLang() const bookingRooms = useBookingConfirmationStore((state) => state.rooms) - const [hasLoadedBookingConfirmation] = useState(() => { + const [loadedBookingConfirmationRefId] = useState(() => { if (typeof window !== "undefined") { - return sessionStorage.getItem("hasLoadedBookingConfirmation") + return sessionStorage.getItem("loadedBookingConfirmationRefId") } return null }) useEffect(() => { - sessionStorage.setItem("hasLoadedBookingConfirmation", "true") - - return () => { - sessionStorage.removeItem("hasLoadedBookingConfirmation") - } - }, []) + sessionStorage.setItem("loadedBookingConfirmationRefId", refId) + }, [refId]) if (!bookingRooms.every(Boolean)) { return null @@ -52,9 +50,17 @@ export default function Tracking({ return ( ) } diff --git a/apps/scandic-web/components/HotelReservation/BookingConfirmation/index.tsx b/apps/scandic-web/components/HotelReservation/BookingConfirmation/index.tsx index eb26a3cac..689ad2495 100644 --- a/apps/scandic-web/components/HotelReservation/BookingConfirmation/index.tsx +++ b/apps/scandic-web/components/HotelReservation/BookingConfirmation/index.tsx @@ -100,7 +100,7 @@ export default async function BookingConfirmation({ - + ) } diff --git a/apps/scandic-web/components/HotelReservation/EnterDetails/Tracking/tracking.ts b/apps/scandic-web/components/HotelReservation/EnterDetails/Tracking/tracking.ts index b61ae7a34..69e5a70c1 100644 --- a/apps/scandic-web/components/HotelReservation/EnterDetails/Tracking/tracking.ts +++ b/apps/scandic-web/components/HotelReservation/EnterDetails/Tracking/tracking.ts @@ -94,7 +94,7 @@ export function getTracking( .join("|"), country: hotel?.address.country, departureDate: format(departureDate, "yyyy-MM-dd"), - discount: rooms.reduce((total, room, idx) => { + discount: storedRooms.reduce((total, { room }, idx) => { const isMainRoom = idx === 0 if ( hasMemberPrice(room.roomRate) && @@ -184,8 +184,8 @@ export function getTracking( rewardNight: booking.searchType === REDEMPTION ? "yes" : "no", rewardNightAvailability: booking.searchType === REDEMPTION ? "true" : "false", - roomPrice: calcTotalRoomPrice(rooms, isMember), - totalPrice: calcTotalPrice(rooms, isMember), + roomPrice: calcTotalRoomPrice(storedRooms, isMember), + totalPrice: calcTotalPrice(storedRooms, isMember), points: // @ts-expect-error - redemption object doesn't exist error rooms.find((room) => "redemption" in room.roomRate)?.roomRate.redemption @@ -286,17 +286,23 @@ function hasBreakfast(entry: RoomEntry): entry is RoomEntry & { ) } -function calcTotalPrice(rooms: Room[], isMember: boolean) { +function calcTotalPrice(rooms: RoomState[], isMember: boolean) { const totalRoomPrice = calcTotalRoomPrice(rooms, isMember) - const totalPackageSum = rooms.reduce((total, room) => { - const packageSum = sumPackages(room.packages) - return (total += packageSum.price ?? 0) + const totalPackageSum = rooms.reduce((total, { room }) => { + if (room.breakfast) { + total += Number(room.breakfast.localPrice.totalPrice) * room.adults + } + if (room.roomFeatures?.length) { + const packageSum = sumPackages(room.roomFeatures) + total += packageSum.price + } + return total }, 0) return totalRoomPrice + totalPackageSum } -function calcTotalRoomPrice(rooms: Room[], isMember: boolean) { - return rooms.reduce((total, room, idx) => { +function calcTotalRoomPrice(rooms: RoomState[], isMember: boolean) { + return rooms.reduce((total, { room }, idx) => { // When it comes special rates, only redemption has additional price and that should be added // other special rates like voucher, corporateCheck should be added as 0 according to Priyanka if ("redemption" in room.roomRate) { diff --git a/apps/scandic-web/stores/tracking.ts b/apps/scandic-web/stores/tracking.ts index c3d57ec85..baf08b081 100644 --- a/apps/scandic-web/stores/tracking.ts +++ b/apps/scandic-web/stores/tracking.ts @@ -1,12 +1,15 @@ "use client" -import isEqual from "fast-deep-equal" import { create } from "zustand" import { convertSearchParamsToObj, searchParamsToRecord } from "@/utils/url" +import { checkIsSameBooking } from "./enter-details/helpers" + import type { ReadonlyURLSearchParams } from "next/navigation" +import type { SelectRateSearchParams } from "@/types/components/hotelReservation/selectRate/selectRate" + interface TrackingStoreState { initialStartTime: number setInitialPageLoadTime: (time: number) => void @@ -78,14 +81,20 @@ const useTrackingStore = create((set, get) => ({ if (!currentPath?.match(/^\/(da|de|en|fi|no|sv)\/(hotelreservation)/)) return false - const previousParamsObject = convertSearchParamsToObj( - searchParamsToRecord(previousParams) - ) - const currentParamsObject = convertSearchParamsToObj( - searchParamsToRecord(currentParams) - ) + const previousParamsObject = + convertSearchParamsToObj( + searchParamsToRecord(previousParams) + ) + const currentParamsObject = + convertSearchParamsToObj( + searchParamsToRecord(currentParams) + ) - return !isEqual(previousParamsObject, currentParamsObject) + const isSameBooking = checkIsSameBooking( + previousParamsObject, + currentParamsObject + ) + return !isSameBooking }, }))