1+ import { browser } from '$app/environment' ;
2+
13import { toastStore } from '@codingcatdev/blackcatui' ;
24import { initializeApp , getApps , FirebaseError } from 'firebase/app' ;
35import { getAuth , setPersistence , browserSessionPersistence , signInWithEmailAndPassword , signInWithPopup , type AuthProvider , type Auth , createUserWithEmailAndPassword } from 'firebase/auth' ;
46import { getFirestore , collection , doc , addDoc , onSnapshot , Firestore } from 'firebase/firestore' ;
57import { httpsCallable , getFunctions , type Functions } from 'firebase/functions' ;
8+ import { getAnalytics , type Analytics , logEvent , type AnalyticsCallOptions } from "firebase/analytics" ;
69
710import { env } from '$env/dynamic/public' ;
811
@@ -20,8 +23,10 @@ let app = getApps().at(0);
2023let auth : Auth ;
2124let db : Firestore ;
2225let functions : Functions ;
26+ let analytics : Analytics ;
2327
2428if ( ! 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