A
A
Andrey Okhotnikov2019-07-03 10:18:09
React
Andrey Okhotnikov, 2019-07-03 10:18:09

How to add analytics script in React?

How to add a view script in React so that it works on every page? Or is it necessary to execute it in ComponentDidMount?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-07-03
@tsepen

Analytics are usually added to HTML.
You can use a specialized library, for ga this is react-ga .
If you need to log page transitions, then using the example of react-router and react-ga, you can do, for example, like this:

const history = createBrowserHistory();

history.listen(location => {
  ReactGA.pageview(location.pathname + location.search);
});

const root = (
<Router history={history}>
  <App />
</Router>
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question