@terrence
To set up Google Analytics with React.js, you can follow these steps:
- Create a Google Analytics account and get the tracking ID. This tracking ID is a unique code that you will need to add to your React.js application to track user interactions.
- Install the react-ga library by running the following command in your project directory:
- Import the react-ga library in your main application file (e.g., App.js) and initialize it with your tracking ID:
1
2
3
|
import ReactGA from 'react-ga';
ReactGA.initialize('YOUR_TRACKING_ID');
|
- Use the ReactGA.pageview() method to track page views in your application. You can do this by calling ReactGA.pageview() in the componentDidMount() method of your components or by using the React Router library to track page changes automatically.
- Track events in your application by using the ReactGA.event() method. For example, you can track button clicks by adding an onClick event handler to your buttons and calling ReactGA.event() with the appropriate category, action, and label parameters.
- Test your Google Analytics setup by navigating through your application and checking the Real-Time reporting section in your Google Analytics account to see if data is being tracked correctly.
By following these steps, you should be able to set up Google Analytics with your React.js application and start tracking user interactions and behavior on your website.