diff --git a/web/components/post/CreatePostView/hooks.ts b/web/components/post/CreatePostView/hooks.ts index 6d8b19a..8739db4 100644 --- a/web/components/post/CreatePostView/hooks.ts +++ b/web/components/post/CreatePostView/hooks.ts @@ -1,10 +1,15 @@ import { useForm, hasLength } from '@mantine/form'; import { notifications } from '@mantine/notifications'; import { IconCheck } from '@tabler/icons'; +import { logEvent } from 'firebase/analytics'; import { useCreatePostMutation } from 'generated/client'; import { useRouter } from 'next/router'; +import { useAuthState } from 'react-firebase-hooks/auth'; +import { analytics, auth } from 'web/lib/firebase'; export const useCreatePost = () => { + const [user] = useAuthState(auth); + const router = useRouter(); const [createPost, { loading }] = useCreatePostMutation(); @@ -37,6 +42,7 @@ export const useCreatePost = () => { }); if (data) { + logEvent(analytics, 'post_created', { userId: user?.uid }); await router.push('/'); notifications.show({ @@ -48,6 +54,7 @@ export const useCreatePost = () => { } if (errors) { + logEvent(analytics, 'post_created_error', { userId: user?.uid }); notifications.show({ message: errors[0].message, color: 'red', diff --git a/web/components/ui/AppHeader.tsx b/web/components/ui/AppHeader.tsx index ccc4d28..2216ed8 100644 --- a/web/components/ui/AppHeader.tsx +++ b/web/components/ui/AppHeader.tsx @@ -17,11 +17,12 @@ import { IconSun, IconTextPlus, } from '@tabler/icons'; +import { logEvent } from 'firebase/analytics'; import Link from 'next/link'; import { useRouter } from 'next/router'; import React, { useEffect } from 'react'; import { useAuthState, useSignInWithGoogle } from 'react-firebase-hooks/auth'; -import { auth } from 'web/lib/firebase'; +import { analytics, auth } from 'web/lib/firebase'; export const AppHeader = () => { const [user] = useAuthState(auth); @@ -111,8 +112,9 @@ export const AppHeader = () => { sx={{ flex: 1 }} variant="outline" onClick={() => { - // eslint-disable-next-line @typescript-eslint/no-floating-promises -- no need to await - signInWithGoogle(); + signInWithGoogle() + .then(() => logEvent(analytics, 'user_login')) + .catch(console.error); }} > Sign in with Google diff --git a/web/lib/firebase.ts b/web/lib/firebase.ts index d10f4f3..1e8d019 100644 --- a/web/lib/firebase.ts +++ b/web/lib/firebase.ts @@ -1,3 +1,4 @@ +import { getAnalytics } from 'firebase/analytics'; import { initializeApp, getApps, getApp, FirebaseApp } from 'firebase/app'; import { browserLocalPersistence, getAuth } from 'firebase/auth'; @@ -21,6 +22,7 @@ if (getApps().length > 0) { export const app = fireApp; export const auth = getAuth(app); +export const analytics = getAnalytics(app); // eslint-disable-next-line @typescript-eslint/no-floating-promises -- This is intentional auth.setPersistence(browserLocalPersistence);