-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomVariablesExtended.php
More file actions
205 lines (177 loc) · 7.61 KB
/
CustomVariablesExtended.php
File metadata and controls
205 lines (177 loc) · 7.61 KB
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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link http://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CustomVariablesExtended;
use Exception;
use Piwik\Common;
use Piwik\Plugin;
use Piwik\Plugins\CustomVariablesExtended\Dao\LogTableConversion;
use Piwik\Plugins\CustomVariablesExtended\Dao\LogTableLinkVisitAction;
use Piwik\Plugins\CustomVariablesExtended\Dao\LogTableVisit;
use Piwik\Plugins\CustomVariablesExtended\Reports\GetCustomVariables;
use Piwik\Plugins\CustomVariablesExtended\Tracker\CustomVariablesExtendedRequestProcessor;
class CustomVariablesExtended extends Plugin {
public const MAX_LENGTH_VARIABLE_NAME = 200;
public const MAX_LENGTH_VARIABLE_VALUE = 1024;
public const FIRST_CUSTOM_VARIABLE_INDEX = 101;
public const LAST_CUSTOM_VARIABLE_INDEX = 120;
public const SCOPE_PAGE = 'page';
public const SCOPE_VISIT = 'visit';
public const SCOPE_CONVERSION = 'conversion';
public const SCOPE_ID_PAGE = 1;
public const SCOPE_ID_VISIT = 2;
public const SCOPE_ID_CONVERSION = 3;
public function isTrackerPlugin(): bool {
return true;
}
/**
* @return array<string, string>
*/
public function registerEvents(): array {
return [
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Dimension.addDimensions' => 'addDimensions',
'Actions.getCustomActionDimensionFieldsAndJoins' => 'provideActionDimensionFields',
'Tracker.newConversionInformation' => 'newConversionInformation',
'Goals.getReportsWithGoalMetrics' => 'getReportsWithGoalMetrics',
'Db.getTablesInstalled' => 'getTablesInstalled',
];
}
public function install(): void {
(new LogTableConversion())->install();
(new LogTableLinkVisitAction())->install();
(new LogTableVisit())->install();
}
public function uninstall(): void {
(new LogTableConversion())->uninstall();
(new LogTableLinkVisitAction())->uninstall();
(new LogTableVisit())->uninstall();
}
/**
* @param array<string> $translationKeys
*/
public function getClientSideTranslationKeys(array &$translationKeys): void {
$translationKeys[] = 'CustomVariablesExtended_CustomVariables';
$translationKeys[] = 'CustomVariablesExtended_ManageDescription';
$translationKeys[] = 'CustomVariablesExtended_ScopeX';
$translationKeys[] = 'CustomVariablesExtended_Index';
$translationKeys[] = 'CustomVariablesExtended_Usages';
$translationKeys[] = 'CustomVariablesExtended_Unused';
$translationKeys[] = 'CustomVariablesExtended_CreateNewSlot';
$translationKeys[] = 'CustomVariablesExtended_UsageDetails';
$translationKeys[] = 'CustomVariablesExtended_CurrentAvailableCustomVariables';
$translationKeys[] = 'CustomVariablesExtended_ToCreateCustomVarExecute';
$translationKeys[] = 'CustomVariablesExtended_CreatingCustomVariableTakesTime';
$translationKeys[] = 'CustomVariablesExtended_SlotsReportIsGeneratedOverTime';
$translationKeys[] = 'General_Loading';
$translationKeys[] = 'General_TrackingScopeVisit';
$translationKeys[] = 'General_TrackingScopePage';
}
/**
* @param array<string> $stylesheets
*/
public function getStylesheetFiles(&$stylesheets): void {
$stylesheets[] = 'plugins/CustomVariablesExtended/vue/src/ManageCustomVars/ManageCustomVars.less';
}
/**
* @param array<\Piwik\Columns\Dimension> $dimensions
*/
public function addDimensions(&$dimensions): void {
foreach ([self::SCOPE_VISIT, self::SCOPE_PAGE, self::SCOPE_CONVERSION] as $scope) {
for ($i = self::FIRST_CUSTOM_VARIABLE_INDEX; $i <= self::LAST_CUSTOM_VARIABLE_INDEX; $i++) {
$custom = new CustomVariableDimension();
$custom->initCustomDimension($scope, $i);
$dimensions[] = $custom;
}
}
}
/**
* @param array<string> $fields
* @param array<string> $joins
*/
public function provideActionDimensionFields(&$fields, &$joins): void {
for ($i = self::FIRST_CUSTOM_VARIABLE_INDEX; $i <= self::LAST_CUSTOM_VARIABLE_INDEX; $i++) {
$fields[] = 'cv_lva_' . $i . '.name as custom_var_k' . $i;
$fields[] = 'cv_lva_' . $i . '.value as custom_var_v' . $i;
$joins[] = 'LEFT JOIN ' . Common::prefixTable('log_custom_variable_link_va')
. ' AS cv_lva_' . $i
. ' ON log_link_visit_action.idvisit = cv_lva_' . $i . '.idvisit'
. ' AND log_link_visit_action.idlink_va = cv_lva_' . $i . '.idlink_va'
. ' AND cv_lva_' . $i . '.index = ' . $i;
}
}
/**
* @param array{idgoal: int, buster: int} $conversion
* @param array{idvisit: int, visit_last_action_time?: int} $visitInformation
* @param \Piwik\Tracker\Request $request
* @param \Piwik\Tracker\Action|null $action
*/
public function newConversionInformation(&$conversion, $visitInformation, $request, $action): void {
$processor = new CustomVariablesExtendedRequestProcessor();
$processor->onNewConversionInformation($conversion, $visitInformation, $request, $action);
}
/**
* @param array<string, mixed> $reportsWithGoals
*/
public function getReportsWithGoalMetrics(&$reportsWithGoals): void {
$report = new GetCustomVariables();
$report->prepareForGoalMetrics();
$reportToInsert = [
'category' => $report->getCategoryId(),
'name' => $report->getName(),
'module' => $report->getModule(),
'action' => $report->getAction(),
'parameters' => $report->getParameters(),
];
$newReportsWithGoals = [];
$didInsert = false;
foreach ($reportsWithGoals as $reportWithGoals) {
$newReportsWithGoals[] = $reportWithGoals;
if (is_array($reportWithGoals)
&& $reportWithGoals['module'] === 'CustomVariables') {
$newReportsWithGoals[] = $reportToInsert;
$didInsert = true;
}
}
if (!$didInsert) {
$newReportsWithGoals[] = $reportToInsert;
}
$reportsWithGoals = $newReportsWithGoals;
}
/**
* @param array<string> $allTablesInstalled
*/
public function getTablesInstalled(&$allTablesInstalled): void {
$allTablesInstalled[] = Common::prefixTable(LogTableVisit::TABLE_NAME);
$allTablesInstalled[] = Common::prefixTable(LogTableLinkVisitAction::TABLE_NAME);
$allTablesInstalled[] = Common::prefixTable(LogTableConversion::TABLE_NAME);
}
public static function scopeNameToId(string $scope): int {
switch ($scope) {
case self::SCOPE_VISIT:
return self::SCOPE_ID_VISIT;
case self::SCOPE_PAGE:
return self::SCOPE_ID_PAGE;
case self::SCOPE_CONVERSION:
return self::SCOPE_ID_CONVERSION;
}
throw new Exception('Invalid scope');
}
public static function scopeIdToName(int $scopeId): string {
switch ($scopeId) {
case self::SCOPE_ID_VISIT:
return self::SCOPE_VISIT;
case self::SCOPE_ID_PAGE:
return self::SCOPE_PAGE;
case self::SCOPE_ID_CONVERSION:
return self::SCOPE_CONVERSION;
}
throw new Exception('Invalid scope id');
}
}