Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Next.js not working with Analytics #544

Unanswered
eddyhdzg-solarx asked this question in Q&A
Discussion options

Does anyone knows how to use getAnalytics + useAnalytics with Next.js?

The error I'm getting is

ReferenceError: window is not defined

You must be logged in to vote

Replies: 1 comment

Comment options

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);
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /