API
1 month ago
Access
3 months ago
Application
1 month ago
Archive
1 month ago
ArchiveProcessor
1 month ago
Archiver
2 years ago
AssetManager
1 month ago
Auth
6 months ago
Category
6 months ago
Changes
1 month ago
CliMulti
1 year ago
Columns
1 month ago
Concurrency
1 month ago
Config
1 month ago
Container
1 month ago
CronArchive
3 months ago
DataAccess
1 month ago
DataFiles
2 years ago
DataTable
2 weeks ago
Db
2 weeks ago
DeviceDetector
1 year ago
Email
2 years ago
Exception
4 months ago
Http
4 months ago
Intl
3 months ago
Log
2 years ago
Mail
1 year ago
Measurable
6 months ago
Menu
1 month ago
Metrics
3 months ago
Notification
6 months ago
Period
1 month ago
Plugin
2 weeks ago
Policy
1 month ago
ProfessionalServices
1 year ago
Report
1 year ago
ReportRenderer
3 months ago
Request
3 months ago
Scheduler
1 month ago
Segment
1 month ago
Session
2 weeks ago
Settings
1 month ago
Tracker
2 weeks ago
Translation
1 month ago
Twig
1 year ago
UpdateCheck
3 months ago
Updater
1 month ago
Updates
2 days ago
Validators
1 year ago
View
1 month ago
ViewDataTable
2 weeks ago
Visualization
1 year ago
Widget
1 month ago
.htaccess
2 years ago
Access.php
1 month ago
Archive.php
1 month ago
ArchiveProcessor.php
1 month ago
AssetManager.php
1 month ago
Auth.php
6 months ago
AuthResult.php
6 months ago
BaseFactory.php
2 years ago
Cache.php
2 years ago
CacheId.php
4 months ago
CliMulti.php
1 month ago
Common.php
2 weeks ago
Config.php
1 month ago
Console.php
3 months ago
Context.php
2 years ago
Cookie.php
1 year ago
CronArchive.php
1 month ago
DI.php
3 months ago
DataArray.php
1 month ago
DataTable.php
1 month ago
Date.php
1 month ago
Db.php
1 month ago
DbHelper.php
1 month ago
Development.php
1 year ago
ErrorHandler.php
6 months ago
EventDispatcher.php
1 month ago
ExceptionHandler.php
4 months ago
FileIntegrity.php
1 month ago
Filechecks.php
1 year ago
Filesystem.php
1 month ago
FrontController.php
4 months ago
Http.php
1 month ago
IP.php
1 year ago
Log.php
3 months ago
LogDeleter.php
1 year ago
Mail.php
1 year ago
Metrics.php
1 month ago
NoAccessException.php
2 years ago
Nonce.php
6 months ago
Notification.php
1 month ago
NumberFormatter.php
5 months ago
Option.php
5 months ago
Period.php
1 month ago
Piwik.php
1 month ago
Plugin.php
1 month ago
Process.php
1 month ago
Profiler.php
6 months ago
ProxyHeaders.php
4 months ago
ProxyHttp.php
5 months ago
QuickForm2.php
3 months ago
RankingQuery.php
1 month ago
ReportRenderer.php
1 month ago
Request.php
1 month ago
Segment.php
1 month ago
Sequence.php
6 months ago
Session.php
2 weeks ago
SettingsPiwik.php
1 month ago
SettingsServer.php
1 year ago
Singleton.php
2 years ago
Site.php
1 month ago
SiteContentDetector.php
1 month ago
SupportedBrowser.php
2 years ago
TCPDF.php
1 year ago
Theme.php
1 year ago
Timer.php
1 month ago
Tracker.php
1 month ago
Twig.php
1 month ago
Unzip.php
1 year ago
UpdateCheck.php
1 month ago
Updater.php
1 month ago
UpdaterErrorException.php
2 years ago
Updates.php
3 months ago
Url.php
3 months ago
UrlHelper.php
1 month ago
Version.php
2 days ago
View.php
1 month ago
bootstrap.php
1 year ago
dispatch.php
2 years ago
testMinimumPhpVersion.php
6 months ago
DataArray.php
332 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 | * |
| 65 | * @return void |
| 66 | */ |
| 67 | protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate) |
| 68 | { |
| 69 | // Pre 1.2 format: string indexed rows are returned from the DB |
| 70 | // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string |
| 71 | if (!isset($newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS])) { |
| 72 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits']; |
| 73 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions']; |
| 74 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors']; |
| 75 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_USERS] += $newRowToAdd['nb_users']; |
| 76 | $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS] = (float) max($newRowToAdd['max_actions'], $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS]); |
| 77 | $oldRowToUpdate[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd['sum_visit_length']; |
| 78 | $oldRowToUpdate[\Piwik\Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd['bounce_count']; |
| 79 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd['nb_visits_converted']; |
| 80 | return; |
| 81 | } |
| 82 | // Edge case fail safe |
| 83 | if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS])) { |
| 84 | return; |
| 85 | } |
| 86 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS]; |
| 87 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_ACTIONS]; |
| 88 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS]; |
| 89 | // In case the existing Row had no action metrics (eg. Custom Variable XYZ with "visit" scope) |
| 90 | // but the new Row has action metrics (eg. same Custom Variable XYZ this time with a "page" scope) |
| 91 | if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS])) { |
| 92 | $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); |
| 93 | foreach ($toZero as $metric) { |
| 94 | $oldRowToUpdate[$metric] = 0; |
| 95 | } |
| 96 | } |
| 97 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_USERS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_USERS]; |
| 98 | $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS] = (float) max($newRowToAdd[\Piwik\Metrics::INDEX_MAX_ACTIONS], $oldRowToUpdate[\Piwik\Metrics::INDEX_MAX_ACTIONS]); |
| 99 | $oldRowToUpdate[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd[\Piwik\Metrics::INDEX_SUM_VISIT_LENGTH]; |
| 100 | $oldRowToUpdate[\Piwik\Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd[\Piwik\Metrics::INDEX_BOUNCE_COUNT]; |
| 101 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS_CONVERTED]; |
| 102 | } |
| 103 | /** |
| 104 | * Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference |
| 105 | * The rows are php arrays Name => value |
| 106 | * |
| 107 | * @param array $newRowToAdd |
| 108 | * @param array $oldRowToUpdate |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
| 112 | protected function doSumActionsMetrics($newRowToAdd, &$oldRowToUpdate) |
| 113 | { |
| 114 | // Pre 1.2 format: string indexed rows are returned from the DB |
| 115 | // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string |
| 116 | if (!isset($newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS])) { |
| 117 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits']; |
| 118 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions']; |
| 119 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors']; |
| 120 | return; |
| 121 | } |
| 122 | // Edge case fail safe |
| 123 | if (!isset($oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS])) { |
| 124 | return; |
| 125 | } |
| 126 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS]; |
| 127 | if (array_key_exists(\Piwik\Metrics::INDEX_NB_ACTIONS, $newRowToAdd)) { |
| 128 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_ACTIONS]; |
| 129 | } |
| 130 | if (array_key_exists(\Piwik\Metrics::INDEX_PAGE_NB_HITS, $newRowToAdd)) { |
| 131 | if (!array_key_exists(\Piwik\Metrics::INDEX_PAGE_NB_HITS, $oldRowToUpdate)) { |
| 132 | $oldRowToUpdate[\Piwik\Metrics::INDEX_PAGE_NB_HITS] = 0; |
| 133 | } |
| 134 | $oldRowToUpdate[\Piwik\Metrics::INDEX_PAGE_NB_HITS] += $newRowToAdd[\Piwik\Metrics::INDEX_PAGE_NB_HITS]; |
| 135 | } |
| 136 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS]; |
| 137 | } |
| 138 | public function sumMetricsGoals($label, $row) |
| 139 | { |
| 140 | $idGoal = $row['idgoal']; |
| 141 | if (!isset($this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal])) { |
| 142 | $this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal); |
| 143 | } |
| 144 | $this->doSumGoalsMetrics($row, $this->data[$label][\Piwik\Metrics::INDEX_GOALS][$idGoal]); |
| 145 | } |
| 146 | /** |
| 147 | * @param $idGoal |
| 148 | * @return array |
| 149 | */ |
| 150 | protected static function makeEmptyGoalRow($idGoal) |
| 151 | { |
| 152 | if ($idGoal > GoalManager::IDGOAL_ORDER) { |
| 153 | return array(\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS => 0, \Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0, \Piwik\Metrics::INDEX_GOAL_REVENUE => 0); |
| 154 | } |
| 155 | if ($idGoal == GoalManager::IDGOAL_ORDER) { |
| 156 | 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); |
| 157 | } |
| 158 | // idGoal == GoalManager::IDGOAL_CART |
| 159 | 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); |
| 160 | } |
| 161 | /** |
| 162 | * @param $newRowToAdd |
| 163 | * @param $oldRowToUpdate |
| 164 | */ |
| 165 | protected function doSumGoalsMetrics($newRowToAdd, &$oldRowToUpdate) |
| 166 | { |
| 167 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_NB_CONVERSIONS]; |
| 168 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_NB_VISITS_CONVERTED]; |
| 169 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_REVENUE] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_REVENUE]; |
| 170 | // Cart & Order |
| 171 | if (isset($oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS])) { |
| 172 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_ITEMS]; |
| 173 | // Order only |
| 174 | if (isset($oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL])) { |
| 175 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL]; |
| 176 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX]; |
| 177 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING]; |
| 178 | $oldRowToUpdate[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT] += $newRowToAdd[\Piwik\Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT]; |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | public function sumMetricsActions($label, $row) |
| 183 | { |
| 184 | if (!isset($this->data[$label])) { |
| 185 | $this->data[$label] = static::makeEmptyActionRow(); |
| 186 | } |
| 187 | $this->doSumActionsMetrics($row, $this->data[$label]); |
| 188 | } |
| 189 | protected static function makeEmptyActionRow() |
| 190 | { |
| 191 | return array(\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS => 0, \Piwik\Metrics::INDEX_NB_VISITS => 0, \Piwik\Metrics::INDEX_NB_ACTIONS => 0); |
| 192 | } |
| 193 | public function sumMetricsEvents($label, $row) |
| 194 | { |
| 195 | if (!isset($this->data[$label])) { |
| 196 | $this->data[$label] = static::makeEmptyEventRow(); |
| 197 | } |
| 198 | $this->doSumEventsMetrics($row, $this->data[$label]); |
| 199 | } |
| 200 | protected static function makeEmptyEventRow() |
| 201 | { |
| 202 | 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); |
| 203 | } |
| 204 | public const EVENT_VALUE_PRECISION = 2; |
| 205 | /** |
| 206 | * @param array $newRowToAdd |
| 207 | * @param array $oldRowToUpdate |
| 208 | * @return void |
| 209 | */ |
| 210 | protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate) |
| 211 | { |
| 212 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_VISITS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_VISITS]; |
| 213 | $oldRowToUpdate[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[\Piwik\Metrics::INDEX_NB_UNIQ_VISITORS]; |
| 214 | $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_NB_HITS]; |
| 215 | $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE]; |
| 216 | $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE], static::EVENT_VALUE_PRECISION); |
| 217 | $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_SUM_EVENT_VALUE]; |
| 218 | $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); |
| 219 | // Update minimum only if it is set |
| 220 | if ($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== \false && $newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== null) { |
| 221 | if ($oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] === \false) { |
| 222 | $oldRowToUpdate[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[\Piwik\Metrics::INDEX_EVENT_MIN_EVENT_VALUE], static::EVENT_VALUE_PRECISION); |
| 223 | } else { |
| 224 | $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); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | /** |
| 229 | * Generic function that will sum all columns of the given row, at the specified label's row. |
| 230 | * |
| 231 | * @param $label |
| 232 | * @param $row |
| 233 | * @throws Exception if the the data row contains non numeric values |
| 234 | */ |
| 235 | public function sumMetrics($label, $row) |
| 236 | { |
| 237 | foreach ($row as $columnName => $columnValue) { |
| 238 | if (empty($columnValue)) { |
| 239 | continue; |
| 240 | } |
| 241 | if (empty($this->data[$label][$columnName])) { |
| 242 | $this->data[$label][$columnName] = 0; |
| 243 | } |
| 244 | if (!is_numeric($columnValue)) { |
| 245 | throw new Exception("DataArray->sumMetricsPivot expects rows of numeric values, non numeric found: " . var_export($columnValue, \true) . " for column {$columnName}"); |
| 246 | } |
| 247 | $this->data[$label][$columnName] += $columnValue; |
| 248 | } |
| 249 | } |
| 250 | public function sumMetricsVisitsPivot($parentLabel, $label, $row) |
| 251 | { |
| 252 | if (!isset($this->dataTwoLevels[$parentLabel][$label])) { |
| 253 | $this->dataTwoLevels[$parentLabel][$label] = static::makeEmptyRow(); |
| 254 | } |
| 255 | $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]); |
| 256 | } |
| 257 | public function sumMetricsGoalsPivot($parentLabel, $label, $row) |
| 258 | { |
| 259 | $idGoal = $row['idgoal']; |
| 260 | if (!isset($this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal])) { |
| 261 | $this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal] = static::makeEmptyGoalRow($idGoal); |
| 262 | } |
| 263 | $this->doSumGoalsMetrics($row, $this->dataTwoLevels[$parentLabel][$label][\Piwik\Metrics::INDEX_GOALS][$idGoal]); |
| 264 | } |
| 265 | public function sumMetricsActionsPivot($parentLabel, $label, $row) |
| 266 | { |
| 267 | if (!isset($this->dataTwoLevels[$parentLabel][$label])) { |
| 268 | $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyActionRow(); |
| 269 | } |
| 270 | $this->doSumActionsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]); |
| 271 | } |
| 272 | public function sumMetricsEventsPivot($parentLabel, $label, $row) |
| 273 | { |
| 274 | if (!isset($this->dataTwoLevels[$parentLabel][$label])) { |
| 275 | $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyEventRow(); |
| 276 | } |
| 277 | $this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]); |
| 278 | } |
| 279 | public function setRowColumnPivot($parentLabel, $label, $column, $value) |
| 280 | { |
| 281 | $this->dataTwoLevels[$parentLabel][$label][$column] = $value; |
| 282 | } |
| 283 | public function enrichMetricsWithConversions() |
| 284 | { |
| 285 | $this->enrichWithConversions($this->data); |
| 286 | foreach ($this->dataTwoLevels as &$metricsBySubLabel) { |
| 287 | $this->enrichWithConversions($metricsBySubLabel); |
| 288 | } |
| 289 | } |
| 290 | /** |
| 291 | * Given an array of stats, it will process the sum of goal conversions |
| 292 | * and sum of revenue and add it in the stats array in two new fields. |
| 293 | * |
| 294 | * @param array $data Passed by reference, two new columns |
| 295 | * will be added: total conversions, and total revenue, for all goals for this label/row |
| 296 | */ |
| 297 | protected function enrichWithConversions(&$data) |
| 298 | { |
| 299 | foreach ($data as &$values) { |
| 300 | EnrichRecordWithGoalMetricSums::enrichWithConversions($values); |
| 301 | } |
| 302 | } |
| 303 | /** |
| 304 | * Returns true if the row looks like an Action metrics row |
| 305 | * |
| 306 | * @param $row |
| 307 | * @return bool |
| 308 | */ |
| 309 | public static function isRowActions($row) |
| 310 | { |
| 311 | return count($row) == count(static::makeEmptyActionRow()) && isset($row[\Piwik\Metrics::INDEX_NB_ACTIONS]); |
| 312 | } |
| 313 | /** |
| 314 | * Converts array to a datatable |
| 315 | * |
| 316 | * @return \Piwik\DataTable |
| 317 | */ |
| 318 | public function asDataTable() |
| 319 | { |
| 320 | $dataArray = $this->getDataArray(); |
| 321 | $dataArrayTwoLevels = $this->getDataArrayWithTwoLevels(); |
| 322 | $subtableByLabel = null; |
| 323 | if (!empty($dataArrayTwoLevels)) { |
| 324 | $subtableByLabel = array(); |
| 325 | foreach ($dataArrayTwoLevels as $label => $subTable) { |
| 326 | $subtableByLabel[$label] = \Piwik\DataTable::makeFromIndexedArray($subTable); |
| 327 | } |
| 328 | } |
| 329 | return \Piwik\DataTable::makeFromIndexedArray($dataArray, $subtableByLabel); |
| 330 | } |
| 331 | } |
| 332 |