There are a couple of analytics tools you could either copy (bad idea) or integrate (better idea). They are fairly easy to integrate so don't worry.
- Google Analytics: Google product so you can expect fast load times and definitely a large community. This is definitely a must. You can get realtime info on users on your site, which pages they are viewing, how they got there, bounce rates, even ecommerce (check if people have converted and how much you've earned), what device they are on, the OS, country, etc.
- HotJar: Another analytics tool that offers cool tools such as heatmaps and integration of other popular analytics (or user feedback tools). You can have heatmaps with Google Analytics, but I think it will need some sort of add-on.
Now, if you want to create a tracking tool from scratch, there are a couple of things you can do. As @EdwinLambregts said in the comments section, you can start by tracking clicks. This method can also tell you how far down the page the user went (you can check the onhover event of the button, since people at times hover over links/buttons as they progress down the page).
Of course before this you'll need a method of getting the data:
One more thing, if you use Firebase Database you can setup live or real time data much more easily. Since it can update your dashboard (if you create when) when things change in the database tree.