Track time spent in application

What is the best way to measure the user’s time spent in the application?

As I don´t know when a user closes the application, it seems like the best option I have is to run a timer to count usage as long as the user has the application open? Deactivating the counter using the newly created On Hidden/On Visible action?

I see that this will be very dependent on the On Hidden/On Visible actions, and on the page visibility API. Is there any way for me to see whether users have that active or not? Can I either see that directly, or indirectly through knowing which browser my users are using?

It would be great to have some kind of an “On App Closed” action or service, if that is even possible

Does anyone use any third-party analytics tool to measure this?

Hi there.
There are 3rd party tools that could cover this, but an option is also the one you are mentioning: using on hidden/on visible to control a timer. The timer could be reset when "on visible is triggered and be saved to an object class dedicated to this purpose.
The events “on hidden/on visible” would not trigger if the browser does not support it.