I have a developed web application in react.js and deployed on heroku platform. I would like to track no of hits to my website. How can I do that in Heroku?
I would like to achieve similar functionality to google analytics.
I have a developed web application in react.js and deployed on heroku platform. I would like to track no of hits to my website. How can I do that in Heroku?
I would like to achieve similar functionality to google analytics.
You can track your website hits using google analytics. All you need to create is google analytics account and get the tracking id. Once you get the tracking id add below code under of your public/index.html replacing TRACKING_ID with you tracking id from google analytics.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<TRACKING-ID>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<TRACKING-ID>');
</script>
I think the tool you're looking for is react-ga. As per the documentation:
npm i react-ga
Then include the following in your App.js
import ReactGA from 'react-ga';
ReactGA.initialize('<tracking_id>'); // Insert tracking_id from Google Analytics here
ReactGA.pageview(window.location.pathname + window.location.search);
Note: By default Google will give you a tracking ID beginning with 'G-...' but this react package requires a tracking ID beginning with 'UA-...' This article explains how to get a tracking ID beginning with 'UA-...'