/** * @internal param $this->getProcessor() */ public function aggregateMultipleReports() { /* * Archive Ecommerce Items */ $dataTableToSum = $this->dimensionRecord; foreach ($this->dimensionRecord as $recordName) { $dataTableToSum[] = self::getItemRecordNameAbandonedCart($recordName); } $this->getProcessor()->aggregateDataTableRecords($dataTableToSum); /* * Archive General Goal metrics */ $goalIdsToSum = GoalManager::getGoalIds($this->getProcessor()->getParams()->getSite()->getId()); //Ecommerce $goalIdsToSum[] = GoalManager::IDGOAL_ORDER; $goalIdsToSum[] = GoalManager::IDGOAL_CART; //bug here if idgoal=1 // Overall goal metrics $goalIdsToSum[] = false; $fieldsToSum = array(); foreach ($goalIdsToSum as $goalId) { $metricsToSum = Goals::getGoalColumns($goalId); unset($metricsToSum[array_search('conversion_rate', $metricsToSum)]); foreach ($metricsToSum as $metricName) { $fieldsToSum[] = self::getRecordName($metricName, $goalId); } } $records = $this->getProcessor()->aggregateNumericMetrics($fieldsToSum); // also recording conversion_rate for each goal foreach ($goalIdsToSum as $goalId) { $nb_conversions = $records[self::getRecordName('nb_visits_converted', $goalId)]; $conversion_rate = $this->getConversionRate($nb_conversions); $this->getProcessor()->insertNumericRecord(self::getRecordName('conversion_rate', $goalId), $conversion_rate); // sum up the visits to conversion data table & the days to conversion data table $this->getProcessor()->aggregateDataTableRecords(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME, $goalId), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME, $goalId))); } // sum up goal overview reports $this->getProcessor()->aggregateDataTableRecords(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME))); }
/** * Similar to {@link get()} but does not return any metrics for new and returning visitors. It won't apply * any segment by default. This method is deprecated from the API as it is only there to make the implementation of * the actual {@link get()} method easy. * * @deprecated * @internal */ public function getMetrics($idSite, $period, $date, $segment = false, $idGoal = false, $columns = array()) { Piwik::checkUserHasViewAccess($idSite); $archive = Archive::build($idSite, $period, $date, $segment); // Mapping string idGoal to internal ID $idGoal = self::convertSpecialGoalIds($idGoal); $isEcommerceGoal = $idGoal === GoalManager::IDGOAL_ORDER || $idGoal === GoalManager::IDGOAL_CART; $allMetrics = Goals::getGoalColumns($idGoal); $columnsToShow = Piwik::getArrayFromApiParameter($columns); $requestedColumns = $columnsToShow; $shouldAddAverageOrderRevenue = (in_array('avg_order_revenue', $requestedColumns) || empty($requestedColumns)) && $isEcommerceGoal; if ($shouldAddAverageOrderRevenue && !empty($requestedColumns)) { $avgOrder = new AverageOrderRevenue(); $metricsToAdd = $avgOrder->getDependentMetrics(); $requestedColumns = array_unique(array_merge($requestedColumns, $metricsToAdd)); } $report = Report::factory('Goals', 'getMetrics'); $columnsToGet = $report->getMetricsRequiredForReport($allMetrics, $requestedColumns); $inDbMetricNames = array_map(function ($name) use($idGoal) { return $name == 'nb_visits' ? $name : Archiver::getRecordName($name, $idGoal); }, $columnsToGet); $dataTable = $archive->getDataTableFromNumeric($inDbMetricNames); if (count($columnsToGet) > 0) { $newNameMapping = array_combine($inDbMetricNames, $columnsToGet); } else { $newNameMapping = array(); } $dataTable->filter('ReplaceColumnNames', array($newNameMapping)); // TODO: this should be in Goals/Get.php but it depends on idGoal parameter which isn't always in _GET (ie, // it's not in ProcessedReport.php). more refactoring must be done to report class before this can be // corrected. if ($shouldAddAverageOrderRevenue) { $dataTable->filter(function (DataTable $table) { $extraProcessedMetrics = $table->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME); if (empty($extraProcessedMetrics)) { $extraProcessedMetrics = array(); } $extraProcessedMetrics[] = new AverageOrderRevenue(); $table->setMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME, $extraProcessedMetrics); }); } // remove temporary metrics that were not explicitly requested if (empty($columnsToShow)) { $columnsToShow = $allMetrics; $columnsToShow[] = 'conversion_rate'; if ($isEcommerceGoal) { $columnsToShow[] = 'avg_order_revenue'; } } $dataTable->queueFilter('ColumnDelete', array($columnsToRemove = array(), $columnsToShow)); return $dataTable; }
/** * 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); $archive = Archive::build($idSite, $period, $date, $segment); $columns = Piwik::getArrayFromApiParameter($columns); // Mapping string idGoal to internal ID $idGoal = self::convertSpecialGoalIds($idGoal); if (empty($columns)) { $columns = Goals::getGoalColumns($idGoal); if ($idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) { $columns[] = 'avg_order_revenue'; } } if (in_array('avg_order_revenue', $columns) && $idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) { $columns[] = 'nb_conversions'; $columns[] = 'revenue'; $columns = array_values(array_unique($columns)); } $columnsToSelect = array(); foreach ($columns as &$columnName) { $columnsToSelect[] = Archiver::getRecordName($columnName, $idGoal); } $dataTable = $archive->getDataTableFromNumeric($columnsToSelect); // Rewrite column names as we expect them foreach ($columnsToSelect as $id => $oldName) { $dataTable->renameColumn($oldName, $columns[$id]); } if ($idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER) { if ($dataTable instanceof DataTable\Map) { foreach ($dataTable->getDataTables() as $row) { $this->enrichTable($row); } } else { $this->enrichTable($dataTable); } } return $dataTable; }
/** * @internal param $this->getProcessor() */ public function aggregateMultipleReports() { /* * Archive Ecommerce Items */ $dataTableToSum = $this->dimensionRecord; foreach ($this->dimensionRecord as $recordName) { $dataTableToSum[] = self::getItemRecordNameAbandonedCart($recordName); } $columnsAggregationOperation = null; $this->getProcessor()->aggregateDataTableRecords($dataTableToSum, $maximumRowsInDataTableLevelZero = null, $maximumRowsInSubDataTable = null, $columnToSortByBeforeTruncation = null, $columnsAggregationOperation, $columnsToRenameAfterAggregation = null, $countRowsRecursive = array()); /* * Archive General Goal metrics */ $goalIdsToSum = GoalManager::getGoalIds($this->getProcessor()->getParams()->getSite()->getId()); //Ecommerce $goalIdsToSum[] = GoalManager::IDGOAL_ORDER; $goalIdsToSum[] = GoalManager::IDGOAL_CART; //bug here if idgoal=1 // Overall goal metrics $goalIdsToSum[] = false; $fieldsToSum = array(); foreach ($goalIdsToSum as $goalId) { $metricsToSum = Goals::getGoalColumns($goalId); foreach ($metricsToSum as $metricName) { $fieldsToSum[] = self::getRecordName($metricName, $goalId); } } $this->getProcessor()->aggregateNumericMetrics($fieldsToSum); $columnsAggregationOperation = null; foreach ($goalIdsToSum as $goalId) { // sum up the visits to conversion data table & the days to conversion data table $this->getProcessor()->aggregateDataTableRecords(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME, $goalId), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME, $goalId)), $maximumRowsInDataTableLevelZero = null, $maximumRowsInSubDataTable = null, $columnToSortByBeforeTruncation = null, $columnsAggregationOperation, $columnsToRenameAfterAggregation = null, $countRowsRecursive = array()); } $columnsAggregationOperation = null; // sum up goal overview reports $this->getProcessor()->aggregateDataTableRecords(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME)), $maximumRowsInDataTableLevelZero = null, $maximumRowsInSubDataTable = null, $columnToSortByBeforeTruncation = null, $columnsAggregationOperation, $columnsToRenameAfterAggregation = null, $countRowsRecursive = array()); }