Example #1
0
 /**
  * Returns Goals data.
  *
  * @param int $idSite
  * @param string $period
  * @param string $date
  * @param bool $segment
  * @param bool|int $idGoal
  * @param array $columns Array of metrics to fetch: nb_conversions, conversion_rate, revenue
  * @return DataTable
  */
 public function get($idSite, $period, $date, $segment = false, $idGoal = false, $columns = array())
 {
     Piwik::checkUserHasViewAccess($idSite);
     /** @var DataTable|DataTable\Map $table */
     $table = null;
     $segments = array('' => false, '_new_visit' => 'visitorType%3D%3Dnew', '_returning_visit' => VisitFrequencyAPI::RETURNING_VISITOR_SEGMENT);
     foreach ($segments as $appendToMetricName => $predefinedSegment) {
         $segmentToUse = $this->appendSegment($predefinedSegment, $segment);
         /** @var DataTable|DataTable\Map $tableSegmented */
         $tableSegmented = Request::processRequest('Goals.getMetrics', array('segment' => $segmentToUse, 'idSite' => $idSite, 'period' => $period, 'date' => $date, 'idGoal' => $idGoal, 'columns' => $columns, 'serialize' => '0', 'format_metrics' => 'bc'));
         $tableSegmented->filter('Piwik\\Plugins\\Goals\\DataTable\\Filter\\AppendNameToColumnNames', array($appendToMetricName));
         if (!isset($table)) {
             $table = $tableSegmented;
         } else {
             $merger = new MergeDataTables();
             $merger->mergeDataTables($table, $tableSegmented);
         }
     }
     return $table;
 }
Example #2
0
 /**
  * Get a combined report of the *.get API methods.
  */
 public function get($idSite, $period, $date, $segment = false, $columns = false)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $columns = Piwik::getArrayFromApiParameter($columns);
     // build columns map for faster checks later on
     $columnsMap = array();
     foreach ($columns as $column) {
         $columnsMap[$column] = true;
     }
     // find out which columns belong to which plugin
     $columnsByPlugin = array();
     $meta = \Piwik\Plugins\API\API::getInstance()->getReportMetadata($idSite, $period, $date);
     foreach ($meta as $reportMeta) {
         // scan all *.get reports
         if ($reportMeta['action'] == 'get' && !isset($reportMeta['parameters']) && $reportMeta['module'] != 'API' && !empty($reportMeta['metrics'])) {
             $plugin = $reportMeta['module'];
             $allMetrics = array_merge($reportMeta['metrics'], @$reportMeta['processedMetrics'] ?: array());
             foreach ($allMetrics as $column => $columnTranslation) {
                 // a metric from this report has been requested
                 if (isset($columnsMap[$column]) || empty($columnsMap)) {
                     $columnsByPlugin[$plugin][] = $column;
                 }
             }
         }
     }
     krsort($columnsByPlugin);
     $mergedDataTable = false;
     $params = compact('idSite', 'period', 'date', 'segment', 'idGoal');
     foreach ($columnsByPlugin as $plugin => $columns) {
         // load the data
         $className = Request::getClassNameAPI($plugin);
         $params['columns'] = implode(',', $columns);
         $dataTable = Proxy::getInstance()->call($className, 'get', $params);
         $dataTable->filter(function (DataTable $table) {
             $table->clearQueuedFilters();
         });
         // merge reports
         if ($mergedDataTable === false) {
             $mergedDataTable = $dataTable;
         } else {
             $merger = new MergeDataTables();
             $merger->mergeDataTables($mergedDataTable, $dataTable);
         }
     }
     if (!empty($columnsMap) && !empty($mergedDataTable)) {
         $mergedDataTable->queueFilter('ColumnDelete', array(false, array_keys($columnsMap)));
     }
     return $mergedDataTable;
 }