Skip to content

Commit 8e96995

Browse files
committed
analytics and theme fix
1 parent a922a3b commit 8e96995

File tree

4 files changed

+202
-176
lines changed

4 files changed

+202
-176
lines changed

apps/codingcatdev/src/lib/client/firebase.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { browser } from '$app/environment';
2+
13
import { toastStore } from '@codingcatdev/blackcatui';
24
import { initializeApp, getApps, FirebaseError } from 'firebase/app';
35
import { getAuth, setPersistence, browserSessionPersistence, signInWithEmailAndPassword, signInWithPopup, type AuthProvider, type Auth, createUserWithEmailAndPassword } from 'firebase/auth';
46
import { getFirestore, collection, doc, addDoc, onSnapshot, Firestore } from 'firebase/firestore';
57
import { httpsCallable, getFunctions, type Functions } from 'firebase/functions';
8+
import { getAnalytics, type Analytics, logEvent, type AnalyticsCallOptions } from "firebase/analytics";
69

710
import { env } from '$env/dynamic/public';
811

@@ -20,8 +23,10 @@ let app = getApps().at(0);
2023
let auth: Auth;
2124
let db: Firestore;
2225
let functions: Functions;
26+
let analytics: Analytics;
2327

2428
if (!app &&
29+
browser &&
2530
firebaseConfig.apiKey &&
2631
firebaseConfig.authDomain &&
2732
firebaseConfig.projectId &&
@@ -37,6 +42,7 @@ if (!app &&
3742
setPersistence(auth, browserSessionPersistence);
3843
db = getFirestore(app);
3944
functions = getFunctions(app);
45+
analytics = getAnalytics(app);
4046
}
4147

4248
/* AUTH */
@@ -114,4 +120,15 @@ export const openStripePortal = async () => {
114120
returnUrl: window.location.href,
115121
}) as { data: { url: string } };
116122
window.location.assign(data.url);
123+
}
124+
125+
/* Analytics */
126+
export const analyticsLogPageView = async (eventParams?: {
127+
page_title?: string;
128+
page_location?: string;
129+
page_path?: string;
130+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
131+
[key: string]: any;
132+
}, options?: AnalyticsCallOptions) => {
133+
logEvent(analytics, "page_view", eventParams, options)
117134
}

0 commit comments

Comments
 (0)