diff --git a/plugins/Goals/API.php b/plugins/Goals/API.php index b27757f2419be1f9da6cd5848436cdcb1003f3ef..6850aa25ff06dfe7845eb41a6014d5f9271288aa 100644 --- a/plugins/Goals/API.php +++ b/plugins/Goals/API.php @@ -401,13 +401,16 @@ class API extends \Piwik\Plugin\API $isEcommerceGoal = $idGoal === GoalManager::IDGOAL_ORDER || $idGoal === GoalManager::IDGOAL_CART; $allMetrics = Goals::getGoalColumns($idGoal); - $requestedColumns = Piwik::getArrayFromApiParameter($columns); + $columnsToShow = Piwik::getArrayFromApiParameter($columns); + $requestedColumns = $columnsToShow; $shouldAddAverageOrderRevenue = (in_array('avg_order_revenue', $requestedColumns) || empty($requestedColumns)) && $isEcommerceGoal; if ($shouldAddAverageOrderRevenue && !empty($requestedColumns)) { + $avgOrder = new AverageOrderRevenue(); $metricsToAdd = $avgOrder->getDependentMetrics(); + $requestedColumns = array_unique(array_merge($requestedColumns, $metricsToAdd)); } @@ -441,13 +444,14 @@ class API extends \Piwik\Plugin\API } // remove temporary metrics that were not explicitly requested - $allColumns = $allMetrics; - $allColumns[] = 'conversion_rate'; - if ($isEcommerceGoal) { - $allColumns[] = 'avg_order_revenue'; + if (empty($columnsToShow)) { + $columnsToShow = $allMetrics; + $columnsToShow[] = 'conversion_rate'; + if ($isEcommerceGoal) { + $columnsToShow[] = 'avg_order_revenue'; + } } - $columnsToShow = $requestedColumns ?: $allColumns; $dataTable->queueFilter('ColumnDelete', array($columnsToRemove = array(), $columnsToShow)); return $dataTable; diff --git a/tests/PHPUnit/System/EcommerceOrderWithItemsTest.php b/tests/PHPUnit/System/EcommerceOrderWithItemsTest.php index 5ac300b2e99d228818a9dd2b4aa05d33475e7e66..5e02c2493d6ae276574ab488f3261e73053ee4a2 100755 --- a/tests/PHPUnit/System/EcommerceOrderWithItemsTest.php +++ b/tests/PHPUnit/System/EcommerceOrderWithItemsTest.php @@ -245,6 +245,16 @@ class EcommerceOrderWithItemsTest extends SystemTestCase array($goalWeekApi, array('idSite' => $idSite2, 'date' => $dateTime, 'periods' => array('week'), 'testSuffix' => '_Website2')), + // see https://github.com/piwik/piwik/issues/7851 make sure avg_order_revenue is calculated correct + // even if only this column is given + array('Goals.get', array('idSite' => $idSite, + 'date' => $dateTime, + 'periods' => array('week'), + 'otherRequestParameters' => array( + 'idGoal' => Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER, + 'columns' => 'avg_order_revenue'), + 'testSuffix' => '_AvgOrderRevenue')), + ), self::getApiForTestingScheduledReports($dateTime, 'week') ); diff --git a/tests/PHPUnit/System/expected/test_ecommerceOrderWithItems_AvgOrderRevenue__Goals.get_week.xml b/tests/PHPUnit/System/expected/test_ecommerceOrderWithItems_AvgOrderRevenue__Goals.get_week.xml new file mode 100644 index 0000000000000000000000000000000000000000..345702c9a6e10de19e25993d0a3a5681ea22aa56 --- /dev/null +++ b/tests/PHPUnit/System/expected/test_ecommerceOrderWithItems_AvgOrderRevenue__Goals.get_week.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8" ?> +<result> + <avg_order_revenue>3337.78</avg_order_revenue> + <avg_order_revenue_new_visit>0</avg_order_revenue_new_visit> + <avg_order_revenue_returning_visit>3337.78</avg_order_revenue_returning_visit> +</result> \ No newline at end of file