public function getApiForTesting()
 {
     $dateTime = self::$fixture->dateTime;
     $idSite1 = self::$fixture->idSite1;
     // NOTE: copied from TwoVisitors_TwoWebsites_DifferentDays (including the test or inheriting means
     // the test will get run by phpunit, even when we only want to run this one. should be put into
     // non-test class later.)
     $apiToCall = $this->getApiToCall();
     $singlePeriodApi = array('VisitsSummary.get', 'Goals.get');
     $periods = array('day', 'week', 'month', 'year');
     $result = array();
     // Live output for a quick visualisation if some other API test break
     $result[] = array('Live.getLastVisitsDetails', array('idSite' => self::$fixture->idSite1, 'date' => $dateTime, 'periods' => 'year', 'keepLiveDates' => true, 'otherRequestParameters' => array('showColumns' => 'lastActionDateTime,referrerType,referrerName,actions,events,visitConverted')));
     // Request data for the last 6 periods and idSite=all
     $result[] = array($apiToCall, array('idSite' => 'all', 'date' => $dateTime, 'periods' => $periods, 'setDateLastN' => true));
     // Request data for the last 6 periods and idSite=1
     $result[] = array($apiToCall, array('idSite' => $idSite1, 'date' => $dateTime, 'periods' => $periods, 'setDateLastN' => true, 'testSuffix' => '_idSiteOne_'));
     // We also test a single period to check that this use case (Reports per idSite in the response) works
     $result[] = array($singlePeriodApi, array('idSite' => 'all', 'date' => $dateTime, 'periods' => array('day', 'month'), 'setDateLastN' => false, 'testSuffix' => '_NotLastNPeriods'));
     // testing metadata API for multiple periods
     $apiToCall = array_diff($apiToCall, array('Actions.getPageTitle', 'Actions.getPageUrl'));
     foreach ($apiToCall as $api) {
         list($apiModule, $apiAction) = explode(".", $api);
         $result[] = array('API.getProcessedReport', array('idSite' => $idSite1, 'date' => $dateTime, 'periods' => array('day'), 'setDateLastN' => true, 'apiModule' => $apiModule, 'apiAction' => $apiAction, 'testSuffix' => '_' . $api . '_firstSite_lastN'));
     }
     // Tests that getting a visits summary metric (nb_visits) & a Goal's metric (Goal_revenue)
     // at the same time works.
     $dateTimeRange = '2010-01-03,2010-01-06';
     $columns = 'nb_visits,' . Archiver::getRecordName('conversion_rate');
     $result[] = array('VisitsSummary.get', array('idSite' => 'all', 'date' => $dateTimeRange, 'periods' => 'range', 'otherRequestParameters' => array('columns' => $columns), 'testSuffix' => '_getMetricsFromDifferentReports'));
     return $result;
 }
 public function getApiForTesting()
 {
     $dateTime = self::$fixture->dateTime;
     $idSite1 = self::$fixture->idSite1;
     // NOTE: copied from TwoVisitors_TwoWebsites_DifferentDays (including the test or inheriting means
     // the test will get run by phpunit, even when we only want to run this one. should be put into
     // non-test class later.)
     $apiToCall = $this->getApiToCall();
     $singlePeriodApi = array('VisitsSummary.get', 'Goals.get');
     $periods = array('day', 'week', 'month', 'year');
     $result = array(array($apiToCall, array('idSite' => 'all', 'date' => $dateTime, 'periods' => $periods, 'setDateLastN' => true)), array($apiToCall, array('idSite' => $idSite1, 'date' => $dateTime, 'periods' => $periods, 'setDateLastN' => true, 'testSuffix' => '_idSiteOne_')), array($singlePeriodApi, array('idSite' => 'all', 'date' => $dateTime, 'periods' => array('day', 'month'), 'setDateLastN' => false, 'testSuffix' => '_NotLastNPeriods')));
     // testing metadata API for multiple periods
     $apiToCall = array_diff($apiToCall, array('Actions.getPageTitle', 'Actions.getPageUrl'));
     foreach ($apiToCall as $api) {
         list($apiModule, $apiAction) = explode(".", $api);
         $result[] = array('API.getProcessedReport', array('idSite' => $idSite1, 'date' => $dateTime, 'periods' => array('day'), 'setDateLastN' => true, 'apiModule' => $apiModule, 'apiAction' => $apiAction, 'testSuffix' => '_' . $api . '_firstSite_lastN'));
     }
     // Tests that getting a visits summary metric (nb_visits) & a Goal's metric (Goal_revenue)
     // at the same time works.
     $dateTime = '2010-01-03,2010-01-06';
     $columns = 'nb_visits,' . Archiver::getRecordName('conversion_rate');
     $result[] = array('VisitsSummary.get', array('idSite' => 'all', 'date' => $dateTime, 'periods' => 'range', 'otherRequestParameters' => array('columns' => $columns), 'testSuffix' => '_getMetricsFromDifferentReports'));
     return $result;
 }
Пример #3
0
 /**
  * Returns the names of metrics that should be kept when purging as they appear in
  * archive tables.
  */
 public static function getAllMetricsToKeep()
 {
     $metricsToKeep = self::getMetricsToKeep();
     // convert goal metric names to correct archive names
     if (Common::isGoalPluginEnabled()) {
         $goalMetricsToKeep = self::getGoalMetricsToKeep();
         $maxGoalId = self::getMaxGoalId();
         // for each goal metric, there's a different name for each goal, including the overview,
         // the order report & cart report
         foreach ($goalMetricsToKeep as $metric) {
             for ($i = 1; $i <= $maxGoalId; ++$i) {
                 $metricsToKeep[] = Archiver::getRecordName($metric, $i);
             }
             $metricsToKeep[] = Archiver::getRecordName($metric);
             $metricsToKeep[] = Archiver::getRecordName($metric, GoalManager::IDGOAL_ORDER);
             $metricsToKeep[] = Archiver::getRecordName($metric, GoalManager::IDGOAL_CART);
         }
     }
     return $metricsToKeep;
 }
Пример #4
0
 /**
  * Utility method that retrieve an archived DataTable for a specific site, date range,
  * segment and goal. If not goal is specified, this method will retrieve and sum the
  * data for every goal.
  *
  * @param string $recordName The archive entry name.
  * @param int|string $idSite The site(s) to select data for.
  * @param string $period The period type.
  * @param string $date The date type.
  * @param string $segment The segment.
  * @param int|bool $idGoal The id of the goal to get data for. If this is set to false,
  *                         data for every goal that belongs to $idSite is returned.
  * @return false|DataTable
  */
 protected function getGoalSpecificDataTable($recordName, $idSite, $period, $date, $segment, $idGoal)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Archive::build($idSite, $period, $date, $segment);
     // check for the special goal ids
     $realGoalId = $idGoal != true ? false : self::convertSpecialGoalIds($idGoal);
     // get the data table
     $dataTable = $archive->getDataTable(Archiver::getRecordName($recordName, $realGoalId), $idSubtable = null);
     $dataTable->queueFilter('ReplaceColumnNames');
     return $dataTable;
 }
Пример #5
0
 /**
  * @ignore
  */
 public static function getApiMetrics($enhanced)
 {
     $metrics = self::$baseMetrics;
     if (Common::isActionsPluginEnabled()) {
         $metrics[self::NB_PAGEVIEWS_LABEL] = array(self::METRIC_TRANSLATION_KEY => 'General_ColumnPageviews', self::METRIC_EVOLUTION_COL_NAME_KEY => 'pageviews_evolution', self::METRIC_RECORD_NAME_KEY => self::NB_PAGEVIEWS_METRIC, self::METRIC_COL_NAME_KEY => self::NB_PAGEVIEWS_LABEL, self::METRIC_IS_ECOMMERCE_KEY => false);
     }
     if (Common::isGoalPluginEnabled()) {
         // goal revenue metric
         $metrics[self::GOAL_REVENUE_METRIC] = array(self::METRIC_TRANSLATION_KEY => 'General_ColumnRevenue', self::METRIC_EVOLUTION_COL_NAME_KEY => self::GOAL_REVENUE_METRIC . '_evolution', self::METRIC_RECORD_NAME_KEY => Archiver::getRecordName(self::GOAL_REVENUE_METRIC), self::METRIC_COL_NAME_KEY => self::GOAL_REVENUE_METRIC, self::METRIC_IS_ECOMMERCE_KEY => false);
         if ($enhanced) {
             // number of goal conversions metric
             $metrics[self::GOAL_CONVERSION_METRIC] = array(self::METRIC_TRANSLATION_KEY => 'Goals_ColumnConversions', self::METRIC_EVOLUTION_COL_NAME_KEY => self::GOAL_CONVERSION_METRIC . '_evolution', self::METRIC_RECORD_NAME_KEY => Archiver::getRecordName(self::GOAL_CONVERSION_METRIC), self::METRIC_COL_NAME_KEY => self::GOAL_CONVERSION_METRIC, self::METRIC_IS_ECOMMERCE_KEY => false);
             // number of orders
             $metrics[self::ECOMMERCE_ORDERS_METRIC] = array(self::METRIC_TRANSLATION_KEY => 'General_EcommerceOrders', self::METRIC_EVOLUTION_COL_NAME_KEY => self::ECOMMERCE_ORDERS_METRIC . '_evolution', self::METRIC_RECORD_NAME_KEY => Archiver::getRecordName(self::GOAL_CONVERSION_METRIC, 0), self::METRIC_COL_NAME_KEY => self::ECOMMERCE_ORDERS_METRIC, self::METRIC_IS_ECOMMERCE_KEY => true);
             // eCommerce revenue
             $metrics[self::ECOMMERCE_REVENUE_METRIC] = array(self::METRIC_TRANSLATION_KEY => 'General_ProductRevenue', self::METRIC_EVOLUTION_COL_NAME_KEY => self::ECOMMERCE_REVENUE_METRIC . '_evolution', self::METRIC_RECORD_NAME_KEY => Archiver::getRecordName(self::GOAL_REVENUE_METRIC, 0), self::METRIC_COL_NAME_KEY => self::ECOMMERCE_REVENUE_METRIC, self::METRIC_IS_ECOMMERCE_KEY => true);
         }
     }
     return $metrics;
 }
Пример #6
0
 protected static function _addReportData()
 {
     $date = Date::factory(self::$dateTime);
     $archive = Archive::build(self::$idSite, 'year', $date);
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'year', $date);
     //        APIVisitorInterest::getInstance()->get(self::$idSite, 'month', $date, $segment = false, self::$idSite);
     // months are added via the 'year' period, but weeks must be done manually
     for ($daysAgo = self::$daysAgoStart; $daysAgo > 0; $daysAgo -= 7) {
         $dateTime = $date->subDay($daysAgo);
         $archive = Archive::build(self::$idSite, 'week', $dateTime);
         $archive->getNumeric('nb_visits');
         APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'week', $dateTime);
     }
     // add segment for one day
     $archive = Archive::build(self::$idSite, 'day', '2012-01-14', 'browserCode==FF');
     $archive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'day', '2012-01-14', 'browserCode==FF');
     // add range within January
     $rangeEnd = Date::factory('2012-01-29');
     $rangeStart = $rangeEnd->subDay(1);
     $range = $rangeStart->toString('Y-m-d') . "," . $rangeEnd->toString('Y-m-d');
     $rangeArchive = Archive::build(self::$idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'range', $range);
     // add range between January & February
     $rangeStart = $rangeEnd;
     $rangeEnd = $rangeStart->addDay(3);
     $range = $rangeStart->toString('Y-m-d') . "," . $rangeEnd->toString('Y-m-d');
     $rangeArchive = Archive::build(self::$idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     APIVisitorInterest::getInstance()->getNumberOfVisitsPerVisitDuration(self::$idSite, 'range', $range);
     // when archiving is initiated, the archive metrics & reports for EVERY loaded plugin
     // are archived. don't want this test to depend on every possible metric, so get rid of
     // the unwanted archive data now.
     $metricsToSave = array('nb_visits', 'nb_actions', Archiver::getRecordName('revenue'), Archiver::getRecordName('nb_conversions', 1), Archiver::getRecordName('revenue', GoalManager::IDGOAL_ORDER));
     $archiveTables = self::_getArchiveTableNames();
     foreach ($archiveTables['numeric'] as $table) {
         $realTable = Common::prefixTable($table);
         $sql = "DELETE FROM {$realTable} WHERE name NOT IN ('" . implode("','", $metricsToSave) . "') AND name NOT LIKE 'done%'";
         Db::query($sql);
     }
     foreach ($archiveTables['blob'] as $table) {
         $realTable = Common::prefixTable($table);
         Db::query("DELETE FROM {$realTable} WHERE name NOT IN ('VisitorInterest_timeGap')");
     }
     // add garbage metrics
     $janDate1 = '2012-01-05';
     $febDate1 = '2012-02-04';
     $sql = "INSERT INTO %s (idarchive,name,idsite,date1,date2,period,ts_archived,value)\n                        VALUES (10000,?,1,?,?,?,?,?)";
     // one metric for jan & one for feb
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 1, 100));
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['numeric'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 1, 200));
     // add garbage reports
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 10, 'blobval'));
     Db::query(sprintf($sql, Common::prefixTable($archiveTables['blob'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 20, 'blobval'));
 }
Пример #7
0
 /**
  * Sets the total visits, actions & revenue for a DataTable returned by
  * $this->buildDataTable.
  *
  * @param DataTable $dataTable
  * @param array $apiMetrics Metrics info.
  * @return array Array of three values: total visits, total actions, total revenue
  */
 private function setMetricsTotalsMetadata($dataTable, $apiMetrics)
 {
     if ($dataTable instanceof DataTable\Map) {
         foreach ($dataTable->getDataTables() as $table) {
             $this->setMetricsTotalsMetadata($table, $apiMetrics);
         }
     } else {
         $revenueMetric = '';
         if (Common::isGoalPluginEnabled()) {
             $revenueMetric = Archiver::getRecordName(self::GOAL_REVENUE_METRIC);
         }
         $totals = array();
         foreach ($apiMetrics as $label => $metricInfo) {
             $totalMetadataName = self::getTotalMetadataName($label);
             $totals[$totalMetadataName] = 0;
         }
         foreach ($dataTable->getRows() as $row) {
             foreach ($apiMetrics as $label => $metricInfo) {
                 $totalMetadataName = self::getTotalMetadataName($label);
                 $totals[$totalMetadataName] += $row->getColumn($metricInfo[self::METRIC_RECORD_NAME_KEY]);
             }
         }
         foreach ($totals as $name => $value) {
             $dataTable->setMetadata($name, $value);
         }
     }
 }