Skip to content

Commit

Permalink
add events
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Dobkowski committed Dec 12, 2024
1 parent 3397f0c commit 02a9510
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
12 changes: 3 additions & 9 deletions src/features/analytics/hooks/useGoogleAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ export const useGoogleAnalytics: UseGoogleAnalytics = () => {
const [trackingId, setTrackingId] = useState<string>("");

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}`);
};
Expand Down
6 changes: 3 additions & 3 deletions src/features/booking/hooks/useBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookingQueryResult, BookingQueryVariables>(
GET_BOOKING,
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/features/booking/hooks/useConfirmBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}, []);

Expand Down
6 changes: 3 additions & 3 deletions src/features/booking/hooks/useDeleteBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CancelBookingMutationResult, CancelBookingMutationVariables>(CANCEL_BOOKING, {
context: {
headers: {
Expand All @@ -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;
};
6 changes: 3 additions & 3 deletions src/features/service/components/ServiceHeaders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/features/service/hooks/useBookSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BookSlotMutationRespons, BookSlotMutationVariables>(
BOOK_SLOT,
Expand All @@ -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`,
Expand All @@ -45,7 +45,7 @@ export const useBookSlot = () => {
}),
);
}
}, [data?.bookingCreate.bookingId, navigate, PAGES, urlSearchParams]);
}, [data?.bookingCreate.bookingId, navigate, PAGES, urlSearchParams, sendEvent]);

useEffect(() => {
if (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/features/service/hooks/useRescheduleBooking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`,
Expand All @@ -47,7 +47,7 @@ export const useRescheduleBooking = () => {
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data]);
}, [data, sendEvent]);

useEffect(() => {
if (error) {
Expand Down

0 comments on commit 02a9510

Please sign in to comment.