Adam Hughes
Adam Hughes

Reputation: 2277

react-ga console warning - [react-ga] gaTrackingID is required in initialize()

I'm trying to add React-GA to my create-react-app project (it's ejected) and I'm getting a the following console warning come up.

[react-ga] gaTrackingID is required in initialize()

I've added the following to index.js

import ReactGA from 'react-ga';
ReactGA.initialize('UA-XXXXXXXX');

Any ideas? I'm probably missing something simple.

Upvotes: 0

Views: 3148

Answers (3)

cah
cah

Reputation: 53

Posting what worked for me incase someone else runs into the same issue. For me I had to do the following:

ReactGA.initialize({ trackingId: process.env.TRACKING_ID });

Upvotes: 2

Adam Hughes
Adam Hughes

Reputation: 2277

So it turns out to be a catalogue of stupid things on my part.

  1. I don't think the way I was pulling in my .env with my GA code was working on my auto deployment. I've just hardcoded it in for now to fix it.

  2. Copying and pasting GA code for the past million years turns out to be bad for the soul. Didn't even notice that I wasn't pushing the pageview event to Google.

ReactGA.set({ page: window.location.pathname }); ReactGA.pageview(window.location.pathname);

Upvotes: 2

paqash
paqash

Reputation: 2314

That looks fine to me. Is there any chance it's imported in another place and called before it's initialized?

Upvotes: 0

Related Questions