Skip to content

Commit

Permalink
Fixes #3868, make sure show/hideColumns query params affect metrics w…
Browse files Browse the repository at this point in the history
…hen report metadata doesn't provide a translation for metrics, and make sure if no columns are displayed for a report, API.get doesn't fail.
  • Loading branch information
diosmosis committed Apr 17, 2013
1 parent 540a0bb commit 08cfe92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
22 changes: 13 additions & 9 deletions plugins/API/API.php
Expand Up @@ -584,14 +584,6 @@ public function getReportMetadata($idSites = '', $period = false, $date = false,
// set metric documentation to default if it's not set
$availableReport['metricsDocumentation'] = $this->getDefaultMetricsDocumentation();
}

// if hide/show columns specified, hide/show metrics & docs
$availableReport['metrics'] = $this->hideShowMetrics($availableReport['metrics']);
$availableReport['processedMetrics'] = $this->hideShowMetrics($availableReport['processedMetrics']);
if (isset($availableReport['metricsDocumentation'])) {
$availableReport['metricsDocumentation'] =
$this->hideShowMetrics($availableReport['metricsDocumentation']);
}
}

// Some plugins need to add custom metrics after all plugins hooked in
Expand Down Expand Up @@ -623,6 +615,16 @@ public function getReportMetadata($idSites = '', $period = false, $date = false,
}
$availableReport['metrics'] = $cleanedMetrics;

// if hide/show columns specified, hide/show metrics & docs
$availableReport['metrics'] = $this->hideShowMetrics($availableReport['metrics']);
if (isset($availableReport['processedMetrics'])) {
$availableReport['processedMetrics'] = $this->hideShowMetrics($availableReport['processedMetrics']);
}
if (isset($availableReport['metricsDocumentation'])) {
$availableReport['metricsDocumentation'] =
$this->hideShowMetrics($availableReport['metricsDocumentation']);
}

// Remove array elements that are false (to clean up API output)
foreach ($availableReport as $attributeName => $attributeValue) {
if (empty($attributeValue)) {
Expand Down Expand Up @@ -1057,8 +1059,10 @@ public function get($idSite, $period, $date, $segment = false, $columns = false)
$meta = Piwik_API_API::getInstance()->getReportMetadata($idSite, $period, $date);
foreach ($meta as $reportMeta) {
// scan all *.get reports
if ($reportMeta['action'] == 'get' && !isset($reportMeta['parameters'])
if ($reportMeta['action'] == 'get'
&& !isset($reportMeta['parameters'])
&& $reportMeta['module'] != 'API'
&& !empty($reportMeta['metrics'])
) {
$plugin = $reportMeta['module'];
foreach ($reportMeta['metrics'] as $column => $columnTranslation) {
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPUnit/Integration/OneVisitorTwoVisitsTest.php
Expand Up @@ -136,6 +136,17 @@ public function getApiForTesting()
'avg_time_generation,nb_hits_with_time_generation',
'expanded' => '1'
))),

// test showColumns on API.get
array('API.get', array(
'idSite' => $idSite,
'date' => $dateTime,
'periods' => 'day',
'testSuffix' => '_showColumns',
'otherRequestParameters' => array(
'showColumns' => 'nb_uniq_visitors,nb_pageviews,bounce_rate'
)
)),
);
}

Expand Down
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<result>
<nb_uniq_visitors>1</nb_uniq_visitors>
<bounce_rate>50%</bounce_rate>
<nb_pageviews>4</nb_pageviews>
</result>

0 comments on commit 08cfe92

Please sign in to comment.