/** * @param DataTable $table * @param int $idSite * @param string $period * @param string $date * @return mixed */ protected function removeHoursInFuture($table, $idSite, $period, $date) { $site = new Site($idSite); if ($period == 'day' && ($date == 'today' || $date == Date::factory('now', $site->getTimezone())->toString())) { $currentHour = Date::factory('now', $site->getTimezone())->toString('G'); // If no data for today, this is an exception to the API output rule, as we normally return nothing: // we shall return all hours of the day, with nb_visits = 0 if ($table->getRowsCount() == 0) { for ($hour = 0; $hour <= $currentHour; $hour++) { $table->addRowFromSimpleArray(array('label' => $hour, 'nb_visits' => 0)); } return $table; } $idsToDelete = array(); foreach ($table->getRows() as $id => $row) { $hour = $row->getColumn('label'); if ($hour > $currentHour) { $idsToDelete[] = $id; } } $table->deleteRows($idsToDelete); } return $table; }
/** * See {@link GroupBy}. * * @param DataTable $table */ public function filter($table) { $groupByRows = array(); $nonGroupByRowIds = array(); foreach ($table->getRows() as $rowId => $row) { // skip the summary row if ($rowId == DataTable::ID_SUMMARY_ROW) { continue; } // reduce the group by column of this row $groupByColumnValue = $row->getColumn($this->groupByColumn); $parameters = array_merge(array($groupByColumnValue), $this->parameters); $groupByValue = call_user_func_array($this->reduceFunction, $parameters); if (!isset($groupByRows[$groupByValue])) { // if we haven't encountered this group by value before, we mark this row as a // row to keep, and change the group by column to the reduced value. $groupByRows[$groupByValue] = $row; $row->setColumn($this->groupByColumn, $groupByValue); } else { // if we have already encountered this group by value, we add this row to the // row that will be kept, and mark this one for deletion $groupByRows[$groupByValue]->sumRow($row, $copyMeta = true, $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME)); $nonGroupByRowIds[] = $rowId; } } // delete the unneeded rows. $table->deleteRows($nonGroupByRowIds); }
/** * Adds the processed metrics. See {@link AddColumnsProcessedMetrics} for * more information. * * @param DataTable $table */ public function filter($table) { $rowsIdToDelete = array(); foreach ($table->getRows() as $key => $row) { $nbVisits = $this->getColumn($row, Metrics::INDEX_NB_VISITS); $nbActions = $this->getColumn($row, Metrics::INDEX_NB_ACTIONS); if ($nbVisits == 0 && $nbActions == 0 && $this->deleteRowsWithNoVisit) { // case of keyword/website/campaign with a conversion for this day, // but no visit, we don't show it $rowsIdToDelete[] = $key; continue; } $nbVisitsConverted = (int) $this->getColumn($row, Metrics::INDEX_NB_VISITS_CONVERTED); if ($nbVisitsConverted > 0) { $conversionRate = round(100 * $nbVisitsConverted / $nbVisits, $this->roundPrecision); try { $row->addColumn('conversion_rate', $conversionRate . "%"); } catch (\Exception $e) { // conversion_rate can be defined upstream apparently? FIXME } } if ($nbVisits == 0) { $actionsPerVisit = $averageTimeOnSite = $bounceRate = $this->invalidDivision; } else { // nb_actions / nb_visits => Actions/visit // sum_visit_length / nb_visits => Avg. Time on Site // bounce_count / nb_visits => Bounce Rate $actionsPerVisit = round($nbActions / $nbVisits, $this->roundPrecision); $visitLength = $this->getColumn($row, Metrics::INDEX_SUM_VISIT_LENGTH); $averageTimeOnSite = round($visitLength / $nbVisits, $rounding = 0); $bounceRate = round(100 * $this->getColumn($row, Metrics::INDEX_BOUNCE_COUNT) / $nbVisits, $this->roundPrecision); } try { $row->addColumn('nb_actions_per_visit', $actionsPerVisit); $row->addColumn('avg_time_on_site', $averageTimeOnSite); // It could be useful for API users to have raw sum length value. //$row->addMetadata('sum_visit_length', $visitLength); } catch (\Exception $e) { } try { $row->addColumn('bounce_rate', $bounceRate . "%"); } catch (\Exception $e) { } $this->filterSubTable($row); } $table->deleteRows($rowsIdToDelete); }