Merged in fix/tracking-fixes (pull request #1930)
Fix/tracking fixes * fix: remove hotelInfo and paymentInfo when user reloads page on confirmation page * fix: clean session storage item on unmount * fix: commented out hard navigation hook * fix: update price calculation on room ancillary in tracking * fix: update discount calculation * fix: add space between fns * fix: allow useSoftNavigation to fire pageview again on same pathname * fix: prevent bedSelection and breakfastSelection from tracking twice Approved-by: Hrishikesh Vaipurkar
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"use client"
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
import { useBookingConfirmationStore } from "@/stores/booking-confirmation"
|
||||
|
||||
import TrackingSDK from "@/components/TrackingSDK"
|
||||
@@ -17,6 +19,22 @@ export default function Tracking({
|
||||
}) {
|
||||
const lang = useLang()
|
||||
const bookingRooms = useBookingConfirmationStore((state) => state.rooms)
|
||||
|
||||
const [hasLoadedBookingConfirmation] = useState(() => {
|
||||
if (typeof window !== "undefined") {
|
||||
return sessionStorage.getItem("hasLoadedBookingConfirmation")
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
sessionStorage.setItem("hasLoadedBookingConfirmation", "true")
|
||||
|
||||
return () => {
|
||||
sessionStorage.removeItem("hasLoadedBookingConfirmation")
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (!bookingRooms.every(Boolean)) {
|
||||
return null
|
||||
}
|
||||
@@ -34,8 +52,8 @@ export default function Tracking({
|
||||
return (
|
||||
<TrackingSDK
|
||||
pageData={pageTrackingData}
|
||||
hotelInfo={hotelsTrackingData}
|
||||
paymentInfo={paymentInfo}
|
||||
hotelInfo={hasLoadedBookingConfirmation ? undefined : hotelsTrackingData}
|
||||
paymentInfo={hasLoadedBookingConfirmation ? undefined : paymentInfo}
|
||||
ancillaries={ancillaries}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -57,7 +57,9 @@ export default function BedType() {
|
||||
const selectedBedType = methods.watch("bedType")
|
||||
const handleSubmit = methods.handleSubmit
|
||||
useEffect(() => {
|
||||
handleSubmit(onSubmit)()
|
||||
if (selectedBedType) {
|
||||
handleSubmit(onSubmit)()
|
||||
}
|
||||
}, [selectedBedType, handleSubmit, onSubmit])
|
||||
|
||||
return (
|
||||
|
||||
@@ -69,7 +69,9 @@ export default function Breakfast() {
|
||||
const selectedBreakfast = methods.watch("breakfast")
|
||||
const handleSubmit = methods.handleSubmit
|
||||
useEffect(() => {
|
||||
handleSubmit(onSubmit)()
|
||||
if (selectedBreakfast) {
|
||||
handleSubmit(onSubmit)()
|
||||
}
|
||||
}, [selectedBreakfast, handleSubmit, onSubmit])
|
||||
|
||||
return (
|
||||
|
||||
@@ -93,22 +93,27 @@ export function getTracking(
|
||||
.join("|"),
|
||||
country: hotel?.address.country,
|
||||
departureDate: format(departureDate, "yyyy-MM-dd"),
|
||||
discount: rooms
|
||||
.map((room, idx) => {
|
||||
const isMainRoom = idx === 0
|
||||
if (
|
||||
hasMemberPrice(room.roomRate) &&
|
||||
isMainRoom &&
|
||||
isMember &&
|
||||
hasPublicPrice(room.roomRate)
|
||||
) {
|
||||
const memberPrice = room.roomRate.member.localPrice.pricePerStay
|
||||
const publicPrice = room.roomRate.public.localPrice.pricePerStay
|
||||
return publicPrice - memberPrice
|
||||
}
|
||||
return 0
|
||||
})
|
||||
.join(","),
|
||||
discount: rooms.reduce((total, room, idx) => {
|
||||
const isMainRoom = idx === 0
|
||||
if (
|
||||
hasMemberPrice(room.roomRate) &&
|
||||
isMainRoom &&
|
||||
isMember &&
|
||||
hasPublicPrice(room.roomRate)
|
||||
) {
|
||||
const memberPrice = room.roomRate.member.localPrice.pricePerStay
|
||||
const publicPrice = room.roomRate.public.localPrice.pricePerStay
|
||||
total += publicPrice - memberPrice
|
||||
} else if (
|
||||
hasPublicPrice(room.roomRate) &&
|
||||
room.roomRate.public.localPrice.regularPricePerStay
|
||||
) {
|
||||
const publicPrice = room.roomRate.public.localPrice.pricePerStay
|
||||
const regularPrice = room.roomRate.public.localPrice.regularPricePerStay
|
||||
total += regularPrice - publicPrice
|
||||
}
|
||||
return total
|
||||
}, 0),
|
||||
duration: differenceInCalendarDays(departureDate, arrivalDate),
|
||||
hotelID: hotel?.operaId,
|
||||
leadTime: differenceInCalendarDays(arrivalDate, new Date()),
|
||||
@@ -194,19 +199,18 @@ export function getTracking(
|
||||
const ancillaries: TrackingSDKAncillaries = roomsWithPetRoom
|
||||
.slice(0, 1) // should only be one item
|
||||
.map((room) => {
|
||||
const petRoomPackage = room.packages.find(
|
||||
(p) => p.code === RoomPackageCodeEnum.PET_ROOM
|
||||
)
|
||||
return {
|
||||
hotelId: hotel.operaId,
|
||||
productId: petRoomPackage?.code!, // property is guaranteed at this point
|
||||
productUnits: roomsWithPetRoom.length,
|
||||
productPoints: 0,
|
||||
productPrice: petRoomPackage?.localPrice.totalPrice ?? 0,
|
||||
productType: "room preference",
|
||||
productName: "pet room",
|
||||
productCategory: "",
|
||||
}
|
||||
return room.packages
|
||||
.filter((p) => p.code === RoomPackageCodeEnum.PET_ROOM)
|
||||
.map((pkg) => ({
|
||||
hotelId: hotel.operaId,
|
||||
productId: pkg.code,
|
||||
productUnits: roomsWithPetRoom.length,
|
||||
productPoints: 0,
|
||||
productPrice: pkg.localPrice.totalPrice * roomsWithPetRoom.length,
|
||||
productType: "room preference",
|
||||
productName: "pet room",
|
||||
productCategory: "",
|
||||
}))[0]
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -215,6 +219,7 @@ export function getTracking(
|
||||
ancillaries,
|
||||
}
|
||||
}
|
||||
|
||||
function hasPublicPrice(
|
||||
roomRate: Product
|
||||
): roomRate is PriceProduct & { public: NonNullable<PriceProduct["public"]> } {
|
||||
@@ -223,6 +228,7 @@ function hasPublicPrice(
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function hasMemberPrice(
|
||||
roomRate: Product
|
||||
): roomRate is PriceProduct & { member: NonNullable<PriceProduct["member"]> } {
|
||||
@@ -231,6 +237,7 @@ function hasMemberPrice(
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function hasPetRoom(
|
||||
room: Room
|
||||
): room is Room & { packages: NonNullable<Room["packages"]> } {
|
||||
@@ -239,6 +246,7 @@ function hasPetRoom(
|
||||
}
|
||||
return room.packages.some((p) => p.code === RoomPackageCodeEnum.PET_ROOM)
|
||||
}
|
||||
|
||||
function calcTotalPrice(rooms: Room[], isMember: boolean) {
|
||||
const totalRoomPrice = calcTotalRoomPrice(rooms, isMember)
|
||||
const totalPackageSum = rooms.reduce((total, room) => {
|
||||
@@ -247,6 +255,7 @@ function calcTotalPrice(rooms: Room[], isMember: boolean) {
|
||||
}, 0)
|
||||
return totalRoomPrice + totalPackageSum
|
||||
}
|
||||
|
||||
function calcTotalRoomPrice(rooms: Room[], isMember: boolean) {
|
||||
return rooms.reduce((total, room, idx) => {
|
||||
// When it comes special rates, only redemption has additional price and that should be added
|
||||
|
||||
Reference in New Issue
Block a user