react-ga icon indicating copy to clipboard operation
react-ga copied to clipboard

TypeError: Cannot read property 'parentNode' of undefined

Open alxvallejo opened this issue 6 years ago • 4 comments

Tried following the demo file but it was overly complex. Why is it being initialized in the render method?? Shouldn't the init happen at either constructor or CDM level?

Either way, I am also facing an issue with my App.test when I run my build:

> [email protected] test /opt/atlassian/pipelines/agent/build
> react-scripts test
FAIL src/App.test.js
  ● Test suite failed to run
    TypeError: Cannot read property 'parentNode' of undefined
       6 | import ReactGA from 'react-ga';
       7 | 
    >  8 | ReactGA.initialize('UA-132839204-1');
         |         ^
       9 | ReactGA.pageview('/');
      10 | 
      11 | 

Anyone know where to properly initialize or how to avoid this error that's preventing my build?

alxvallejo avatar Jan 21 '19 18:01 alxvallejo

The demo file is kind of a full test-bed kind of example - I put all the weird scenarios that you have to run ga for and can't do tests for in there.

initialize will run the GA code that loads their libraries in the browser - react-ga is convenience functions for interacting with it, you need to use testMode option for running in tests; https://github.com/react-ga/react-ga#test-mode Many people also do testMode: process.NODE_ENV === 'test' or similar to avoid this.

SimeonC avatar Jan 28 '19 16:01 SimeonC

This is a duplicate of https://github.com/react-ga/react-ga/issues/322

dawsbot avatar Aug 23 '19 07:08 dawsbot

Many people also do testMode: process.NODE_ENV === 'test' or similar to avoid this.

Should be process.env.NODE_ENV, but other than that works a treat 😁

Thank you!

surdu avatar Oct 19 '20 15:10 surdu

Just adding a +1 for this, adding the testMode property worked for me. For anyone needing it spelled out for them, the OP should replace the initialisation line with:

ReactGA.initialize('UA-132839204-1', { testMode: process.env.NODE_ENV === 'test' });

matthewpennell avatar Oct 29 '20 10:10 matthewpennell