PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.6.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.6.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / DataArray.php
matomo / app / core Last commit date
API 7 months ago Access 7 months ago Application 1 year ago Archive 7 months ago ArchiveProcessor 7 months ago Archiver 2 years ago AssetManager 7 months ago Auth 7 months ago Category 7 months ago Changes 7 months ago CliMulti 1 year ago Columns 7 months ago Concurrency 7 months ago Config 7 months ago Container 7 months ago CronArchive 7 months ago DataAccess 7 months ago DataFiles 2 years ago DataTable 7 months ago Db 7 months ago DeviceDetector 1 year ago Email 2 years ago Exception 1 year ago Http 1 year ago Intl 7 months ago Log 2 years ago Mail 1 year ago Measurable 7 months ago Menu 7 months ago Metrics 7 months ago Notification 7 months ago Period 7 months ago Plugin 7 months ago Policy 7 months ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 1 year ago Request 7 months ago Scheduler 7 months ago Segment 7 months ago Session 7 months ago Settings 7 months ago Tracker 7 months ago Translation 1 year ago Twig 1 year ago UpdateCheck 7 months ago Updater 7 months ago Updates 7 months ago Validators 1 year ago View 7 months ago ViewDataTable 7 months ago Visualization 1 year ago Widget 1 year ago .htaccess 2 years ago Access.php 7 months ago Archive.php 7 months ago ArchiveProcessor.php 7 months ago AssetManager.php 1 year ago Auth.php 7 months ago AuthResult.php 7 months ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 1 year ago CliMulti.php 7 months ago Common.php 7 months ago Config.php 1 year ago Console.php 1 year ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 7 months ago DI.php 1 year ago DataArray.php 1 year ago DataTable.php 7 months ago Date.php 7 months ago Db.php 1 year ago DbHelper.php 7 months ago Development.php 1 year ago ErrorHandler.php 7 months ago EventDispatcher.php 1 year ago ExceptionHandler.php 7 months ago FileIntegrity.php 1 year ago Filechecks.php 1 year ago Filesystem.php 1 year ago FrontController.php 7 months ago Http.php 7 months ago IP.php 1 year ago Log.php 7 months ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 7 months ago NoAccessException.php 2 years ago Nonce.php 7 months ago Notification.php 7 months ago NumberFormatter.php 1 year ago Option.php 1 year ago Period.php 1 year ago Piwik.php 7 months ago Plugin.php 1 year ago Process.php 1 year ago Profiler.php 7 months ago ProxyHeaders.php 2 years ago ProxyHttp.php 1 year ago QuickForm2.php 1 year ago RankingQuery.php 7 months ago ReportRenderer.php 1 year ago Request.php 1 year ago Segment.php 7 months ago Sequence.php 7 months ago Session.php 1 year ago SettingsPiwik.php 1 year ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 7 months ago SiteContentDetector.php 1 year ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 2 years ago Tracker.php 1 year ago Twig.php 7 months ago Unzip.php 1 year ago UpdateCheck.php 1 year ago Updater.php 1 year ago UpdaterErrorException.php 2 years ago Updates.php 1 year ago Url.php 7 months ago UrlHelper.php 1 year ago Version.php 7 months ago View.php 7 months ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 7 months ago
DataArray.php
335 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik;
10
11 use Exception;
12 use Piwik\DataTable\Filter\EnrichRecordWithGoalMetricSums;
13 use Piwik\Tracker\GoalManager;
14 /**
15 * The DataArray is a data structure used to aggregate datasets,
16 * ie. sum arrays made of rows made of columns,
17 * data from the logs is stored in a DataArray before being converted in a DataTable
18 *
19 */
20 class DataArray
21 {
22 protected $data = array();
23 protected $dataTwoLevels = array();
24 public function __construct($data = array(), $dataArrayByLabel = array())
25 {
26 $this->data = $data;
27 $this->dataTwoLevels = $dataArrayByLabel;
28 }
29 /**
30 * This returns the actual raw data array
31 *
32 * @return array
33 */
34 public function &getDataArray()
35 {
36 return $this->data;
37 }
38 public function getDataArrayWithTwoLevels()
39 {
40 return $this->dataTwoLevels;
41 }
42 public function sumMetricsVisits($label, $row)
43 {
44 if (!isset($this->data[$label])) {
45 $this->data[$label] = static::makeEmptyRow();
46 }
47 $this->doSumVisitsMetrics($row, $this->data[$label]);
48 }
49 /**
50 * Returns an empty row containing default metrics
51 *
52 * @return array
53 */
54 public static function makeEmptyRow()
55 {
56 return array(\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS => 0, \Piwik\Metrics::INDEX_NB_VISITS => 0, \Piwik\Metrics::INDEX_NB_ACTIONS => 0, \Piwik\Metrics::INDEX_NB_USERS => 0, \Piwik\Metrics::INDEX_MAX_ACTIONS => 0, \Piwik\Metrics::INDEX_SUM_VISIT_LENGTH => 0, \Piwik\Metrics::INDEX_BOUNCE_COUNT => 0, \Piwik\Metrics::INDEX_NB_VISITS_CONVERTED => 0);
57 }
58 /**
59 * Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference
60 * The rows are php arrays Name => value
61 *
62 * @param array $newRowToAdd
63 * @param array $oldRowToUpdate
64 * @param bool $onlyMetricsAvailableInActionsTable
65 *
66 * @return void
67 */
68 protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate)
69 {
70 // Pre 1.2 format: string indexed rows are returned from the DB
71 // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
72 if (!isset($newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS])) {
73 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
74 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
75 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
76 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_USERS] += $newRowToAdd['nb_users'];
77 $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS] = (float) max($newRowToAdd['max_actions'], $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS]);
78 $oldRowToUpdate[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd['sum_visit_length'];
79 $oldRowToUpdate[\Piwik\Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd['bounce_count'];
80 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd['nb_visits_converted'];
81 return;
82 }
83 // Edge case fail safe
84 if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS])) {
85 return;
86 }
87 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS];
88 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_ACTIONS];
89 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS];
90 // In case the existing Row had no action metrics (eg. Custom Variable XYZ with "visit" scope)
91 // but the new Row has action metrics (eg. same Custom Variable XYZ this time with a "page" scope)
92 if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS])) {
93 $toZero = array(\Piwik\Metrics::INDEX_NB_USERS, \Piwik\Metrics::INDEX_MAX_ACTIONS, \Piwik\Metrics::INDEX_SUM_VISIT_LENGTH, \Piwik\Metrics::INDEX_BOUNCE_COUNT, \Piwik\Metrics::INDEX_NB_VISITS_CONVERTED);
94 foreach ($toZero as $metric) {
95 $oldRowToUpdate[$metric] = 0;
96 }
97 }
98 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_USERS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_USERS];
99 $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS] = (float) max($newRowToAdd[\Piwik\Metrics::INDEX_MAX_ACTIONS], $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS]);
100 $oldRowToUpdate[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH];
101 $oldRowToUpdate[\Piwik\Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd[\Piwik\Metrics::INDEX_BOUNCE_COUNT];
102 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED];
103 }
104 /**
105 * Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference
106 * The rows are php arrays Name => value
107 *
108 * @param array $newRowToAdd
109 * @param array $oldRowToUpdate
110 * @param bool $onlyMetricsAvailableInActionsTable
111 *
112 * @return void
113 */
114 protected function doSumActionsMetrics($newRowToAdd, &$oldRowToUpdate)
115 {
116 // Pre 1.2 format: string indexed rows are returned from the DB
117 // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
118 if (!isset($newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS])) {
119 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
120 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
121 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
122 return;
123 }
124 // Edge case fail safe
125 if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS])) {
126 return;
127 }
128 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS];
129 if (array_key_exists(\Piwik\Metrics::INDEX_NB_ACTIONS, $newRowToAdd)) {
130 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_ACTIONS];
131 }
132 if (array_key_exists(\Piwik\Metrics::INDEX_PAGE_NB_HITS, $newRowToAdd)) {
133 if (!array_key_exists(\Piwik\Metrics::INDEX_PAGE_NB_HITS, $oldRowToUpdate)) {
134 $oldRowToUpdate[\Piwik\Metrics::INDEX_PAGE_NB_HITS] = 0;
135 }
136 $oldRowToUpdate[\Piwik\Metrics::INDEX_PAGE_NB_HITS] += $newRowToAdd[\Piwik\Metrics::INDEX_PAGE_NB_HITS];
137 }
138 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS];
139 }
140 public function sumMetricsGoals($label, $row)
141 {
142 $idGoal = $row['idgoal'];
143 if (!isset($this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal])) {
144 $this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal);
145 }
146 $this->doSumGoalsMetrics($row, $this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal]);
147 }
148 /**
149 * @param $idGoal
150 * @return array
151 */
152 protected static function makeEmptyGoalRow($idGoal)
153 {
154 if ($idGoal > GoalManager::IDGOAL_ORDER) {
155 return array(\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS => 0, \Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0, \Piwik\Metrics::INDEX_GOAL_REVENUE => 0);
156 }
157 if ($idGoal == GoalManager::IDGOAL_ORDER) {
158 return array(\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS => 0, \Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0, \Piwik\Metrics::INDEX_GOAL_REVENUE => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0);
159 }
160 // idGoal == GoalManager::IDGOAL_CART
161 return array(\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS => 0, \Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0, \Piwik\Metrics::INDEX_GOAL_REVENUE => 0, \Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0);
162 }
163 /**
164 *
165 * @param $newRowToAdd
166 * @param $oldRowToUpdate
167 */
168 protected function doSumGoalsMetrics($newRowToAdd, &$oldRowToUpdate)
169 {
170 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS];
171 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED];
172 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_REVENUE] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_REVENUE];
173 // Cart & Order
174 if (isset($oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS])) {
175 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS];
176 // Order only
177 if (isset($oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL])) {
178 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL];
179 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX];
180 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING];
181 $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT];
182 }
183 }
184 }
185 public function sumMetricsActions($label, $row)
186 {
187 if (!isset($this->data[$label])) {
188 $this->data[$label] = static::makeEmptyActionRow();
189 }
190 $this->doSumActionsMetrics($row, $this->data[$label]);
191 }
192 protected static function makeEmptyActionRow()
193 {
194 return array(\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS => 0, \Piwik\Metrics::INDEX_NB_VISITS => 0, \Piwik\Metrics::INDEX_NB_ACTIONS => 0);
195 }
196 public function sumMetricsEvents($label, $row)
197 {
198 if (!isset($this->data[$label])) {
199 $this->data[$label] = static::makeEmptyEventRow();
200 }
201 $this->doSumEventsMetrics($row, $this->data[$label]);
202 }
203 protected static function makeEmptyEventRow()
204 {
205 return array(\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS => 0, \Piwik\Metrics::INDEX_NB_VISITS => 0, \Piwik\Metrics::INDEX_EVENT_NB_HITS => 0, \Piwik\Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE => 0, \Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE => 0, \Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE => \false, \Piwik\Metrics::INDEX_EVENT_MAX_EVENT_VALUE => 0);
206 }
207 public const EVENT_VALUE_PRECISION = 2;
208 /**
209 * @param array $newRowToAdd
210 * @param array $oldRowToUpdate
211 * @return void
212 */
213 protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate)
214 {
215 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS];
216 $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS];
217 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_NB_HITS];
218 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE];
219 $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE], static::EVENT_VALUE_PRECISION);
220 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE];
221 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = round(max(0, $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MAX_EVENT_VALUE]), static::EVENT_VALUE_PRECISION);
222 // Update minimum only if it is set
223 if ($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== \false && $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== null) {
224 if ($oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] === \false) {
225 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE], static::EVENT_VALUE_PRECISION);
226 } else {
227 $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round(min($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE]), static::EVENT_VALUE_PRECISION);
228 }
229 }
230 }
231 /**
232 * Generic function that will sum all columns of the given row, at the specified label's row.
233 *
234 * @param $label
235 * @param $row
236 * @throws Exception if the the data row contains non numeric values
237 */
238 public function sumMetrics($label, $row)
239 {
240 foreach ($row as $columnName => $columnValue) {
241 if (empty($columnValue)) {
242 continue;
243 }
244 if (empty($this->data[$label][$columnName])) {
245 $this->data[$label][$columnName] = 0;
246 }
247 if (!is_numeric($columnValue)) {
248 throw new Exception("DataArray->sumMetricsPivot expects rows of numeric values, non numeric found: " . var_export($columnValue, \true) . " for column {$columnName}");
249 }
250 $this->data[$label][$columnName] += $columnValue;
251 }
252 }
253 public function sumMetricsVisitsPivot($parentLabel, $label, $row)
254 {
255 if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
256 $this->dataTwoLevels[$parentLabel][$label] = static::makeEmptyRow();
257 }
258 $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
259 }
260 public function sumMetricsGoalsPivot($parentLabel, $label, $row)
261 {
262 $idGoal = $row['idgoal'];
263 if (!isset($this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal])) {
264 $this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal);
265 }
266 $this->doSumGoalsMetrics($row, $this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal]);
267 }
268 public function sumMetricsActionsPivot($parentLabel, $label, $row)
269 {
270 if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
271 $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyActionRow();
272 }
273 $this->doSumActionsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
274 }
275 public function sumMetricsEventsPivot($parentLabel, $label, $row)
276 {
277 if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
278 $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyEventRow();
279 }
280 $this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
281 }
282 public function setRowColumnPivot($parentLabel, $label, $column, $value)
283 {
284 $this->dataTwoLevels[$parentLabel][$label][$column] = $value;
285 }
286 public function enrichMetricsWithConversions()
287 {
288 $this->enrichWithConversions($this->data);
289 foreach ($this->dataTwoLevels as &$metricsBySubLabel) {
290 $this->enrichWithConversions($metricsBySubLabel);
291 }
292 }
293 /**
294 * Given an array of stats, it will process the sum of goal conversions
295 * and sum of revenue and add it in the stats array in two new fields.
296 *
297 * @param array $data Passed by reference, two new columns
298 * will be added: total conversions, and total revenue, for all goals for this label/row
299 */
300 protected function enrichWithConversions(&$data)
301 {
302 foreach ($data as &$values) {
303 EnrichRecordWithGoalMetricSums::enrichWithConversions($values);
304 }
305 }
306 /**
307 * Returns true if the row looks like an Action metrics row
308 *
309 * @param $row
310 * @return bool
311 */
312 public static function isRowActions($row)
313 {
314 return count($row) == count(static::makeEmptyActionRow()) && isset($row[\Piwik\Metrics::INDEX_NB_ACTIONS]);
315 }
316 /**
317 * Converts array to a datatable
318 *
319 * @return \Piwik\DataTable
320 */
321 public function asDataTable()
322 {
323 $dataArray = $this->getDataArray();
324 $dataArrayTwoLevels = $this->getDataArrayWithTwoLevels();
325 $subtableByLabel = null;
326 if (!empty($dataArrayTwoLevels)) {
327 $subtableByLabel = array();
328 foreach ($dataArrayTwoLevels as $label => $subTable) {
329 $subtableByLabel[$label] = \Piwik\DataTable::makeFromIndexedArray($subTable);
330 }
331 }
332 return \Piwik\DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);
333 }
334 }
335