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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
<?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) && $request->getMetadata('CoreHome', 'isNewVisit')) {
// this logic is only for new visits because we have to create the visit to get an idvisit before we can insert
// the action and only then we can update the last_idink_va of the previously on the visit from the previously created actions.
// so flow is:
// * Create visit -> creates idVisit A
// * Create link visit action (requires idvisit) -> creates idlink_va B
// * Update visit A and set the idlink_va B
$idLinkVisit = $action->getIdLinkVisitAction();
$idVisit = $visitProperties->getProperty('idvisit');
$model->updateVisit($request->getIdSite(), $idVisit, array('last_idlink_va' => $idLinkVisit));
}
$lastIdLinkVa = $visitProperties->getProperty('last_idlink_va');
$previousIdLinkVa = $request->getMetadata('CustomDimensions', 'previous_idlink_va');
if ($previousIdLinkVa) {
// when last_idlink_va was already updated in this visit because it was existing visit... we need to get the idlink_va from previous tracking request
// it's actually not needed to store previous_idlink_va in metadata currently but figured it be better just in case the logic changes in the future
// to prevent updating the wrong idlink_va
$lastIdLinkVa = $previousIdLinkVa;
}
$timeSpent = $visitProperties->getProperty('time_spent_ref_action');
if (!empty($lastIdLinkVa) && $timeSpent > 0) {
// here we don't update the action that was created in this request but the action of the previous tracking request
// we can only know how much time was spent on the previous action when the next action is recorded.
$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);
}
$action = $request->getMetadata('Actions', 'action');
/** @var Action $action */
if (!empty($action) && $action->getIdLinkVisitAction() && self::hasActionCustomDimensionConfiguredInSite($request)) {
// when it is an existing visit, then we first create the action before recording the visit. This allows us
// to update last_idlink_va in the regular visit update
$request->setMetadata('CustomDimensions','previous_idlink_va', $visitProperties->getProperty('last_idlink_va'));
$valuesToUpdate['last_idlink_va'] = $action->getIdLinkVisitAction();
}
}
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'];
}
}
|