Piwik\Plugins\Insights\API::getInsights PHP Method

getInsights() public method

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.
public getInsights ( integer $idSite, string $period, string $date, string $reportUniqueId, boolean | string $segment = false, integer $limitIncreaser = 5, integer $limitDecreaser = 5, string $filterBy = '', integer $minImpactPercent = 2, integer $minGrowthPercent = 20, integer $comparedToXPeriods = 1, string $orderBy = 'absolute' ) : DataTable
$idSite integer
$period string
$date string
$reportUniqueId string eg 'Actions_getPageUrls'. An id like 'Goals_getVisitsUntilConversion_idGoal--4' works as well.
$segment boolean | string
$limitIncreaser integer Value '0' ignores all increasers
$limitDecreaser integer Value '0' ignores all decreasers
$filterBy string By default all rows will be ignored. If given only 'movers', 'new' or 'disappeared' will be returned.
$minImpactPercent integer 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.
$minGrowthPercent integer 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.
$comparedToXPeriods integer The report will be compared to X periods before.
$orderBy string Orders the rows by 'absolute', 'relative' or 'importance'.
return Piwik\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;
    }