From 02a9510dc22debc128175c77b0d5f566664fd89a Mon Sep 17 00:00:00 2001 From: Daniel Dobkowski Date: Thu, 12 Dec 2024 21:18:10 +0100 Subject: [PATCH] add events --- src/features/analytics/hooks/useGoogleAnalytics.ts | 12 +++--------- src/features/booking/hooks/useBooking.ts | 6 +++--- src/features/booking/hooks/useConfirmBooking.ts | 4 ++-- src/features/booking/hooks/useDeleteBooking.ts | 6 +++--- src/features/service/components/ServiceHeaders.tsx | 6 +++--- src/features/service/hooks/useBookSlot.ts | 6 +++--- src/features/service/hooks/useRescheduleBooking.ts | 6 +++--- 7 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/features/analytics/hooks/useGoogleAnalytics.ts b/src/features/analytics/hooks/useGoogleAnalytics.ts index 035e271..9da51d6 100644 --- a/src/features/analytics/hooks/useGoogleAnalytics.ts +++ b/src/features/analytics/hooks/useGoogleAnalytics.ts @@ -6,26 +6,20 @@ export const useGoogleAnalytics: UseGoogleAnalytics = () => { const [trackingId, setTrackingId] = useState(""); const init = (id: string) => { - if (!id) { - throw new Error("Google Analytics tracking ID is required"); - } + if (!id) return; ReactGA.initialize(id); setTrackingId(id); console.log(`Google Analytics initialized with tracking ID: ${id}`); }; const sendEvent = (event: GoogleAnalyticsEvent) => { - if (!trackingId) { - throw new Error("Google Analytics is not initialized"); - } + if (!trackingId) return; ReactGA.event(event); console.log(`Google Analytics event sent: ${JSON.stringify(event)}`); }; const send = (path: any) => { - if (!trackingId) { - throw new Error("Google Analytics is not initialized"); - } + if (!trackingId) return; ReactGA.send(path); console.log(`Google Analytics pageview sent: ${path}`); }; diff --git a/src/features/booking/hooks/useBooking.ts b/src/features/booking/hooks/useBooking.ts index 0366ccb..959013d 100644 --- a/src/features/booking/hooks/useBooking.ts +++ b/src/features/booking/hooks/useBooking.ts @@ -17,7 +17,7 @@ export const useBookingState = (bookingId: string) => { const navigate = useNavigate(); const setBooking = useSetRecoilState(bookingAtom); const setServiceLoader = useSetRecoilState(loaderAtom(LOADERS.BOOKING)); - const { send } = useContext(AnalyticsContext); + const { sendEvent } = useContext(AnalyticsContext); const { loading, data, error, startPolling, stopPolling } = useQuery( GET_BOOKING, @@ -44,9 +44,9 @@ export const useBookingState = (bookingId: string) => { if (data && data.booking) { const { booking } = data; setBooking({ ...booking }); - send({ event: "booking", action: "view" }); + sendEvent({ event: "booking", action: "view" }); } - }, [data, setBooking, send]); + }, [data, setBooking, sendEvent]); useEffect(() => { if (error || data?.booking === null) { diff --git a/src/features/booking/hooks/useConfirmBooking.ts b/src/features/booking/hooks/useConfirmBooking.ts index cb2d07f..54d1205 100644 --- a/src/features/booking/hooks/useConfirmBooking.ts +++ b/src/features/booking/hooks/useConfirmBooking.ts @@ -11,7 +11,7 @@ import { CONFIRM_BOOKING } from "../api/mutations/mutations"; export const useConfirmBooking = (bookingId: string) => { const navigate = useNavigate(); const { PAGES } = useIsEmbeddedPage(); - const { send } = useContext(AnalyticsContext); + const { sendEvent } = useContext(AnalyticsContext); const [confirmBooking, { data, loading, error }] = useMutation< ConfirmBookingMutationResult, @@ -30,7 +30,7 @@ export const useConfirmBooking = (bookingId: string) => { useEffect(() => { confirmBooking(); - send({ event: "booking", action: "confirm" }); + sendEvent({ event: "booking", action: "confirm" }); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/src/features/booking/hooks/useDeleteBooking.ts b/src/features/booking/hooks/useDeleteBooking.ts index 31bc2b8..d48a45b 100644 --- a/src/features/booking/hooks/useDeleteBooking.ts +++ b/src/features/booking/hooks/useDeleteBooking.ts @@ -8,7 +8,7 @@ import { CANCEL_BOOKING } from "../api/mutations/mutations"; export const useDeleteBooking = () => { const { id } = useParams<{ id: string }>(); - const { send } = useContext(AnalyticsContext); + const { sendEvent } = useContext(AnalyticsContext); const [bookSlotMutation] = useMutation(CANCEL_BOOKING, { context: { headers: { @@ -21,8 +21,8 @@ export const useDeleteBooking = () => { const deleteBooking = () => id && bookSlotMutation({ variables: { bookingId: id } }); useEffect(() => { - send({ event: "booking", action: "delete" }); - }, [send]); + sendEvent({ event: "booking", action: "delete" }); + }, [sendEvent]); return deleteBooking; }; diff --git a/src/features/service/components/ServiceHeaders.tsx b/src/features/service/components/ServiceHeaders.tsx index 840dfe3..99640ad 100644 --- a/src/features/service/components/ServiceHeaders.tsx +++ b/src/features/service/components/ServiceHeaders.tsx @@ -8,14 +8,14 @@ import { headerSelector } from "state/selectors/headerSelector"; const ServiceHeaders = () => { const icon = useRecoilValue(headerSelector)?.logoUrl; const service = useRecoilValue(serviceAtom); - const { init, send } = useContext(AnalyticsContext); + const { init, sendEvent } = useContext(AnalyticsContext); useEffect(() => { if (service?.project?.googleTagId) { init(service.project.googleTagId); - send({ event: "pageview" }); + sendEvent({ event: "pageview" }); } - }, [init, service?.project?.googleTagId]); + }, [init, service?.project?.googleTagId, sendEvent]); if (service === undefined) return null; diff --git a/src/features/service/hooks/useBookSlot.ts b/src/features/service/hooks/useBookSlot.ts index 498d315..94672bf 100644 --- a/src/features/service/hooks/useBookSlot.ts +++ b/src/features/service/hooks/useBookSlot.ts @@ -18,7 +18,7 @@ export const useBookSlot = () => { const { PAGES } = useIsEmbeddedPage(); const [searchParams] = useSearchParams(); const urlSearchParams = Object.fromEntries(searchParams.entries()); - const { send } = useContext(AnalyticsContext); + const { sendEvent } = useContext(AnalyticsContext); const [bookSlotMutation, { data, loading, error }] = useMutation( BOOK_SLOT, @@ -34,7 +34,7 @@ export const useBookSlot = () => { useEffect(() => { if (data?.bookingCreate.bookingId) { - send({ event: "booking", action: "create" }); + sendEvent({ event: "booking", action: "create" }); navigate( getPath({ url: `${PAGES.BOOKING}:query`, @@ -45,7 +45,7 @@ export const useBookSlot = () => { }), ); } - }, [data?.bookingCreate.bookingId, navigate, PAGES, urlSearchParams]); + }, [data?.bookingCreate.bookingId, navigate, PAGES, urlSearchParams, sendEvent]); useEffect(() => { if (error) { diff --git a/src/features/service/hooks/useRescheduleBooking.ts b/src/features/service/hooks/useRescheduleBooking.ts index 6fbb531..37b2569 100644 --- a/src/features/service/hooks/useRescheduleBooking.ts +++ b/src/features/service/hooks/useRescheduleBooking.ts @@ -18,7 +18,7 @@ export const useRescheduleBooking = () => { const { PAGES } = useIsEmbeddedPage(); const [searchParams] = useSearchParams(); const urlSearchParams = Object.fromEntries(searchParams.entries()); - const { send } = useContext(AnalyticsContext); + const { sendEvent } = useContext(AnalyticsContext); const [rescheduleBookingMutation, { data, loading, error }] = useMutation< RescheduleBookingResponse, @@ -35,7 +35,7 @@ export const useRescheduleBooking = () => { useEffect(() => { if (data?.bookingReschedule.bookingId) { setSelectedSlot(""); - send({ event: "booking", action: "reschedule" }); + sendEvent({ event: "booking", action: "reschedule" }); navigate( getPath({ url: `${PAGES.BOOKING}:query`, @@ -47,7 +47,7 @@ export const useRescheduleBooking = () => { ); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [data]); + }, [data, sendEvent]); useEffect(() => { if (error) {