-
Notifications
You must be signed in to change notification settings - Fork 408
Next.js not working with Analytics #544
Unanswered
eddyhdzg-solarx
asked this question in
Q&A
-
Does anyone knows how to use getAnalytics + useAnalytics with Next.js?
The error I'm getting is
ReferenceError: window is not defined
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Temporarily solve it with this solution from https://github.com/vercel/next.js/discussions/35773 , but I'm looking for a better way to do it.
import { useState, useEffect } from "react"; import { AnalyticsProvider as ReactFireAnalyticsProvider, useFirebaseApp, } from "reactfire"; import { getAnalytics } from "firebase/analytics"; import { CenterLoader } from "components"; function withAnalyticsProvider( WrappedComponent: React.ComponentType<React.PropsWithChildren> ) { return (props: React.PropsWithChildren) => { const [isSSR, setIsSSR] = useState(true); useEffect(() => { setIsSSR(false); }, []); if (isSSR) return <CenterLoader />; return <WrappedComponent {...props} />; }; } function AnalyticsProvider({ children }: React.PropsWithChildren) { const firebaseApp = useFirebaseApp(); const analytics = getAnalytics(firebaseApp); return ( <ReactFireAnalyticsProvider sdk={analytics}> {children} </ReactFireAnalyticsProvider> ); } export default withAnalyticsProvider(AnalyticsProvider);
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment