Many webmasters decide to monitor the usage of their websites. For example, if I want to monitor how long a person stays on this page, I must include some external code that periodically sends a ping. On the receiving end, I count how many pings I got, and can calculate the length of my visitor's stay.
Tracking scripts can do much more than that. They are capable of recording how a person moves the mouse on the screen, where they click. For example, ReCaptcha uses such scripts to identify real people or bots. Scripts can record the location, operating system, screen resolution, browser version, fonts available.
All the information that is gathered by a script is sent to a service that then turns it into some form of insight. They can make a graph showing how many guests per day you get on average, at what time, from which countries. Building an analytics service is not hard .