diff --git a/plugins/CoreHome/Columns/UserId.php b/plugins/CoreHome/Columns/UserId.php index 55fea6b5173fe6345efde2730c5510a5268eeabe..ee7512444cf75119028dfe52e3b971f215d11b71 100644 --- a/plugins/CoreHome/Columns/UserId.php +++ b/plugins/CoreHome/Columns/UserId.php @@ -9,6 +9,7 @@ namespace Piwik\Plugins\CoreHome\Columns; use Piwik\Plugin\Dimension\VisitDimension; +use Piwik\Plugins\VisitsSummary\API as VisitsSummaryApi; use Piwik\Tracker\Request; use Piwik\Tracker\Visitor; use Piwik\Tracker\Action; @@ -51,4 +52,27 @@ class UserId extends VisitDimension return $request->getForcedUserId(); } + public function isUsedInAtLeastOneSite($idSites, $period, $date) + { + if ($period === 'day' || $period === 'week') { + $period = 'month'; + } + + foreach ($idSites as $idSite) { + $result = VisitsSummaryApi::getInstance()->get($idSite, $period, $date, false, 'nb_users'); + + if (!$result->getRowsCount()) { + continue; + } + + $numUsers = $result->getFirstRow()->getColumn('nb_users'); + + if (!empty($numUsers)) { + return true; + } + } + + return false; + } + } \ No newline at end of file diff --git a/plugins/VisitsSummary/Reports/Get.php b/plugins/VisitsSummary/Reports/Get.php index f2a087a517eee4d476337a0619c136ca9fcb2778..9df37d9bf5dc3800470e9d19e703ac40e175bb53 100644 --- a/plugins/VisitsSummary/Reports/Get.php +++ b/plugins/VisitsSummary/Reports/Get.php @@ -12,6 +12,7 @@ use Piwik\Piwik; use Piwik\Plugins\CoreHome\Columns\Metrics\ActionsPerVisit; use Piwik\Plugins\CoreHome\Columns\Metrics\AverageTimeOnSite; use Piwik\Plugins\CoreHome\Columns\Metrics\BounceRate; +use Piwik\Plugins\CoreHome\Columns\UserId; class Get extends \Piwik\Plugin\Report { @@ -39,6 +40,28 @@ class Get extends \Piwik\Plugin\Report $this->order = 1; } + public function configureReportMetadata(&$availableReports, $infos) + { + if (!$this->isEnabled()) { + return; + } + + if (!empty($infos['idSites']) && !empty($infos['period']) && !empty($infos['date'])) { + $userId = new UserId(); + $isUserIdUsed = $userId->isUsedInAtLeastOneSite($infos['idSites'], $infos['period'], $infos['date']); + + if (!$isUserIdUsed) { + $key = array_search('nb_users', $this->metrics); + if (false !== $key) { + unset($this->metrics[$key]); + $this->metrics = array_values($this->metrics); + } + }; + } + + parent::configureReportMetadata($availableReports, $infos); + } + public function getMetrics() { $metrics = parent::getMetrics();