File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 11import { initializeApp } from "firebase/app" ;
22import { Firestore , getFirestore } from "firebase/firestore" ;
33import { Auth , getAuth } from "firebase/auth" ;
4+ import { Analytics , getAnalytics } from "firebase/analytics" ;
45
56let auth : Auth | undefined ;
67let db : Firestore | undefined ;
8+ let analytics : Analytics | undefined ;
79
810try {
911 const firebaseConfig = {
2022
2123 auth = getAuth ( app ) ;
2224 db = getFirestore ( app ) ;
25+ if ( typeof window !== "undefined" ) analytics = getAnalytics ( app ) ;
2326} catch ( error ) {
2427 console . log (
2528 Error (
2831 ) ;
2932}
3033
31- export { auth , db } ;
34+ export { auth , db , analytics } ;
Original file line number Diff line number Diff line change 1+ import { analytics } from "@/config/firebase" ;
12import { AuthProvider } from "@/context/AuthContext" ;
23import { SnippngContextProvider } from "@/context/SnippngEditorContext" ;
34import { ToastProvider } from "@/context/ToastContext" ;
45import "@/styles/globals.css" ;
6+ import { logEvent } from "firebase/analytics" ;
57import type { AppProps } from "next/app" ;
68import Head from "next/head" ;
9+ import { useRouter } from "next/router" ;
10+ import { useEffect } from "react" ;
711
812export default function App ( { Component, pageProps } : AppProps ) {
13+ const router = useRouter ( ) ;
14+
15+ useEffect ( ( ) => {
16+ if ( analytics && process . env . NODE_ENV === "production" ) {
17+ const _logEvent = ( path : string ) => {
18+ if ( ! analytics ) return ;
19+ logEvent ( analytics , "page_view" , {
20+ screen_name : path ,
21+ } ) ;
22+ } ;
23+ router . events . on ( "routeChangeComplete" , ( path ) => {
24+ _logEvent ( path ) ;
25+ } ) ;
26+
27+ _logEvent ( window . location . pathname ) ;
28+ return ( ) => {
29+ router . events . off ( "routeChangeComplete" , _logEvent ) ;
30+ } ;
31+ }
32+ } , [ router . events ] ) ;
33+
934 return (
1035 < >
1136 < Head >
You can’t perform that action at this time.
0 commit comments