diff options
author | diosmosis <benaka@piwik.pro> | 2014-11-08 02:46:58 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2014-11-08 03:00:47 +0300 |
commit | 68d77438edb1a492c9ef7943c16ac9874175b362 (patch) | |
tree | f5904b96eeefa21836b4033df180f5cdace544fb /plugins | |
parent | 949adebcc57524bd49c61eba6fda670c46654630 (diff) |
Converted most other processed metric calculation to use ProcessedMetric class.
Diffstat (limited to 'plugins')
26 files changed, 459 insertions, 79 deletions
diff --git a/plugins/API/ProcessedReport.php b/plugins/API/ProcessedReport.php index 9b317a15d2..2b72e07849 100644 --- a/plugins/API/ProcessedReport.php +++ b/plugins/API/ProcessedReport.php @@ -456,7 +456,7 @@ class ProcessedReport private function handleTableReport($idSite, $dataTable, &$reportMetadata, $showRawMetrics = false) { $hasDimension = isset($reportMetadata['dimension']); - $columns = $reportMetadata['metrics']; + $columns = @$reportMetadata['metrics'] ?: array(); if ($hasDimension) { $columns = array_merge( diff --git a/plugins/Actions/API.php b/plugins/Actions/API.php index 41c8cccda1..cdb34b9961 100644 --- a/plugins/Actions/API.php +++ b/plugins/Actions/API.php @@ -17,6 +17,10 @@ use Piwik\Date; use Piwik\Metrics; use Piwik\Piwik; use Piwik\Plugin\Report; +use Piwik\Plugins\Actions\Metrics\AveragePageGenerationTime; +use Piwik\Plugins\Actions\Metrics\AverageTimeOnPage; +use Piwik\Plugins\Actions\Metrics\BounceRate; +use Piwik\Plugins\Actions\Metrics\ExitRate; use Piwik\Plugins\CustomVariables\API as APICustomVariables; use Piwik\Plugins\Actions\Actions\ActionSiteSearch; use Piwik\Tracker\Action; @@ -62,7 +66,7 @@ class API extends \Piwik\Plugin\API $newNameMapping = array_combine($inDbColumnNames, $columns); $dataTable->filter('ReplaceColumnNames', array($newNameMapping)); - // TODO: either replace w/ temporary metrics, or just include them in the results and let people remove themif + // TODO: either replace w/ temporary metrics, or just include them in the results and let people remove them if // they want. $dataTable->queueFilter('ColumnDelete', array(array('sum_time_generation', 'nb_hits_with_time_generation'))); @@ -162,7 +166,7 @@ class API extends \Piwik\Plugin\API { $callBackParameters = array('Actions_actions_url', $idSite, $period, $date, $segment, $expanded = false, $idSubtable = false); $dataTable = $this->getFilterPageDatatableSearch($callBackParameters, $pageUrl, Action::TYPE_PAGE_URL); - // $this->filterPageDatatable($dataTable); TODO: no report metadata for getPageUrl... problem? can add extra processed metrics via metadata. + $this->addPageProcessedMetrics($dataTable); $this->filterActionsDataTable($dataTable); return $dataTable; } @@ -202,7 +206,7 @@ class API extends \Piwik\Plugin\API { $callBackParameters = array('Actions_actions', $idSite, $period, $date, $segment, $expanded = false, $idSubtable = false); $dataTable = $this->getFilterPageDatatableSearch($callBackParameters, $pageName, Action::TYPE_PAGE_TITLE); - // $this->filterPageDatatable($dataTable); TODO: no GetPageTitle report + $this->addPageProcessedMetrics($dataTable); $this->filterActionsDataTable($dataTable); return $dataTable; } @@ -447,7 +451,7 @@ class API extends \Piwik\Plugin\API $dataTable->filter('ReplaceColumnNames'); $dataTable->filter('Sort', array('nb_visits', 'desc', $naturalSort = false, $expanded)); - //$dataTable->queueFilter('ReplaceSummaryRowLabel'); TODO: necessary? + $dataTable->queueFilter('ReplaceSummaryRowLabel'); } /** @@ -493,4 +497,14 @@ class API extends \Piwik\Plugin\API } return Archive::getDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $idSubtable, $skipAggregationOfSubTables, $depth); } -} + + private function addPageprocessedMetrics(DataTable $dataTable) + { + $extraProcessedMetrics = $dataTable->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME); + $extraProcessedMetrics[] = new AverageTimeOnPage(); + $extraProcessedMetrics[] = new BounceRate(); + $extraProcessedMetrics[] = new ExitRate(); + $extraProcessedMetrics[] = new AveragePageGenerationTime(); + $dataTable->setMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME, $extraProcessedMetrics); + } +}
\ No newline at end of file diff --git a/plugins/Actions/Metrics/AveragePageGenerationTime.php b/plugins/Actions/Metrics/AveragePageGenerationTime.php index 1bbe713a12..678fd52f31 100644 --- a/plugins/Actions/Metrics/AveragePageGenerationTime.php +++ b/plugins/Actions/Metrics/AveragePageGenerationTime.php @@ -43,9 +43,19 @@ class AveragePageGenerationTime extends ProcessedMetric return Piwik::getQuotientSafe($sumGenerationTime, $hitsWithTimeGeneration, $precision = 3); } - public function shouldComputeForTable(Report $report, DataTable $table) + public function beforeCompute(Report $report, DataTable $table) { - $hasTimeGeneration = array_sum($table->getColumn(Metrics::INDEX_PAGE_SUM_TIME_GENERATION)) > 0; + $columnName = Metrics::INDEX_PAGE_SUM_TIME_GENERATION; + + // TODO: code redundancy w/ another metric (VisitsPercent i think?) + $firstRow = $table->getFirstRow(); + if (!empty($firstRow) + && $firstRow->getColumn($columnName) === false + ) { + $columnName = 'sum_time_generation'; + } + + $hasTimeGeneration = array_sum($table->getColumn($columnName)) > 0; if (!$hasTimeGeneration) { // TODO: ideally this logic shouldn't exist... // No generation time: remove it from the API output and add it to empty_columns metadata, so that diff --git a/plugins/Actions/Metrics/ExitRate.php b/plugins/Actions/Metrics/ExitRate.php index 2570de0ac1..89a1955804 100644 --- a/plugins/Actions/Metrics/ExitRate.php +++ b/plugins/Actions/Metrics/ExitRate.php @@ -32,7 +32,12 @@ class ExitRate extends ProcessedMetric $exitVisits = $this->getColumn($row, 'exit_nb_visits'); $visits = $this->getColumn($row, 'nb_visits'); - return Piwik::getQuotientSafe($exitVisits, $visits, $precision = 0); + return Piwik::getQuotientSafe($exitVisits, $visits, $precision = 2); + } + + public function format($value) // TODO: should create ratio & percent processed metric base types + { + return ($value * 100) . '%'; } public function getDependenctMetrics() diff --git a/plugins/Actions/Reports/GetEntryPageTitles.php b/plugins/Actions/Reports/GetEntryPageTitles.php index f5b9c41817..f1332f2266 100644 --- a/plugins/Actions/Reports/GetEntryPageTitles.php +++ b/plugins/Actions/Reports/GetEntryPageTitles.php @@ -11,6 +11,10 @@ namespace Piwik\Plugins\Actions\Reports; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\Plugins\Actions\Columns\EntryPageTitle; +use Piwik\Plugins\Actions\Metrics\AveragePageGenerationTime; +use Piwik\Plugins\Actions\Metrics\AverageTimeOnPage; +use Piwik\Plugins\Actions\Metrics\BounceRate; +use Piwik\Plugins\Actions\Metrics\ExitRate; class GetEntryPageTitles extends Base { @@ -22,7 +26,13 @@ class GetEntryPageTitles extends Base $this->name = Piwik::translate('Actions_EntryPageTitles'); $this->documentation = Piwik::translate('Actions_ExitPageTitlesReportDocumentation', '<br />') . ' ' . Piwik::translate('General_UsePlusMinusIconsDocumentation'); - $this->metrics = array('entry_nb_visits', 'entry_bounce_count', 'bounce_rate'); + $this->metrics = array('entry_nb_visits', 'entry_bounce_count'); + $this->processedMetrics = array( + new AverageTimeOnPage(), + new BounceRate(), + new ExitRate(), + new AveragePageGenerationTime() + ); $this->order = 6; $this->actionToLoadSubTables = $this->action; diff --git a/plugins/Actions/Reports/GetEntryPageUrls.php b/plugins/Actions/Reports/GetEntryPageUrls.php index 71cab238b7..d0c42891fe 100644 --- a/plugins/Actions/Reports/GetEntryPageUrls.php +++ b/plugins/Actions/Reports/GetEntryPageUrls.php @@ -13,6 +13,10 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\API\Request; use Piwik\Plugins\Actions\Columns\EntryPageUrl; +use Piwik\Plugins\Actions\Metrics\AveragePageGenerationTime; +use Piwik\Plugins\Actions\Metrics\AverageTimeOnPage; +use Piwik\Plugins\Actions\Metrics\BounceRate; +use Piwik\Plugins\Actions\Metrics\ExitRate; class GetEntryPageUrls extends Base { @@ -25,7 +29,13 @@ class GetEntryPageUrls extends Base $this->documentation = Piwik::translate('Actions_EntryPagesReportDocumentation', '<br />') . '<br />' . Piwik::translate('General_UsePlusMinusIconsDocumentation'); - $this->metrics = array('entry_nb_visits', 'entry_bounce_count', 'bounce_rate'); + $this->metrics = array('entry_nb_visits', 'entry_bounce_count'); + $this->processedMetrics = array( + new AverageTimeOnPage(), + new BounceRate(), + new ExitRate(), + new AveragePageGenerationTime() + ); $this->order = 3; $this->actionToLoadSubTables = $this->action; diff --git a/plugins/Actions/Reports/GetExitPageTitles.php b/plugins/Actions/Reports/GetExitPageTitles.php index a9d29bd552..996b418843 100644 --- a/plugins/Actions/Reports/GetExitPageTitles.php +++ b/plugins/Actions/Reports/GetExitPageTitles.php @@ -11,6 +11,10 @@ namespace Piwik\Plugins\Actions\Reports; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\Plugins\Actions\Columns\ExitPageTitle; +use Piwik\Plugins\Actions\Metrics\AveragePageGenerationTime; +use Piwik\Plugins\Actions\Metrics\AverageTimeOnPage; +use Piwik\Plugins\Actions\Metrics\BounceRate; +use Piwik\Plugins\Actions\Metrics\ExitRate; class GetExitPageTitles extends Base { @@ -23,7 +27,13 @@ class GetExitPageTitles extends Base $this->documentation = Piwik::translate('Actions_EntryPageTitlesReportDocumentation', '<br />') . ' ' . Piwik::translate('General_UsePlusMinusIconsDocumentation'); - $this->metrics = array('exit_nb_visits', 'nb_visits', 'exit_rate'); + $this->metrics = array('exit_nb_visits', 'nb_visits'); + $this->processedMetrics = array( + new AverageTimeOnPage(), + new BounceRate(), + new ExitRate(), + new AveragePageGenerationTime() + ); $this->order = 7; $this->actionToLoadSubTables = $this->action; diff --git a/plugins/Actions/Reports/GetExitPageUrls.php b/plugins/Actions/Reports/GetExitPageUrls.php index 72a95379b6..781ca05f3a 100644 --- a/plugins/Actions/Reports/GetExitPageUrls.php +++ b/plugins/Actions/Reports/GetExitPageUrls.php @@ -13,6 +13,10 @@ use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\API\Request; use Piwik\Plugins\Actions\Columns\ExitPageUrl; +use Piwik\Plugins\Actions\Metrics\AveragePageGenerationTime; +use Piwik\Plugins\Actions\Metrics\AverageTimeOnPage; +use Piwik\Plugins\Actions\Metrics\BounceRate; +use Piwik\Plugins\Actions\Metrics\ExitRate; class GetExitPageUrls extends Base { @@ -25,7 +29,13 @@ class GetExitPageUrls extends Base $this->documentation = Piwik::translate('Actions_ExitPagesReportDocumentation', '<br />') . '<br />' . Piwik::translate('General_UsePlusMinusIconsDocumentation'); - $this->metrics = array('exit_nb_visits', 'nb_visits', 'exit_rate'); + $this->metrics = array('exit_nb_visits', 'nb_visits'); + $this->processedMetrics = array( + new AverageTimeOnPage(), + new BounceRate(), + new ExitRate(), + new AveragePageGenerationTime() + ); $this->actionToLoadSubTables = $this->action; $this->order = 4; diff --git a/plugins/Contents/API.php b/plugins/Contents/API.php index 002360588f..43d07b7cdc 100644 --- a/plugins/Contents/API.php +++ b/plugins/Contents/API.php @@ -61,8 +61,5 @@ class API extends \Piwik\Plugin\API } } }); - - // Content interaction rate = interactions / impressions - $dataTable->queueFilter('ColumnCallbackAddColumnPercentage', array('interaction_rate', 'nb_interactions', 'nb_impressions', $precision = 2)); } } diff --git a/plugins/Contents/Metrics/InteractionRate.php b/plugins/Contents/Metrics/InteractionRate.php new file mode 100644 index 0000000000..41efeac82a --- /dev/null +++ b/plugins/Contents/Metrics/InteractionRate.php @@ -0,0 +1,48 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Contents\Metrics; + +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; + +/** + * TODO +// Content interaction rate = interactions / impressions + */ +class InteractionRate extends ProcessedMetric +{ + public function getName() + { + return 'interaction_rate'; + } + + public function getTranslatedName() + { + return Piwik::translate('Contents_InteractionRate'); + } + + public function compute(Row $row) + { + $interactions = $this->getColumn($row, 'nb_interactions'); + $impressions = $this->getColumn($row, 'nb_impressions'); + + return Piwik::getQuotientSafe($interactions, $impressions, $precision = 4); + } + + public function format($value) + { + return ($value * 100) . '%'; + } + + public function getDependenctMetrics() + { + return array('nb_interactions', 'nb_impressions'); + } +}
\ No newline at end of file diff --git a/plugins/Events/API.php b/plugins/Events/API.php index 890979db2c..d81144f855 100644 --- a/plugins/Events/API.php +++ b/plugins/Events/API.php @@ -217,14 +217,5 @@ class API extends \Piwik\Plugin\API $row->setColumn('label', Piwik::translate('General_NotDefined', Piwik::translate('Events_EventName'))); } }); - - // add processed metric avg_event_value - $dataTable->queueFilter('ColumnCallbackAddColumnQuotient', - array('avg_event_value', - 'sum_event_value', - 'nb_events_with_value', - $precision = 2, - $shouldSkipRows = true) - ); } }
\ No newline at end of file diff --git a/plugins/Events/Metrics/AverageEventValue.php b/plugins/Events/Metrics/AverageEventValue.php new file mode 100644 index 0000000000..e09079f266 --- /dev/null +++ b/plugins/Events/Metrics/AverageEventValue.php @@ -0,0 +1,42 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Events\Metrics; + +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; + +/** + * TODO + */ +class AverageEventValue extends ProcessedMetric +{ + public function getName() + { + return 'avg_event_value'; + } + + public function getTranslatedName() + { + return Piwik::translate('Events_AvgValueDocumentation'); + } + + public function compute(Row $row) + { + $sumEventValue = $this->getColumn($row, 'sum_event_value'); + $eventsWithValue = $this->getColumn($row, 'nb_events_with_value'); + + return Piwik::getQuotientSafe($sumEventValue, $eventsWithValue, $precision = 2); // TODO: used to use shouldSkipRows = true + } + + public function getDependenctMetrics() + { + return array('sum_event_value', 'nb_events_with_value'); + } +}
\ No newline at end of file diff --git a/plugins/Events/Reports/Base.php b/plugins/Events/Reports/Base.php index 2caf9b6a75..e49c7815fa 100644 --- a/plugins/Events/Reports/Base.php +++ b/plugins/Events/Reports/Base.php @@ -9,13 +9,16 @@ namespace Piwik\Plugins\Events\Reports; use Piwik\Plugins\Events\API; +use Piwik\Plugins\Events\Metrics\AverageEventValue; abstract class Base extends \Piwik\Plugin\Report { protected function init() { $this->category = 'Events_Events'; - $this->processedMetrics = false; + $this->processedMetrics = array( + new AverageEventValue() + ); $this->widgetParams = array( 'secondaryDimension' => API::getInstance()->getDefaultSecondaryDimension($this->action) diff --git a/plugins/Goals/API.php b/plugins/Goals/API.php index 36d0007f83..dcf39f8874 100644 --- a/plugins/Goals/API.php +++ b/plugins/Goals/API.php @@ -230,17 +230,8 @@ class API extends \Piwik\Plugin\API $dataTable->renameColumn(Metrics::INDEX_ECOMMERCE_ORDERS, $ordersColumn); } - // Average price = sum product revenue / quantity - $dataTable->queueFilter('ColumnCallbackAddColumnQuotient', array('avg_price', 'price', $ordersColumn, GoalManager::REVENUE_PRECISION)); - - // Average quantity = sum product quantity / abandoned carts - $dataTable->queueFilter('ColumnCallbackAddColumnQuotient', - array('avg_quantity', 'quantity', $ordersColumn, $precision = 1)); $dataTable->queueFilter('ColumnDelete', array('price')); - // Product conversion rate = orders / visits - $dataTable->queueFilter('ColumnCallbackAddColumnPercentage', array('conversion_rate', $ordersColumn, 'nb_visits', GoalManager::REVENUE_PRECISION)); - return $dataTable; } diff --git a/plugins/Goals/Goals.php b/plugins/Goals/Goals.php index 381c37f7ee..cd75af0c9e 100644 --- a/plugins/Goals/Goals.php +++ b/plugins/Goals/Goals.php @@ -117,7 +117,7 @@ class Goals extends \Piwik\Plugin 'revenue_per_visit' => 'General_ColumnValuePerVisit', 'quantity' => 'General_Quantity', 'avg_price' => 'General_AveragePrice', - 'avg_quantity' => 'General_AverageQuantity', + 'avg_quantity' => 'onv', 'revenue_subtotal' => 'General_Subtotal', 'revenue_tax' => 'General_Tax', 'revenue_shipping' => 'General_Shipping', diff --git a/plugins/Goals/Metrics/AveragePrice.php b/plugins/Goals/Metrics/AveragePrice.php new file mode 100644 index 0000000000..1bc6932d68 --- /dev/null +++ b/plugins/Goals/Metrics/AveragePrice.php @@ -0,0 +1,45 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Goals\Metrics; + +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; +use Piwik\Tracker\GoalManager; + +/** + * TODO +// Average price = sum product revenue / quantity + */ +class AveragePrice extends ProcessedMetric +{ + public function getName() + { + return 'avg_price'; + } + + public function getTranslatedName() + { + return Piwik::translate('General_AveragePrice'); + } + + public function compute(Row $row) + { + $price = $this->getColumn($row, 'price'); + $orders = $this->getColumn($row, 'orders'); + $abandonedCarts = $this->getColumn($row, 'abandoned_carts'); + + return Piwik::getQuotientSafe($price, $orders === false ? $abandonedCarts : $orders, GoalManager::REVENUE_PRECISION); + } + + public function getDependenctMetrics() + { + return array('price', 'orders', 'abandoned_carts'); + } +}
\ No newline at end of file diff --git a/plugins/Goals/Metrics/AverageQuantity.php b/plugins/Goals/Metrics/AverageQuantity.php new file mode 100644 index 0000000000..6644840e82 --- /dev/null +++ b/plugins/Goals/Metrics/AverageQuantity.php @@ -0,0 +1,44 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Goals\Metrics; + +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; + +/** + * TODO +// Average quantity = sum product quantity / abandoned carts + */ +class AverageQuantity extends ProcessedMetric +{ + public function getName() + { + return 'avg_quantity'; + } + + public function getTranslatedName() + { + return Piwik::translate('General_AverageQuantity'); + } + + public function compute(Row $row) + { + $quantity = $this->getColumn($row, 'quantity'); + $orders = $this->getColumn($row, 'orders'); + $abandonedCarts = $this->getColumn($row, 'abandoned_carts'); + + return Piwik::getQuotientSafe($quantity, $orders === false ? $abandonedCarts : $orders, $precision = 1); + } + + public function getDependenctMetrics() + { + return array('quantity', 'orders', 'abandoned_carts'); + } +}
\ No newline at end of file diff --git a/plugins/Goals/Metrics/ProductConversionRate.php b/plugins/Goals/Metrics/ProductConversionRate.php new file mode 100644 index 0000000000..7be6bf28af --- /dev/null +++ b/plugins/Goals/Metrics/ProductConversionRate.php @@ -0,0 +1,45 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\Goals\Metrics; + +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; +use Piwik\Tracker\GoalManager; + +/** + * TODO +// Product conversion rate = orders / visits + */ +class ProductConversionRate extends ProcessedMetric +{ + public function getName() + { + return 'conversion_rate'; + } + + public function getTranslatedName() + { + return Piwik::translate('General_ColumnConversionRate'); + } + + public function compute(Row $row) + { + $orders = $this->getColumn($row, 'orders'); + $abandonedCarts = $this->getColumn($row, 'abandoned_carts'); + $visits = $this->getColumn($row, 'nb_visits'); + + return Piwik::getQuotientSafe($orders === false ? $abandonedCarts : $orders, $visits, GoalManager::REVENUE_PRECISION); + } + + public function getDependenctMetrics() + { + return array('orders', 'abandoned_carts', 'nb_visits'); + } +}
\ No newline at end of file diff --git a/plugins/Goals/Reports/BaseEcommerceItem.php b/plugins/Goals/Reports/BaseEcommerceItem.php index 8699fe386b..b74585c7f4 100644 --- a/plugins/Goals/Reports/BaseEcommerceItem.php +++ b/plugins/Goals/Reports/BaseEcommerceItem.php @@ -13,15 +13,22 @@ use Piwik\Piwik; use Piwik\Plugin\Report; use Piwik\Plugin\ViewDataTable; use Piwik\Plugins\Goals\Goals; +use Piwik\Plugins\Goals\Metrics\AveragePrice; +use Piwik\Plugins\Goals\Metrics\AverageQuantity; +use Piwik\Plugins\Goals\Metrics\ProductConversionRate; abstract class BaseEcommerceItem extends BaseEcommerce { protected function init() { parent::init(); - $this->processedMetrics = false; + $this->processedMetrics = array( + new AveragePrice(), + new AverageQuantity(), + new ProductConversionRate() + ); $this->metrics = array( - 'revenue', 'quantity', 'orders', 'avg_price', 'avg_quantity', 'nb_visits', 'conversion_rate' + 'revenue', 'quantity', 'orders', 'nb_visits' ); } diff --git a/plugins/Live/API.php b/plugins/Live/API.php index d15cca9e36..67f7d46cd4 100644 --- a/plugins/Live/API.php +++ b/plugins/Live/API.php @@ -167,6 +167,7 @@ class API extends \Piwik\Plugin\API $dataTable = $this->loadLastVisitorDetailsFromDatabase($idSite, $period, $date, $segment, $countVisitorsToFetch, $visitorId = false, $minTimestamp, $filterSortOrder); $this->addFilterToCleanVisitors($dataTable, $idSite, $flat, $doNotFetchActions); + // TODO: why is this necessary? shouldn't be. uncomment & see if tests pass. $filterSortColumn = Common::getRequestVar('filter_sort_column', false, 'string'); $filterSortOrder = Common::getRequestVar('filter_sort_order', 'desc', 'string'); diff --git a/plugins/UserSettings/API.php b/plugins/UserSettings/API.php index 76b98ad8b2..014db7a765 100644 --- a/plugins/UserSettings/API.php +++ b/plugins/UserSettings/API.php @@ -196,6 +196,14 @@ class API extends \Piwik\Plugin\API } } + // Calculate percentage, but ignore IE users because plugin detection doesn't work on IE + $ieVisits = 0; + + $ieStats = $browserType->getRowFromLabel('Trident'); + if ($ieStats !== false) { + $ieVisits = $ieStats->getColumn(Metrics::INDEX_NB_VISITS); + } + // get according visitsSum foreach ($visitSumsArray as $k => $visits) { if ($k == $key) { @@ -211,24 +219,9 @@ class API extends \Piwik\Plugin\API } } - // Calculate percentage, but ignore IE users because plugin detection doesn't work on IE - $ieVisits = 0; - - $ieStats = $browserType->getRowFromLabel('Trident'); - if ($ieStats !== false) { - $ieVisits = $ieStats->getColumn(Metrics::INDEX_NB_VISITS); - } - $visitsSum = $visitsSumTotal - $ieVisits; - // When Truncate filter is applied, it will call AddSummaryRow which tries to sum all rows. - // We tell the object to skip the column nb_visits_percentage when aggregating (since it's not correct to sum % values) - $columnAggregationOps = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); - $columnAggregationOps['nb_visits_percentage'] = 'skip'; - $table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOps); - - // The filter must be applied now so that the new column can - // be sorted by the generic filters (applied right after this loop exits) + // TODO: refactor this message before using PluginsVisitsPercent $table->filter('ColumnCallbackAddColumnPercentage', array('nb_visits_percentage', Metrics::INDEX_NB_VISITS, $visitsSum, 1)); $table->filter('RangeCheck', array('nb_visits_percentage')); } diff --git a/plugins/UserSettings/Metrics/PluginsVisitsPercent.php b/plugins/UserSettings/Metrics/PluginsVisitsPercent.php new file mode 100644 index 0000000000..b3b87bc0e5 --- /dev/null +++ b/plugins/UserSettings/Metrics/PluginsVisitsPercent.php @@ -0,0 +1,54 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\UserSettings\Metrics; + +use Piwik\DataTable; +use Piwik\DataTable\Row; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; +use Piwik\Plugin\Report; + +/** + * TODO + */ +class PluginsVisitsPercent extends ProcessedMetric +{ + private $cachedTotalVisits = null; + + public function getName() + { + return 'nb_visits_percentage'; + } + + public function getTranslatedName() + { + return Piwik::translate('General_ColumnPercentageVisits'); + } + + public function compute(Row $row) + { + // TODO: Implement compute() method. + } + + public function format($value) + { + return ($value * 100) . '%'; + } + + public function getDependenctMetrics() + { + return array('nb_visits'); + } + + public function beforeCompute(Report $report, DataTable $table) + { + // TODO + return true; + } +}
\ No newline at end of file diff --git a/plugins/VisitFrequency/API.php b/plugins/VisitFrequency/API.php index a100521788..d05cf436c7 100644 --- a/plugins/VisitFrequency/API.php +++ b/plugins/VisitFrequency/API.php @@ -48,10 +48,12 @@ class API extends \Piwik\Plugin\API 'serialize' => 0 // tests set this to 1 ); - /** @var DataTable $table */ $table = Request::processRequest('VisitsSummary.get', $params); $this->prefixColumns($table, $period); - $table->clearQueuedFilters(); + + $table->filter(function (DataTable $table) { + $table->clearQueuedFilters(); + }); return $table; } diff --git a/plugins/VisitorInterest/API.php b/plugins/VisitorInterest/API.php index 44df111588..33f6d97b58 100644 --- a/plugins/VisitorInterest/API.php +++ b/plugins/VisitorInterest/API.php @@ -87,28 +87,6 @@ class API extends \Piwik\Plugin\API $dataTable->queueFilter('BeautifyRangeLabels', array( Piwik::translate('General_OneVisit'), Piwik::translate('General_NVisits'))); - // add visit percent column - self::addVisitsPercentColumn($dataTable); - return $dataTable; } - - /** - * Utility function that adds a visit percent column to a data table, - * regardless of whether the data table is an data table array or just - * a data table. - * - * @param DataTable $dataTable The data table to modify. - */ - private static function addVisitsPercentColumn($dataTable) - { - if ($dataTable instanceof DataTable\Map) { - foreach ($dataTable->getDataTables() as $table) { - self::addVisitsPercentColumn($table); - } - } else { - $totalVisits = array_sum($dataTable->getColumn(Metrics::INDEX_NB_VISITS)); - $dataTable->queueFilter('ColumnCallbackAddColumnPercentage', array('nb_visits_percentage', 'nb_visits', $totalVisits)); - } - } } diff --git a/plugins/VisitorInterest/Metrics/VisitsPercent.php b/plugins/VisitorInterest/Metrics/VisitsPercent.php new file mode 100644 index 0000000000..74afb172b9 --- /dev/null +++ b/plugins/VisitorInterest/Metrics/VisitsPercent.php @@ -0,0 +1,67 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\VisitorInterest\Metrics; + +use Piwik\DataTable; +use Piwik\DataTable\Row; +use Piwik\Metrics; +use Piwik\Piwik; +use Piwik\Plugin\ProcessedMetric; +use Piwik\Plugin\Report; + +/** + * TODO + */ +class VisitsPercent extends ProcessedMetric +{ + private $cachedTotalVisits = null; + + public function getName() + { + return 'nb_visits_percentage'; + } + + public function getTranslatedName() + { + return Piwik::translate('General_ColumnPercentageVisits'); + } + + public function compute(Row $row) + { + $visits = $this->getColumn($row, 'nb_visits'); + + return Piwik::getQuotientSafe($visits, $this->cachedTotalVisits, $precision = 2); + } + + public function format($value) + { + return ($value * 100) . '%'; + } + + public function getDependenctMetrics() + { + return array('nb_visits'); + } + + public function beforeCompute(Report $report, DataTable $table) + { + $columnName = 'nb_visits'; + + $firstRow = $table->getFirstRow(); + if (!empty($firstRow) + && $firstRow->getColumn($columnName) === false + ) { + $columnName = Metrics::INDEX_NB_VISITS; + } + + $this->cachedTotalVisits = array_sum($table->getColumn($columnName)); + + return true; // always compute + } +}
\ No newline at end of file diff --git a/plugins/VisitorInterest/Reports/GetNumberOfVisitsByVisitCount.php b/plugins/VisitorInterest/Reports/GetNumberOfVisitsByVisitCount.php index 74cf975a4d..0c42eafab0 100644 --- a/plugins/VisitorInterest/Reports/GetNumberOfVisitsByVisitCount.php +++ b/plugins/VisitorInterest/Reports/GetNumberOfVisitsByVisitCount.php @@ -12,6 +12,7 @@ use Piwik\Metrics; use Piwik\Piwik; use Piwik\Plugin\ViewDataTable; use Piwik\Plugins\VisitorInterest\Columns\VisitsbyVisitNumber; +use Piwik\Plugins\VisitorInterest\Metrics\VisitsPercent; class GetNumberOfVisitsByVisitCount extends Base { @@ -23,7 +24,9 @@ class GetNumberOfVisitsByVisitCount extends Base $this->documentation = Piwik::translate('VisitorInterest_WidgetVisitsByNumDocumentation') . '<br />' . Piwik::translate('General_ChangeTagCloudView'); $this->metrics = array('nb_visits', 'nb_visits_percentage'); - $this->processedMetrics = false; + $this->processedMetrics = array( + new VisitsPercent() + ); $this->constantRowsCount = true; $this->order = 25; $this->widgetTitle = 'VisitorInterest_visitsByVisitCount'; |