Search code examples
node.jsreactjsnext.jsenvironmentdatadog

NextJs activate DatadogRum depending of env


We currently have multiple environments and we would like to init datadogRum only for production.

In the _app.tsx I tried something like this :

import { datadogRum } from '@datadog/browser-rum';

if (process.env.NODE_ENV=== 'production') {
  datadogRum.init({...});
  datadogRum.startSessionReplayRecording();
}

But this is not working because env variables are not available here ...

Do you have any work around for this ?


Solution

  • I succeed using @bwest comment.

    I used nextJs getInitialProps in order to transmit my env variable.

    And I put my datadog init code in a useEffect(() => {...} , []) hook to only execute the code once.