title: How to capture visitor time on page
body: <p>I would like to keep track of how long visitors spend reading a page. If they tab away, or minimize the window, time should not count towards the time on page until they look at the tab again.</p>
  
  <p>I assume some combination of javascript and server side work will be necessary.  </p>
  
  <p>A couple of issues I'm struggling with:</p>
  
  <ol>
  <li>What's the best way to store this information in the database?</li>
  <li>How do I, with Javascript, capture the time on page with a reasonable degree of accuracy? Do I store events like "page loaded", "user idle", "user returned", "page unloaded", and then separately process all the events in the DB to come up with a time on page?</li>
  </ol>