/** * Generates insights by comparing the report for a given date/period with a different date and calculating the * difference. The API can exclude rows which growth is not good enough or did not have enough impact. * * @param int $idSite * @param string $period * @param string $date * @param string $reportUniqueId eg 'Actions_getPageUrls'. An id like 'Goals_getVisitsUntilConversion_idGoal--4' works as well. * @param bool|string $segment * @param int $limitIncreaser Value '0' ignores all increasers * @param int $limitDecreaser Value '0' ignores all decreasers * @param string $filterBy By default all rows will be ignored. If given only 'movers', 'new' or 'disappeared' will be returned. * @param int $minImpactPercent The minimum impact in percent. Eg '2%' of 1000 visits means the change / * increase / decrease has to be at least 20 visits. Usually the '2%' are based on the total * amount of visits but for reports having way less visits the metric total is used. Eg A page * has 1000 visits but only 100 visits having keywords. In this case a minimum impact of '2%' evaluates to 2 and not 20. * @param int $minGrowthPercent The amount of percent a row has to increase or decrease at least compared to the previous period. * If value is '20' the growth has to be either at least '+20%' or '-20%' and lower. * @param int $comparedToXPeriods The report will be compared to X periods before. * @param string $orderBy Orders the rows by 'absolute', 'relative' or 'importance'. * * @return DataTable * * @throws \Exception In case a report having the given ID does not exist * @throws \Exception In case the report exists but does not return a dataTable */ public function getInsights($idSite, $period, $date, $reportUniqueId, $segment = false, $limitIncreaser = 5, $limitDecreaser = 5, $filterBy = '', $minImpactPercent = 2, $minGrowthPercent = 20, $comparedToXPeriods = 1, $orderBy = 'absolute') { Piwik::checkUserHasViewAccess(array($idSite)); $metric = 'nb_visits'; $reportMetadata = $this->model->getReportByUniqueId($idSite, $reportUniqueId); if (empty($reportMetadata)) { throw new \Exception('A report having the ID ' . $reportUniqueId . ' does not exist'); } $totalValue = $this->model->getTotalValue($idSite, $period, $date, $metric, $segment); $currentReport = $this->model->requestReport($idSite, $period, $date, $reportUniqueId, $metric, $segment); $this->checkReportIsValid($currentReport); $lastDate = $this->model->getLastDate($date, $period, $comparedToXPeriods); $lastTotalValue = $this->model->getTotalValue($idSite, $period, $lastDate, $metric, $segment); $lastReport = $this->model->requestReport($idSite, $period, $lastDate, $reportUniqueId, $metric, $segment); $this->checkReportIsValid($lastReport); $minGrowthPercentPositive = abs($minGrowthPercent); $minGrowthPercentNegative = -1 * $minGrowthPercentPositive; $relevantTotal = $this->model->getRelevantTotalValue($currentReport, $metric, $totalValue); $minMoversPercent = -1; $minNewPercent = -1; $minDisappearedPercent = -1; switch ($filterBy) { case self::FILTER_BY_MOVERS: $minMoversPercent = $minImpactPercent; break; case self::FILTER_BY_NEW: $minNewPercent = $minImpactPercent; break; case self::FILTER_BY_DISAPPEARED: $minDisappearedPercent = $minImpactPercent; break; default: $minMoversPercent = $minImpactPercent; $minNewPercent = $minImpactPercent; $minDisappearedPercent = $minImpactPercent; } $insight = new InsightReport(); $table = $insight->generateInsight($reportMetadata, $period, $date, $lastDate, $metric, $currentReport, $lastReport, $relevantTotal, $minMoversPercent, $minNewPercent, $minDisappearedPercent, $minGrowthPercentPositive, $minGrowthPercentNegative, $orderBy, $limitIncreaser, $limitDecreaser); $insight->markMoversAndShakers($table, $currentReport, $lastReport, $totalValue, $lastTotalValue); return $table; }
/** * @expectedException \Exception */ public function test_getLastDate_shouldThrowExceptionInCaseOfRangePeriod() { $this->model->getLastDate('2012-11-11,2012-12-12', 'range', 1); }