Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CustomDimensionsRequestProcessor.php « Tracker « CustomDimensions « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 87ceccd5631fcb93e69bce426f86eaea87af968c (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomDimensions\Tracker;

use Piwik\Common;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Piwik\Plugins\CustomDimensions\Dao;
use Piwik\Plugins\CustomDimensions\Dimension\Extraction;
use Piwik\Tracker\Action;
use Piwik\Tracker\Cache;
use Piwik\Tracker\Model;
use Piwik\Tracker\Request;
use Piwik\Tracker\RequestProcessor;
use Piwik\Tracker\Visit\VisitProperties;
use Piwik\Url;

/**
 * Handles tracking of custom dimensions
 */
class CustomDimensionsRequestProcessor extends RequestProcessor
{

    public function recordLogs(VisitProperties $visitProperties, Request $request)
    {
        if (!self::hasActionCustomDimensionConfiguredInSite($request)) {
            return;
        }

        $model = new Model();

        /** @var Action $action */
        $action = $request->getMetadata('Actions', 'action');

        if (!empty($action)) {
            $idLinkVisit = $action->getIdLinkVisitAction();
            $idVisit     = $visitProperties->getProperty('idvisit');
            $model->updateVisit($request->getIdSite(), $idVisit, array('last_idlink_va' => $idLinkVisit));
        }

        $lastIdLinkVa = $visitProperties->getProperty('last_idlink_va');
        $timeSpent    = $visitProperties->getProperty('time_spent_ref_action');

        if (!empty($lastIdLinkVa) && $timeSpent > 0) {
            $model->updateAction($lastIdLinkVa, array('time_spent' => $timeSpent));
        }
    }

    public static function hasActionCustomDimensionConfiguredInSite($request)
    {
        $dimensions = self::getCachedCustomDimensions($request);
        if (empty($dimensions)) {
            return false;
        }
        foreach ($dimensions as $dimension) {
            if ($dimension['scope'] == CustomDimensions::SCOPE_ACTION) {
                return true;
            }
        }
        return false;
    }

    public function onNewVisit(VisitProperties $visitProperties, Request $request)
    {
        $dimensionsToSet = $this->getCustomDimensionsInScope(CustomDimensions::SCOPE_VISIT, $request);

        foreach ($dimensionsToSet as $field => $value) {
            $visitProperties->setProperty($field, $value);
        }
    }

    public function onExistingVisit(&$valuesToUpdate, VisitProperties $visitProperties, Request $request)
    {
        $dimensionsToSet = $this->getCustomDimensionsInScope(CustomDimensions::SCOPE_VISIT, $request);

        foreach ($dimensionsToSet as $field => $value) {
            $valuesToUpdate[$field] = $value;
            $visitProperties->setProperty($field, $value);
        }
    }

    public function afterRequestProcessed(VisitProperties $visitProperties, Request $request)
    {
        $action = $request->getMetadata('Actions', 'action');

        if (empty($action) || !($action instanceof Action)) {
            return;
        }

        $dimensionsToSet = $this->getCustomDimensionsInScope(CustomDimensions::SCOPE_ACTION, $request);

        foreach ($dimensionsToSet as $field => $value) {
            $action->setCustomField($field, $value);
        }
    }

    private function getCustomDimensionsInScope($scope, Request $request)
    {
        $dimensions = self::getCachedCustomDimensions($request);
        $params = $request->getParams();

        $values = array();

        foreach ($dimensions as $dimension) {
            if ($dimension['scope'] !== $scope) {
                continue;
            }

            $field = self::buildCustomDimensionTrackingApiName($dimension);
            $dbField = Dao\LogTable::buildCustomDimensionColumnName($dimension);

            $value = Common::getRequestVar($field, '', 'string', $params);
            $hasSentEmptyString = isset($params[$field]) && $params[$field] === '';
            if ($value !== '' || $hasSentEmptyString) {
                $values[$dbField] = self::prepareValue($value);
                continue;
            }

            $extractions = $dimension['extractions'];
            if (is_array($extractions)) {
                foreach ($extractions as $extraction) {
                    if (!array_key_exists('dimension', $extraction)
                     || !array_key_exists('pattern', $extraction)
                     || empty($extraction['pattern'])) {
                        continue;
                    }

                    $extraction = new Extraction($extraction['dimension'], $extraction['pattern']);
                    $extraction->setCaseSensitive($dimension['case_sensitive']);
                    $value = $extraction->extract($request);

                    if (!isset($value) || '' === $value) {
                        continue;
                    }

                    $values[$dbField] = self::prepareValue(urldecode($value));
                    break;
                }
            }
        }

        return $values;
    }

    private static function prepareValue($value)
    {
        return Common::mb_substr(trim($value), 0, 250);
    }

    public static function buildCustomDimensionTrackingApiName($idDimensionOrDimension)
    {
        if (is_array($idDimensionOrDimension) && isset($idDimensionOrDimension['idcustomdimension'])) {
            $idDimensionOrDimension = $idDimensionOrDimension['idcustomdimension'];
        }

        $idDimensionOrDimension = (int) $idDimensionOrDimension;

        if ($idDimensionOrDimension >= 1) {
            return 'dimension' . (int) $idDimensionOrDimension;
        }
    }

    public static function getCachedCustomDimensionIndexes($scope)
    {
        $cache = Cache::getCacheGeneral();
        $key = 'custom_dimension_indexes_installed_' . $scope;

        if (empty($cache[$key])) {
            return array();
        }

        return $cache[$key];
    }

    /**
     * Get Cached Custom Dimensions during tracking. Returns only active custom dimensions.
     *
     * @param Request $request
     * @return array
     * @throws \Piwik\Exception\UnexpectedWebsiteFoundException
     */
    public static function getCachedCustomDimensions(Request $request)
    {
        $idSite = $request->getIdSite();
        $cache  = Cache::getCacheWebsiteAttributes($idSite);

        if (empty($cache['custom_dimensions'])) {
            // no custom dimensions set
            return array();
        }

        return $cache['custom_dimensions'];
    }

}