Skip to content

Commit

Permalink
Added simple firebase events tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed May 28, 2023
1 parent dedc655 commit 203efe0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 7 additions & 0 deletions web/components/post/CreatePostView/hooks.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -37,6 +42,7 @@ export const useCreatePost = () => {
});

if (data) {
logEvent(analytics, 'post_created', { userId: user?.uid });
await router.push('/');

notifications.show({
Expand All @@ -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',
Expand Down
8 changes: 5 additions & 3 deletions web/components/ui/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions web/lib/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAnalytics } from 'firebase/analytics';
import { initializeApp, getApps, getApp, FirebaseApp } from 'firebase/app';
import { browserLocalPersistence, getAuth } from 'firebase/auth';

Expand All @@ -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);

0 comments on commit 203efe0

Please sign in to comment.