File: PerformanceDataProcessor.php

package info (click to toggle)
matomo 5.5.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 73,596 kB
  • sloc: php: 231,041; javascript: 102,286; python: 202; xml: 189; sh: 172; makefile: 20; sql: 10
file content (110 lines) | stat: -rw-r--r-- 3,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\PagePerformance\Tracker;

use Piwik\Common;
use Piwik\Log;
use Piwik\Plugin\Dimension\ActionDimension;
use Piwik\Plugins\PagePerformance\Columns\TimeDomCompletion;
use Piwik\Plugins\PagePerformance\Columns\TimeDomProcessing;
use Piwik\Plugins\PagePerformance\Columns\TimeNetwork;
use Piwik\Plugins\PagePerformance\Columns\TimeServer;
use Piwik\Plugins\PagePerformance\Columns\TimeOnLoad;
use Piwik\Plugins\PagePerformance\Columns\TimeTransfer;
use Piwik\Tracker;
use Piwik\Tracker\Action;
use Piwik\Tracker\ActionPageview;
use Piwik\Tracker\Model;
use Piwik\Tracker\Request;
use Piwik\Tracker\RequestProcessor;
use Piwik\Tracker\Visit\VisitProperties;

/**
 * Handles tracker requests containing performance data.
 */
class PerformanceDataProcessor extends RequestProcessor
{
    public function recordLogs(VisitProperties $visitProperties, Request $request)
    {
        /** @var null|Action $action */
        $action = $request->getMetadata('Actions', 'action');

        // update performance metrics only for non pageview requests
        if ($action && $action instanceof ActionPageview) {
            return;
        }

        $pageViewId = $request->getParam('pv_id');
        $idVisit    = $visitProperties->getProperty('idvisit');

        // ignore requests that can't be associated with an existing page view of a visitor
        if (empty($pageViewId) || empty($idVisit)) {
            return;
        }

        /** @var ActionDimension[] $performanceDimensions */
        $performanceDimensions = [
            new TimeNetwork(),
            new TimeServer(),
            new TimeTransfer(),
            new TimeDomProcessing(),
            new TimeDomCompletion(),
            new TimeOnLoad(),
        ];

        $valuesToUpdate = [];

        foreach ($performanceDimensions as $performanceDimension) {
            $paramValue = $request->getParam($performanceDimension->getRequestParam());
            if ($paramValue > -1) {
                $valuesToUpdate[$performanceDimension->getColumnName()] = $paramValue;
            }
        }

        if (empty($valuesToUpdate)) {
            return; // no values to update given with the request
        }

        $idLinkVa = $this->getPageViewId($idVisit, $pageViewId);

        // ignore requests
        if (empty($idLinkVa)) {
            return;
        }

        Log::info('Updating page performance metrics of page view with id ' . $pageViewId);

        $model = new Model();
        $model->updateAction($idLinkVa, $valuesToUpdate);
    }

    protected function getPageViewId($idVisit, $pageViewId)
    {
        $query = sprintf(
            'SELECT idlink_va FROM %1$s LEFT JOIN %2$s ON idaction_url = idaction WHERE idvisit = ? AND idpageview = ? AND %2$s.type = ?',
            Common::prefixTable('log_link_visit_action'),
            Common::prefixTable('log_action')
        );

        $idLinkVa = Tracker::getDatabase()->fetchOne($query, [$idVisit, $pageViewId, Action::TYPE_PAGE_URL]);

        if (!empty($idLinkVa)) {
            return $idLinkVa;
        }

        $query = sprintf(
            'SELECT idlink_va FROM %1$s LEFT JOIN %2$s ON idaction_name = idaction WHERE idvisit = ? AND idpageview = ? AND %2$s.type = ?',
            Common::prefixTable('log_link_visit_action'),
            Common::prefixTable('log_action')
        );

        return Tracker::getDatabase()->fetchOne($query, [$idVisit, $pageViewId, Action::TYPE_PAGE_TITLE]);
    }
}