0

I have the following Google Analytics (gtag) code to be added in my React project. I am trying to add it in render() method. But its not working. How to resolve it. Thanks.

<script async src="https://www.googletagmanager.com/gtag/js?id=#######"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '#########');
</script>

enter image description here

alex
  • 147
  • 1
  • 4
  • 17
  • add inside componentDidMount lifecycle method – Johnson Cherian Jan 30 '20 at 05:24
  • You should put it in your `index.html` file. – Tim Jan 30 '20 at 05:33
  • There is no index.html currently in my react project. Should I create one under public folder - /public/index.html and the code snippet there. Will it work ? I am not sure if there is any other way for especially for React applications. – alex Jan 30 '20 at 06:38
  • There is an index.html file for every React project under the public folder. – Nick Dec 11 '20 at 09:48
  • Does this answer your question? [Adding Google Analytics to React](https://stackoverflow.com/questions/49279820/adding-google-analytics-to-react) – Liam Apr 06 '23 at 15:07

1 Answers1

-1

You can use React-helmet package to Manipulate Document Head