public function get($idSite, $period, $date, $segment = false, $columns = false) { Piwik::checkUserHasViewAccess($idSite); $archive = Piwik_Archive::build($idSite, $period, $date, $segment); // array values are comma separated $columns = Piwik::getArrayFromApiParameter($columns); $countColumnsRequested = count($columns); $bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = false; if (!empty($columns)) { if (($bounceRateRequested = array_search('bounce_rate', $columns)) !== false) { $columns = array('nb_visits', 'bounce_count'); } elseif (($actionsPerVisitRequested = array_search('nb_actions_per_visit', $columns)) !== false) { $columns = array('nb_actions', 'nb_visits'); } elseif (($averageVisitDurationRequested = array_search('avg_time_on_site', $columns)) !== false) { $columns = array('sum_visit_length', 'nb_visits'); } } else { $bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = true; $columns = array('nb_visits', 'nb_uniq_visitors', 'nb_actions', 'nb_visits_converted', 'bounce_count', 'sum_visit_length', 'max_actions'); if (!Piwik::isUniqueVisitorsEnabled($period)) { unset($columns[array_search('nb_uniq_visitors', $columns)]); } // Force reindex from 0 to N otherwise the SQL bind will fail $columns = array_values($columns); } $dataTable = $archive->getDataTableFromNumeric($columns); // Process ratio metrics from base metrics, when requested if ($bounceRateRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnPercentage', array('bounce_rate', 'bounce_count', 'nb_visits', 0)); } if ($actionsPerVisitRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnQuotient', array('nb_actions_per_visit', 'nb_actions', 'nb_visits', 1)); } if ($averageVisitDurationRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnQuotient', array('avg_time_on_site', 'sum_visit_length', 'nb_visits', 0)); } // If only a computed metrics was requested, we delete other metrics // that we selected only to process this one metric if ($countColumnsRequested == 1 && ($bounceRateRequested || $actionsPerVisitRequested || $averageVisitDurationRequested)) { $dataTable->deleteColumns($columns); } return $dataTable; }
/** * Given a list of fields names, the method will fetch all their values over the period, and archive them using the given operation. * * For example if $operationToApply = 'sum' and $aNames = array('nb_visits', 'sum_time_visit') * it will sum all values of nb_visits for the period (for example give the number of visits for the month by summing the visits of every day) * * @param array|string $aNames Array of strings or string containg the field names to select * @param string $operationToApply Available operations = sum, max, min * @throws Exception * @return array */ private function archiveNumericValuesGeneral($aNames, $operationToApply) { $this->loadSubPeriods(); if (!is_array($aNames)) { $aNames = array($aNames); } // fetch the numeric values and apply the operation on them $results = array(); foreach ($this->archives as $id => $archive) { foreach ($aNames as $name) { if (!isset($results[$name])) { $results[$name] = 0; } if ($name == 'nb_uniq_visitors') { continue; } $valueToSum = $archive->getNumeric($name); if ($valueToSum !== false) { switch ($operationToApply) { case 'sum': $results[$name] += $valueToSum; break; case 'max': $results[$name] = max($results[$name], $valueToSum); break; case 'min': $results[$name] = min($results[$name], $valueToSum); break; default: throw new Exception("Operation not applicable."); break; } } } } if (!Piwik::isUniqueVisitorsEnabled($this->period->getLabel())) { unset($results['nb_uniq_visitors']); } foreach ($results as $name => $value) { if ($name == 'nb_uniq_visitors') { $value = (double) $this->computeNbUniqVisitors(); } $this->insertRecord($name, $value); } // if asked for only one field to sum if (count($results) == 1) { return $results[$name]; } // returns the array of records once summed return $results; }
/** * Sets general period variables (available periods, current period, period labels) used by templates * * @param Piwik_View $view * @throws Exception * @return void */ public static function setPeriodVariablesView($view) { if (isset($view->period)) { return; } $currentPeriod = Piwik_Common::getRequestVar('period'); $view->displayUniqueVisitors = Piwik::isUniqueVisitorsEnabled($currentPeriod); $availablePeriods = array('day', 'week', 'month', 'year', 'range'); if (!in_array($currentPeriod, $availablePeriods)) { throw new Exception("Period must be one of: " . implode(",", $availablePeriods)); } $periodNames = array('day' => array('singular' => Piwik_Translate('CoreHome_PeriodDay'), 'plural' => Piwik_Translate('CoreHome_PeriodDays')), 'week' => array('singular' => Piwik_Translate('CoreHome_PeriodWeek'), 'plural' => Piwik_Translate('CoreHome_PeriodWeeks')), 'month' => array('singular' => Piwik_Translate('CoreHome_PeriodMonth'), 'plural' => Piwik_Translate('CoreHome_PeriodMonths')), 'year' => array('singular' => Piwik_Translate('CoreHome_PeriodYear'), 'plural' => Piwik_Translate('CoreHome_PeriodYears')), 'range' => array('singular' => Piwik_Translate('General_DateRangeInPeriodList'), 'plural' => Piwik_Translate('General_DateRangeInPeriodList'))); $found = array_search($currentPeriod, $availablePeriods); if ($found !== false) { unset($availablePeriods[$found]); } $view->period = $currentPeriod; $view->otherPeriods = $availablePeriods; $view->periodsNames = $periodNames; }
public function get($idSite, $period, $date, $segment = false, $columns = false) { Piwik::checkUserHasViewAccess($idSite); $archive = Piwik_Archive::build($idSite, $period, $date, $segment); // array values are comma separated $columns = Piwik::getArrayFromApiParameter($columns); $tempColumns = array(); $bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = false; if (!empty($columns)) { // make sure base metrics are there for processed metrics if (false !== ($bounceRateRequested = array_search('bounce_rate', $columns))) { if (!in_array('nb_visits', $columns)) { $tempColumns[] = 'nb_visits'; } if (!in_array('bounce_count', $columns)) { $tempColumns[] = 'bounce_count'; } unset($columns[$bounceRateRequested]); } if (false !== ($actionsPerVisitRequested = array_search('nb_actions_per_visit', $columns))) { if (!in_array('nb_visits', $columns)) { $tempColumns[] = 'nb_visits'; } if (!in_array('nb_actions', $columns)) { $tempColumns[] = 'nb_actions'; } unset($columns[$actionsPerVisitRequested]); } if (false !== ($averageVisitDurationRequested = array_search('avg_time_on_site', $columns))) { if (!in_array('nb_visits', $columns)) { $tempColumns[] = 'nb_visits'; } if (!in_array('sum_visit_length', $columns)) { $tempColumns[] = 'sum_visit_length'; } unset($columns[$averageVisitDurationRequested]); } $tempColumns = array_unique($tempColumns); rsort($tempColumns); $columns = array_merge($columns, $tempColumns); } else { $bounceRateRequested = $actionsPerVisitRequested = $averageVisitDurationRequested = true; $columns = array('nb_visits', 'nb_actions', 'nb_visits_converted', 'bounce_count', 'sum_visit_length', 'max_actions'); if (Piwik::isUniqueVisitorsEnabled($period)) { $columns = array_merge(array('nb_uniq_visitors'), $columns); } // Force reindex from 0 to N otherwise the SQL bind will fail $columns = array_values($columns); } $dataTable = $archive->getDataTableFromNumeric($columns); // Process ratio metrics from base metrics, when requested if ($bounceRateRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnPercentage', array('bounce_rate', 'bounce_count', 'nb_visits', 0)); } if ($actionsPerVisitRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnQuotient', array('nb_actions_per_visit', 'nb_actions', 'nb_visits', 1)); } if ($averageVisitDurationRequested !== false) { $dataTable->filter('ColumnCallbackAddColumnQuotient', array('avg_time_on_site', 'sum_visit_length', 'nb_visits', 0)); } // remove temp metrics that were used to compute processed metrics $dataTable->deleteColumns($tempColumns); return $dataTable; }