You can use ready-made solutions:
- http://www.google.com/analytics/
- http://www.clicktale.com/
- https://segment.io/
- http://www.extrawatch.com/
- http://mouseflow.com/
- https://www.seevolution.com/
- http://clicky.com/
You can do amazing stuff with Google Analytics and its Event Tracker:
- https://developers.google.com/analytics/devguides/collection/gajs/eventTrackerGuide
- http://searchenginewatch.com/article/2287906/10-Google-Analytics-Custom-Events-That-Track-the-Untrackable
If you’re looking for a custom-made solution, you can try the following one with PHP and JavaScript:
Keep in mind that using third-party solutions is better performance-wise. Writing the coordinates of the mouse movements in a database in real time, needs a lot of resources.