Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix time on page calculation in visits log #18294

Merged
merged 5 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

75 changes: 50 additions & 25 deletions plugins/Actions/VisitorDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,52 +55,67 @@ public function provideActionsForVisit(&$actions, $visitorDetails)
$nextActionId = 0;
foreach ($actionDetails as $idx => &$action) {

if ($idx < $nextActionId || !$this->shouldHandleAction($action)) {
continue; // skip to next action having timeSpentRef
if ($idx < $nextActionId || !$this->isPageView($action)) {
unset($action['timeSpentRef']);
continue; // skip to next page view
}

$action['timeSpent'] = 0;

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

while (isset($actionDetails[$nextActionId]) &&
(!$this->shouldHandleAction($actionDetails[$nextActionId]) ||
!array_key_exists('timeSpentRef', $actionDetails[$nextActionId]))) {
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'];
} else {
$nextAction = isset($actionDetails[$nextActionId]) ? $actionDetails[$nextActionId] : null;

// Last action of a visit.
// By default, Piwik 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
$visitTotalTime = $visitorDetails['visitDuration'];
$timeOfLastAction = Date::factory($action['serverTimePretty'])->getTimestamp();
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
$visitTotalTime = $visitorDetails['visitDuration'];
$timeOfLastAction = Date::factory($action['serverTimePretty'])->getTimestamp();

$timeSpentOnAllActionsApartFromLastOne = ($timeOfLastAction - $visitorDetails['firstActionTimestamp']);
$timeSpentOnPage = $visitTotalTime - $timeSpentOnAllActionsApartFromLastOne;
$timeSpentOnAllActionsApartFromLastOne = ($timeOfLastAction - $visitorDetails['firstActionTimestamp']);
$timeSpentOnPage = $visitTotalTime - $timeSpentOnAllActionsApartFromLastOne;

// Safe net, we assume the time is correct when it's more than 10 seconds
if ($timeSpentOnPage > 10) {
$action['timeSpent'] = $timeSpentOnPage;
// Safe net, we assume the time is correct when it's more than 10 seconds
if ($timeSpentOnPage > 10) {
$action['timeSpent'] = $timeSpentOnPage;
}
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;
}
}

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

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

unset($action['timeSpentRef']); // not needed after timeSpent is added
unset($action['timeSpentRef']);
}

$actions = $actionDetails;
}

private function shouldHandleAction($action) {
private function shouldHandleAction($action)
{
$actionTypesToHandle = array(
Action::TYPE_PAGE_URL,
Action::TYPE_PAGE_TITLE,
Expand All @@ -113,6 +128,16 @@ private function shouldHandleAction($action) {
return in_array($action['type'], $actionTypesToHandle) || !empty($action['eventType']);
}

private function isPageView($action)
{
$pageViewTypes = array(
Action::TYPE_PAGE_URL,
Action::TYPE_PAGE_TITLE,
);

return in_array($action['type'], $pageViewTypes);
}

public function extendActionDetails(&$action, $nextAction, $visitorDetails)
{
$formatter = new Formatter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>15</pageId>
<contentName>ImageAd</contentName>
<contentPiece>Unknown</contentPiece>
Expand All @@ -57,7 +56,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>16</pageId>
<contentName>ImageAd</contentName>
<contentPiece />
Expand All @@ -79,7 +77,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>120</timeSpentRef>
<pageId>17</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad.jpg</contentPiece>
Expand All @@ -101,7 +98,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>18</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad2.jpg</contentPiece>
Expand All @@ -123,7 +119,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>19</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad.jpg</contentPiece>
Expand All @@ -145,7 +140,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>60</timeSpentRef>
<pageId>20</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -167,7 +161,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>21</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click NOW</contentPiece>
Expand All @@ -189,7 +182,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>22</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -211,7 +203,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>23</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click NOW</contentPiece>
Expand All @@ -233,7 +224,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>60</timeSpentRef>
<pageId>24</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -255,7 +245,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>30</timeSpentRef>
<pageId>25</pageId>
<contentName>Video Ad</contentName>
<contentPiece>movie.mov</contentPiece>
Expand All @@ -277,7 +266,6 @@
<pageIdAction>18</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>26</pageId>
<contentName>Video Ad</contentName>
<contentPiece>movie.mov</contentPiece>
Expand Down Expand Up @@ -414,7 +402,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>2</pageId>
<contentName>ImageAd</contentName>
<contentPiece>Unknown</contentPiece>
Expand All @@ -436,7 +423,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>3</pageId>
<contentName>ImageAd</contentName>
<contentPiece />
Expand All @@ -458,7 +444,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>120</timeSpentRef>
<pageId>4</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad.jpg</contentPiece>
Expand All @@ -480,7 +465,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>5</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad2.jpg</contentPiece>
Expand All @@ -502,7 +486,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>6</pageId>
<contentName>ImageAd</contentName>
<contentPiece>/path/ad.jpg</contentPiece>
Expand All @@ -524,7 +507,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>60</timeSpentRef>
<pageId>7</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -546,7 +528,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>8</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click NOW</contentPiece>
Expand All @@ -568,7 +549,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>9</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -590,7 +570,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>10</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click NOW</contentPiece>
Expand All @@ -612,7 +591,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>60</timeSpentRef>
<pageId>11</pageId>
<contentName>Text Ad</contentName>
<contentPiece>Click to download Piwik now</contentPiece>
Expand All @@ -634,7 +612,6 @@
<pageIdAction>3</pageIdAction>


<timeSpentRef>30</timeSpentRef>
<pageId>12</pageId>
<contentName>Video Ad</contentName>
<contentPiece>movie.mov</contentPiece>
Expand All @@ -656,7 +633,6 @@
<pageIdAction>18</pageIdAction>


<timeSpentRef>0</timeSpentRef>
<pageId>13</pageId>
<contentName>Video Ad</contentName>
<contentPiece>movie.mov</contentPiece>
Expand Down
Loading