diff --git a/lib/firebase-client.ts b/lib/firebase-client.ts index 3416c2ad..a338fc4e 100644 --- a/lib/firebase-client.ts +++ b/lib/firebase-client.ts @@ -1,4 +1,5 @@ import firebase from 'firebase/app'; +import 'firebase/analytics'; export const firebaseConfig = { apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY, @@ -15,10 +16,10 @@ export const firebaseConfig = { * * See the contributor docs for more info. */ -export function initFirebase() { - if (firebase.apps.length > 0) { - return; - } - +if (!firebase.apps.length) { firebase.initializeApp(firebaseConfig); } + +const analytics = firebase.analytics; + +export { firebase, analytics }; diff --git a/pages/_app.tsx b/pages/_app.tsx index 65db73b7..555053c2 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -2,7 +2,7 @@ import Head from 'next/head'; import { AppProps } from 'next/dist/shared/lib/router/router'; import 'firebase/auth'; import AppHeader from '../components/AppHeader'; -import { initFirebase } from '../lib/firebase-client'; +import { analytics } from '../lib/firebase-client'; import { AuthProvider } from '../lib/user/AuthContext'; import '../styles/globals.css'; import '../styles/tailwind.css'; @@ -18,8 +18,8 @@ import 'react-notion-x/src/styles.css'; import 'prismjs/themes/prism-tomorrow.css'; // used for rendering equations import 'katex/dist/katex.min.css'; - -initFirebase(); +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; /** * A Wrapper for the HackPortal web app. @@ -28,6 +28,22 @@ initFirebase(); * will load into memory and never re-initialize unless the page refreshes. */ function PortalApp({ Component, pageProps }: AppProps) { + const router = useRouter(); + + useEffect(() => { + if (process.env.NODE_ENV === 'production') { + const logEvent = (url: string) => { + analytics().setCurrentScreen(url); + analytics().logEvent('screen_view'); + }; + router.events.on('routeChangeComplete', logEvent); + logEvent(window.location.pathname); + return () => { + router.events.off('routeChangeComplete', logEvent); + }; + } + }); + return ( diff --git a/pages/register.tsx b/pages/register.tsx index 6f3c04bd..73bb3879 100644 --- a/pages/register.tsx +++ b/pages/register.tsx @@ -5,7 +5,6 @@ import LoadIcon from '../components/LoadIcon'; import { useUser } from '../lib/profile/user-data'; import { RequestHelper } from '../lib/request-helper'; import { useAuthContext } from '../lib/user/AuthContext'; -import firebase from 'firebase/app'; import { Formik, Form, Field, ErrorMessage } from 'formik'; import { hackPortalConfig, formInitialValues } from '../hackportal.config'; import DisplayQuestion from '../components/registerComponents/DisplayQuestion'; diff --git a/pages/schedule/index.tsx b/pages/schedule/index.tsx index f82795fb..effba365 100644 --- a/pages/schedule/index.tsx +++ b/pages/schedule/index.tsx @@ -29,7 +29,6 @@ import PinDrop from '@material-ui/icons/PinDrop'; import ClockIcon from '@material-ui/icons/AccessTime'; import Backpack from '@material-ui/icons/LocalMall'; import Description from '@material-ui/icons/BorderColor'; -import firebase from 'firebase'; const styles = ({ palette }: Theme) => createStyles({