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

5
  • add inside componentDidMount lifecycle method Commented Jan 30, 2020 at 5:24
  • You should put it in your index.html file. Commented Jan 30, 2020 at 5: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. Commented Jan 30, 2020 at 6:38
  • There is an index.html file for every React project under the public folder. Commented Dec 11, 2020 at 9:48
  • Does this answer your question? Adding Google Analytics to React Commented Apr 6, 2023 at 15:07

1 Answer 1

-1

You can use React-helmet package to Manipulate Document Head

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.