Tracking how long it takes a user to submit a node edit form? For a quiz node, I want to track how long it takes a user to submit a node form (the time from Drupal generating the form until the time that the user presses submit), and I want to store this duration in a field on my node.
Use case
Each node of content type Quiz is a 10 question quiz.  I want to track the time that users spend filling out the node in order to track their improvement in speed over time.
I do NOT want to track the time spent on the website in general; for that, I already use Google Analytics with scroll tracking, and there is also the Time Spent module.
What I tried
On my node, I added an Integer field, Duration.  My thought was that I could calculate the interval from the timestamp when the form was generated until the time the node was saved by adding code in hook_node_presave():
  $original_time = ??? // How do I get the time when the node form was generated? 
  $current_time = \Drupal::time()-&gt;getCurrentTime();
  $duration = $current_time - $original_time;

Where I'm stuck is I don't know how to get the time when the node form was created.
I tried to add a Datetime field to my node with a Default date of Current date, but the current date is not set until the node is saved, so that doesn't work.
So, how can I track the time from generating a node edit form until the time the user presses submit?
Here is how I would do it:
/*
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_node_quiz_edit_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['field_duration']['widget'][0]['value']['#type'] = 'hidden'; // hide field
  $form['field_duration']['widget'][0]['value']['#default_value'] = time(); // set default value to start time
  array_unshift($form['actions']['submit']['#submit'], 'mymodule_calc_duration'); // adds a custom submit that runs before the default node submit
}

function mymodule_calc_duration(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $start_time = $form_state-&gt;getValue('field_duration')[0]['value']; // get default value
  $duration = time() - $start_time; // calculate duration
  $duration_value = [
    ['value' =&gt; $duration],
  ];
  $form_state-&gt;setValue('field_duration', $duration_value); // set duration value
}