React-ga: window is not defined error

Created on 28 Jun 2018  路  3Comments  路  Source: react-ga/react-ga

Hi,

I am creating an React App using Next JS for SSR. So when added the ReactGA.initialize event is head tag for _document.js. It is throwing an error for 'window is not defined'.

A quick and prompt solution would be helpful

Most helpful comment

Can you check that window exists before calling initialize().

if (typeof window !== 'undefined') {
  ReactGA.initialize()
}

All 3 comments

Can you check that window exists before calling initialize().

if (typeof window !== 'undefined') {
  ReactGA.initialize()
}

I have the same problem. but in that case GA will not be initialized and will not work?

You most probably don't want it initializing on the server anyway. It should initialize only when the code is run on the client

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hlodver picture hlodver  路  4Comments

amcinerneynewf picture amcinerneynewf  路  4Comments

wasabia picture wasabia  路  5Comments

horaceleung picture horaceleung  路  6Comments

Robinnnnn picture Robinnnnn  路  6Comments