collecting data for UX on a web application

I'm in the process of developing an application and I would like to integrate a user experience data collection tool into the application it self, so that I could evaluate the user behavior. I googled about this and I've no idea where to start on such a tool. I would like to develop my own tool, so if I anyone could guide me on a good article or a tutorial, it would be greatly appreciated.

Please forgive me if this post is not relevant to this forum. I would gladly delete it, if suggested.