From 203efe08df89c29eb2c55776dcee39ebc6878eb3 Mon Sep 17 00:00:00 2001 From: Volodymyr Zakhovaiko Date: Sun, 28 May 2023 23:41:44 +0200 Subject: [PATCH] Added simple firebase events tracker --- web/components/post/CreatePostView/hooks.ts | 7 +++++++ web/components/ui/AppHeader.tsx | 8 +++++--- web/lib/firebase.ts | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) 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);