diff --git a/src/components/generator/Forms/InputFormTopComponent.jsx b/src/components/generator/Forms/InputFormTopComponent.jsx index c5a2f9c..4bfd75d 100644 --- a/src/components/generator/Forms/InputFormTopComponent.jsx +++ b/src/components/generator/Forms/InputFormTopComponent.jsx @@ -17,6 +17,8 @@ import { addPinnedComponent } from "@/lib/generator/pinnedComponents"; import CourseOptions from "./Settings/CourseOptions"; import SortOptions from "./Settings/SortOptions"; +const isAnalyticsEnabled = import.meta.env.PROD; + export default function InputFormTop({ setTimetables, setSelectedDuration, @@ -171,11 +173,13 @@ export default function InputFormTop({ updateDurations(durationLabel); } - ReactGA.event({ - category: "Generator Event", - action: "Added Course", - label: `${cleanCourseCode} D${duration}`, - }); + if (isAnalyticsEnabled) { + ReactGA.event({ + category: "Generator Event", + action: "Added Course", + label: `${cleanCourseCode} D${duration}`, + }); + } }; const getDurationDates = (courseData, duration) => { diff --git a/src/lib/generator/ExportCal.js b/src/lib/generator/ExportCal.js index dc35ce5..91606e1 100644 --- a/src/lib/generator/ExportCal.js +++ b/src/lib/generator/ExportCal.js @@ -1,5 +1,14 @@ +import ReactGA from "react-ga4"; + +const isAnalyticsEnabled = import.meta.env.PROD; let cachedTimetableData; export function exportCal() { + if (isAnalyticsEnabled) { + ReactGA.event({ + category: "Generator Event", + action: "Export Timetable", + }); + } const blob = new Blob([generateICSFileData()], { type: "text/calendar" }); const url = URL.createObjectURL(blob); diff --git a/src/lib/generator/timetableGeneration/utils/UIEventsUtils.js b/src/lib/generator/timetableGeneration/utils/UIEventsUtils.js index 95faf8c..46e3803 100644 --- a/src/lib/generator/timetableGeneration/utils/UIEventsUtils.js +++ b/src/lib/generator/timetableGeneration/utils/UIEventsUtils.js @@ -1,6 +1,8 @@ import eventBus from "@/lib/eventBus"; import ReactGA from "react-ga4"; +const isAnalyticsEnabled = import.meta.env.PROD; + export const emitNoValidTimetablesFound = () => { eventBus.emit("snackbar", { message: @@ -47,10 +49,12 @@ export const emitTruncationWarning = () => { "The generated schedule results are truncated! Click the yellow '!' icon for more information!", variant: "warning", }); - ReactGA.event({ - category: "Generator Event", - action: "Truncation", - }); + if (isAnalyticsEnabled) { + ReactGA.event({ + category: "Generator Event", + action: "Truncation", + }); + } }; export const clearTruncationFlag = () => { diff --git a/src/main.jsx b/src/main.jsx index 813011a..4ef9f2e 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -11,7 +11,10 @@ import ReactGA from "react-ga4"; import "@/styles/index.css"; const App = () => { - ReactGA.initialize("G-M2NP1M6YSK"); + useEffect(() => { + if (!import.meta.env.PROD) return; + ReactGA.initialize("G-M2NP1M6YSK"); + }, []); const [mode, setMode] = useState(() => { const prefersDark = window.matchMedia( "(prefers-color-scheme: dark)", diff --git a/src/pages/GeneratorPage.jsx b/src/pages/GeneratorPage.jsx index 6be9129..efa22b3 100644 --- a/src/pages/GeneratorPage.jsx +++ b/src/pages/GeneratorPage.jsx @@ -31,11 +31,14 @@ import { import FooterComponent from "@/components/sitewide/FooterComponent"; function GeneratorPage() { - ReactGA.send({ - hitType: "pageview", - page: "Generator", - title: "Brock Visual TimeTable", - }); + useEffect(() => { + if (!import.meta.env.PROD) return; + ReactGA.send({ + hitType: "pageview", + page: "Generator", + title: "Brock Visual TimeTable", + }); + }, []); const [timetables, setTimetables] = useState([]); const [selectedDuration, setSelectedDuration] = useState(""); const [durations, setDurations] = useState([]); diff --git a/src/pages/GuidePage.jsx b/src/pages/GuidePage.jsx index 02c6b73..e4cb88a 100644 --- a/src/pages/GuidePage.jsx +++ b/src/pages/GuidePage.jsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { NavbarComponent } from "@/components/guide"; import CssBaseline from "@mui/material/CssBaseline"; import Grid from "@mui/material/Grid"; @@ -19,11 +19,14 @@ import Divider from "@mui/material/Divider"; import FooterComponent from "@/components/sitewide/FooterComponent"; function GuidePage() { - ReactGA.send({ - hitType: "pageview", - page: "Guide", - title: "Brock Visual Guide", - }); + useEffect(() => { + if (!import.meta.env.PROD) return; + ReactGA.send({ + hitType: "pageview", + page: "Guide", + title: "Brock Visual Guide", + }); + }, []); const isBelowMedium = useIsBelowMedium(); const theme = useTheme(); const [openCourseCode, setOpenCourseCode] = useState(false);