Skip to content

Commit

Permalink
ga updates
Browse files Browse the repository at this point in the history
  • Loading branch information
codediodeio committed May 18, 2024
1 parent f6cbb49 commit 9bd7053
Show file tree
Hide file tree
Showing 4 changed files with 13,875 additions and 16,946 deletions.
4 changes: 3 additions & 1 deletion app/stores/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable, derived } from 'svelte/store';
import { auth } from '../util/firebase';
import { GAEvent, GASetUser, auth } from '../util/firebase';
import { onAuthStateChanged } from 'firebase/auth';
import type { User } from 'firebase/auth';
import { siteData } from './data';
Expand Down Expand Up @@ -68,13 +68,15 @@ onAuthStateChanged(auth, async (fbUser) => {
unsubProgress = onSnapshot(progressRef, (snap) => {
userProgress.set(snap.data() as UserProgress);
});
GASetUser(fbUser.uid);
} else {
unsubData && unsubData();
unsubProgress && unsubProgress();
unsubSeats && unsubSeats();
userData.set(null);
userProgress.set(null);
seats.set(null);
GASetUser(null);
}
});

Expand Down
25 changes: 23 additions & 2 deletions app/util/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
signOut,
} from 'firebase/auth';
import type { UserCredential } from 'firebase/auth';
import { initializeAnalytics, logEvent } from 'firebase/analytics';
import { initializeAnalytics, logEvent, setUserId } from 'firebase/analytics';
const firebaseApp = initializeApp(config);
export const auth = getAuth(firebaseApp);

Expand All @@ -36,6 +36,10 @@ export function GAPageView() {
});
}

export function GASetUser(uid: string | null) {
setUserId(anal, uid);
}

export function GAEvent(name: string, data?: any) {
logEvent(anal, name, data);
}
Expand Down Expand Up @@ -95,21 +99,28 @@ export async function firebaseSignOut() {
}

async function loginHandler(promise: Promise<UserCredential>) {
let res: any, serverError: string;
let res: UserCredential, serverError: string;
try {
res = await promise;
modal.set(null);
toast.set({
message: 'Access granted! Logged into the mainframe!',
type: 'success',
});
GAEvent('login', {
method: res.providerId,
});
} catch (err) {
serverError = err.message;
console.error(err);
toast.set({
message: serverError,
type: 'error',
});
GAEvent('exception', {
location: 'loginHandler',
description: err.message,
});
}
return { res, serverError };
}
Expand All @@ -133,10 +144,20 @@ export async function callUserAPI<T>(data: UserAPIData): Promise<T> {
// connectFunctionsEmulator(functions, 'localhost', 5001); // DEV only

const res = await httpsCallable(functions, 'userAPI')(data);

// Capture GA event for all user initiated backend API calls
const { uid, ...rest } = data.payload;
GAEvent(data.fn, {
...rest,
});
return res.data as T;
} catch (error) {
console.log(error);
toast.set({ message: error?.message ?? 'Unknown Error. Contact hello@fireship.io for help', type: 'error' });
GAEvent('exception', {
location: 'callUserAPI',
description: error?.message,
});
}
}

Expand Down
Loading

0 comments on commit 9bd7053

Please sign in to comment.