How do I add Google Analytics to React.js?
@clarabelle First, sign into your Google Analytics account, select an account to link, click Admin > Property > Create New Property, and generate a tracking ID.
Next, add the tracking ID to your React.js application. In the root of your application, create a file named googleAnalytics.js. Inside of this file, add the following code and replace UA-00000000-0 with the tracking ID you generated earlier.
// googleAnalytics.js
import ReactGA from 'react-ga';
ReactGA.initialize('UA-00000000-0');
ReactGA.pageview(window.location.pathname + window.location.search);
Finally, import the file you just created into your App.js file.
// App.js
import React from 'react';
import './googleAnalytics';
function App() {
return (
<div className="App">
// Your application code
</div>
);
}
export default App;