Skip to content

Commit

Permalink
fix calculation of last page view
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Nov 19, 2021
1 parent e23c848 commit dfc13f7
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions plugins/Actions/VisitorDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,15 @@ public function provideActionsForVisit(&$actions, $visitorDetails)
$action['timeSpent'] = 0;

// search for next action with timeSpentRef
$nextActionId = $idx + 1;
$nextActionId = $idx;
$nextAction = null;

while (isset($actionDetails[$nextActionId])) {

if (!$this->shouldHandleAction($actionDetails[$nextActionId]) ||
!array_key_exists('timeSpentRef', $actionDetails[$nextActionId])) {
$nextActionId++;
continue;
}
do {
$nextActionId++;

$nextAction = isset($actionDetails[$nextActionId]) ? $actionDetails[$nextActionId] : null;

// Set the time spent for this action (which is the timeSpentRef of the next action)
if ($nextAction) {
$action['timeSpent'] += $nextAction['timeSpentRef'] ?? 0;
} else {

if (is_null($nextAction)) {
// Last action of a visit.
// By default, Matomo does not know how long the user stayed on the page
// If enableHeartBeatTimer() is used in piwik.js then we can find the accurate time on page for the last pageview
Expand All @@ -97,13 +88,21 @@ public function provideActionsForVisit(&$actions, $visitorDetails)
break;
}

if (!array_key_exists('timeSpentRef', $nextAction)) {
continue;
}

// Set the time spent for this action (which is the timeSpentRef of the next action)
if ($nextAction) {
$action['timeSpent'] += $nextAction['timeSpentRef'] ?? 0;
}

// sum spent time until next page view
if ($this->isPageView($nextAction)) {
break;
}

$nextActionId++;
}
} while (isset($actionDetails[$nextActionId]));

if (isset($action['timeSpent'])) {
$action['timeSpentPretty'] = $formatter->getPrettyTimeFromSeconds($action['timeSpent'], true);
Expand Down

0 comments on commit dfc13f7

Please sign in to comment.