Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include GA in a React project

I followed the documentation and I got to this code to add Google Analytics to my React app:

import 'autotrack'

ga('create', 'MY CODE', 'auto');
ga('require', 'urlChangeTracker')
ga('send', 'pageview')

But I get the following error:

  Line 11:  'ga' is not defined  no-undef
  Line 12:  'ga' is not defined  no-undef
  Line 13:  'ga' is not defined  no-undef

How can I solve it? Will I get all the data I currently get with the original GA snippet following this setting and is it worth it to implement code splitting (I'm using Webpack).

like image 912
ocram Avatar asked Jan 03 '23 23:01

ocram


1 Answers

You get this error from your ESLint setup because you have used ga function without explicitly define it. Since ga is a global variable you will be able to remove these error by calling it with global window object.

window.ga('create', 'MY CODE', 'auto');

But I recommend you to look at react-ga library which is a better way to include Google Analytics to react applications.

And I don't see any value of implementing code splitting in this specific case.

like image 72
Tharaka Wijebandara Avatar answered Jan 08 '23 06:01

Tharaka Wijebandara