Example #1
0
 function index()
 {
     $view = new Piwik_View('Goals/templates/overview.tpl');
     $view->currency = Piwik::getCurrency();
     $view->title = 'All goals - evolution';
     $view->graphEvolution = $this->getEvolutionGraph(true, array(Piwik_Goals::getRecordName('nb_conversions')));
     $view->nameGraphEvolution = 'GoalsgetEvolutionGraph';
     // sparkline for the historical data of the above values
     $view->urlSparklineConversions = $this->getUrlSparkline('getEvolutionGraph', array('columns' => array(Piwik_Goals::getRecordName('nb_conversions'))));
     $view->urlSparklineConversionRate = $this->getUrlSparkline('getEvolutionGraph', array('columns' => array(Piwik_Goals::getRecordName('conversion_rate'))));
     $view->urlSparklineRevenue = $this->getUrlSparkline('getEvolutionGraph', array('columns' => array(Piwik_Goals::getRecordName('revenue'))));
     $request = new Piwik_API_Request("method=Goals.get&format=original");
     $datatable = $request->process();
     $dataRow = $datatable->getFirstRow();
     $view->nb_conversions = $dataRow->getColumn('Goal_nb_conversions');
     $view->conversion_rate = $dataRow->getColumn('Goal_conversion_rate');
     $view->revenue = $dataRow->getColumn('Goal_revenue');
     $goalMetrics = array();
     foreach ($this->goals as $idGoal => $goal) {
         $goalMetrics[$idGoal] = $this->getMetricsForGoal($idGoal);
         $goalMetrics[$idGoal]['name'] = $goal['name'];
     }
     $view->goalMetrics = $goalMetrics;
     $view->goals = $this->goals;
     $view->goalsJSON = json_encode($this->goals);
     $view->userCanEditGoals = Piwik::isUserHasAdminAccess($this->idSite);
     echo $view->render();
 }
Example #2
0
 public function index()
 {
     $view = $this->getOverviewView();
     $view->goalsJSON = json_encode($this->goals);
     $view->goalDimensions = Piwik_Goals::getReportsWithGoalMetrics();
     $view->userCanEditGoals = Piwik::isUserHasAdminAccess($this->idSite);
     $view->displayFullReport = true;
     echo $view->render();
 }
 public function getApiToTest()
 {
     $result = parent::getApiToTest();
     // 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,' . Piwik_Goals::getRecordName('conversion_rate');
     $result[] = array('VisitsSummary.get', array('idSite' => 'all', 'date' => $dateTime, 'periods' => 'range', 'otherRequestParameters' => array('columns' => $columns), 'testSuffix' => '_getMetricsFromDifferentReports'));
     return $result;
 }
 public function getApiForTesting()
 {
     // 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' => self::$dateTime, 'periods' => $periods, 'setDateLastN' => true)), array($apiToCall, array('idSite' => self::$idSite1, 'date' => self::$dateTime, 'periods' => $periods, 'setDateLastN' => true, 'testSuffix' => '_idSiteOne_')), array($singlePeriodApi, array('idSite' => 'all', 'date' => self::$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' => self::$idSite1, 'date' => self::$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,' . Piwik_Goals::getRecordName('conversion_rate');
     $result[] = array('VisitsSummary.get', array('idSite' => 'all', 'date' => $dateTime, 'periods' => 'range', 'otherRequestParameters' => array('columns' => $columns), 'testSuffix' => '_getMetricsFromDifferentReports'));
     return $result;
 }
Example #5
0
 private function addReportData()
 {
     $archive = Piwik_Archive::build($this->idSite, 'year', $this->dateTime);
     $archive->getNumeric('nb_visits', 'nb_hits');
     Piwik_VisitorInterest_API::getInstance()->getNumberOfVisitsPerVisitDuration($this->idSite, 'year', $this->dateTime);
     // months are added via the 'year' period, but weeks must be done manually
     for ($daysAgo = $this->daysAgoStart; $daysAgo > 0; $daysAgo -= 7) {
         $dateTime = $this->dateTime->subDay($daysAgo);
         $archive = Piwik_Archive::build($this->idSite, 'week', $dateTime);
         $archive->getNumeric('nb_visits');
         Piwik_VisitorInterest_API::getInstance()->getNumberOfVisitsPerVisitDuration($this->idSite, 'week', $dateTime);
     }
     // add segment for one day
     $archive = Piwik_Archive::build($this->idSite, 'day', '2012-01-14', 'browserName==FF');
     $archive->getNumeric('nb_visits', 'nb_hits');
     Piwik_VisitorInterest_API::getInstance()->getNumberOfVisitsPerVisitDuration($this->idSite, 'day', '2012-01-14', 'browserName==FF');
     // add range within January
     $rangeEnd = Piwik_Date::factory('2012-01-29');
     $rangeStart = $rangeEnd->subDay(1);
     $range = $rangeStart->toString('Y-m-d') . "," . $rangeEnd->toString('Y-m-d');
     $rangeArchive = Piwik_Archive::build($this->idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     Piwik_VisitorInterest_API::getInstance()->getNumberOfVisitsPerVisitDuration($this->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 = Piwik_Archive::build($this->idSite, 'range', $range);
     $rangeArchive->getNumeric('nb_visits', 'nb_hits');
     Piwik_VisitorInterest_API::getInstance()->getNumberOfVisitsPerVisitDuration($this->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', Piwik_Goals::getRecordName('revenue'), Piwik_Goals::getRecordName('nb_conversions', 1), Piwik_Goals::getRecordName('revenue', Piwik_Tracker_GoalManager::IDGOAL_ORDER));
     $archiveTables = $this->getArchiveTableNames();
     foreach ($archiveTables['numeric'] as $table) {
         $realTable = Piwik_Common::prefixTable($table);
         Piwik_Query("DELETE FROM {$realTable} WHERE name NOT IN ('" . implode("','", $metricsToSave) . "') AND name NOT LIKE 'done%'");
     }
     foreach ($archiveTables['blob'] as $table) {
         $realTable = Piwik_Common::prefixTable($table);
         Piwik_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\t\t                VALUES (10000,?,1,?,?,?,?,?)";
     // one metric for jan & one for feb
     Piwik_Query(sprintf($sql, Piwik_Common::prefixTable($archiveTables['numeric'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 1, 100));
     Piwik_Query(sprintf($sql, Piwik_Common::prefixTable($archiveTables['numeric'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 1, 200));
     // add garbage reports
     Piwik_Query(sprintf($sql, Piwik_Common::prefixTable($archiveTables['blob'][0])), array(self::GARBAGE_FIELD, $janDate1, $janDate1, $janDate1, 10, 'blobval'));
     Piwik_Query(sprintf($sql, Piwik_Common::prefixTable($archiveTables['blob'][1])), array(self::GARBAGE_FIELD, $febDate1, $febDate1, $febDate1, 20, 'blobval'));
 }
Example #6
0
 /**
  * Returns a report displaying the total visits, actions and revenue, as
  * well as the evolution of these values, of all existing sites over a
  * specified period of time.
  * 
  * If the specified period is not a 'range', this function will calculcate
  * evolution metrics. Evolution metrics are metrics that display the
  * percent increase/decrease of another metric since the last period.
  * 
  * This function will merge the result of the archive query so each
  * row in the result DataTable will correspond to the metrics of a single
  * site. If a date range is specified, the result will be a
  * DataTable_Array, but it will still be merged.
  * 
  * @param string $period The period type to get data for.
  * @param string $date The date(s) to get data for.
  * @param string $segment The segments to get data for.
  */
 public function getAll($period, $date, $segment = false)
 {
     Piwik::checkUserHasSomeViewAccess();
     $isGoalPluginEnabled = Piwik_Common::isGoalPluginEnabled();
     // get site data for every viewable site and cache them
     if (Piwik::isUserIsSuperUser()) {
         $sites = Piwik_SitesManager_API::getInstance()->getAllSites();
         Piwik_Site::setSites($sites);
     } else {
         $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess();
         Piwik_Site::setSitesFromArray($sites);
     }
     // build the archive type used to query archive data
     $archive = Piwik_Archive::build('all', $period, $date, $segment);
     // determine what data will be displayed
     $fieldsToGet = array('nb_visits', 'nb_actions');
     if ($isGoalPluginEnabled) {
         $revenueMetric = Piwik_Goals::getRecordName('revenue');
         $fieldsToGet[] = $revenueMetric;
     }
     // get the data
     $dataTable = $archive->getDataTableFromNumeric($fieldsToGet);
     // get rid of the DataTable_Array that is created by the IndexedBySite archive type
     $dataTable = $dataTable->mergeChildren();
     // if the period isn't a range & a lastN/previousN date isn't used, we get the same
     // data for the last period to show the evolution of visits/actions/revenue
     if ($period != 'range' && !preg_match('/(last|previous)([0-9]*)/', $date, $regs)) {
         if (strpos($date, ',')) {
             $rangePeriod = new Piwik_Period_Range($period, $date);
             $lastStartDate = Piwik_Period_Range::removePeriod($period, $rangePeriod->getDateStart(), $n = 1);
             $lastEndDate = Piwik_Period_Range::removePeriod($period, $rangePeriod->getDateEnd(), $n = 1);
             $strLastDate = "{$lastStartDate},{$lastEndDate}";
         } else {
             $strLastDate = Piwik_Period_Range::removePeriod($period, Piwik_Date::factory($date), $n = 1)->toString();
         }
         $pastArchive = Piwik_Archive::build('all', $period, $strLastDate, $segment);
         $pastData = $pastArchive->getDataTableFromNumeric($fieldsToGet);
         $pastData = $pastData->mergeChildren();
         // use past data to calculate evolution percentages
         $this->calculateEvolutionPercentages($dataTable, $pastData, $fieldsToGet);
     }
     // move the site id to a metadata column
     $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'idsite'));
     // set the label of each row to the site name
     $getNameFor = array('Piwik_Site', 'getNameFor');
     $dataTable->filter('ColumnCallbackReplace', array('label', $getNameFor));
     // rename the revenue column from the metric name to 'revenue'
     if ($isGoalPluginEnabled) {
         $mapping = array($revenueMetric => 'revenue');
         $dataTable->filter('ReplaceColumnNames', array($mapping));
     }
     // Ensures data set sorted, for Metadata output
     $dataTable->filter('Sort', array('nb_visits', 'desc', $naturalSort = false));
     return $dataTable;
 }
Example #7
0
 /**
  * Hooks on Period archiving.
  * Sums up Goal conversions stats, and processes overall conversion rate
  *
  * @param Piwik_Event_Notification $notification
  * @return void
  */
 function archivePeriod($notification)
 {
     /**
      * @var Piwik_ArchiveProcessing 
      */
     $archiveProcessing = $notification->getNotificationObject();
     if (!$archiveProcessing->shouldProcessReportsForPlugin($this->getPluginName())) {
         return;
     }
     /*
      * Archive Ecommerce Items
      */
     if ($this->shouldArchiveEcommerceItems($archiveProcessing)) {
         $dataTableToSum = $this->dimensions;
         foreach ($this->dimensions as $recordName) {
             $dataTableToSum[] = self::getItemRecordNameAbandonedCart($recordName);
         }
         $archiveProcessing->archiveDataTable($dataTableToSum);
     }
     /*
      *  Archive General Goal metrics
      */
     $goalIdsToSum = Piwik_Tracker_GoalManager::getGoalIds($archiveProcessing->idsite);
     //Ecommerce
     $goalIdsToSum[] = Piwik_Tracker_GoalManager::IDGOAL_ORDER;
     $goalIdsToSum[] = Piwik_Tracker_GoalManager::IDGOAL_CART;
     //bug here if idgoal=1
     // Overall goal metrics
     $goalIdsToSum[] = false;
     $fieldsToSum = array();
     foreach ($goalIdsToSum as $goalId) {
         $metricsToSum = Piwik_Goals::getGoalColumns($goalId);
         unset($metricsToSum[array_search('conversion_rate', $metricsToSum)]);
         foreach ($metricsToSum as $metricName) {
             $fieldsToSum[] = self::getRecordName($metricName, $goalId);
         }
     }
     $records = $archiveProcessing->archiveNumericValuesSum($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, $archiveProcessing);
         $archiveProcessing->insertNumericRecord(self::getRecordName('conversion_rate', $goalId), $conversion_rate);
         // sum up the visits to conversion data table & the days to conversion data table
         $archiveProcessing->archiveDataTable(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME, $goalId), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME, $goalId)));
     }
     // sum up goal overview reports
     $archiveProcessing->archiveDataTable(array(self::getRecordName(self::VISITS_UNTIL_RECORD_NAME), self::getRecordName(self::DAYS_UNTIL_CONV_RECORD_NAME)));
 }
Example #8
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.
  */
 protected function getGoalSpecificDataTable($recordName, $idSite, $period, $date, $segment, $idGoal)
 {
     Piwik::checkUserHasViewAccess($idSite);
     $archive = Piwik_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(Piwik_Goals::getRecordName($recordName, $realGoalId), $idSubtable = null);
     $dataTable->queueFilter('ReplaceColumnNames');
     return $dataTable;
 }
Example #9
0
	public function getRevenue( $idSite, $period, $date, $idGoal = false )
	{
		return self::getNumeric( $idSite, $period, $date, Piwik_Goals::getRecordName('revenue', $idGoal));
	}
Example #10
0
 /**
  * @ignore
  */
 public static function getApiMetrics($enhanced)
 {
     $metrics = self::$baseMetrics;
     if (Piwik_Common::isGoalPluginEnabled()) {
         // goal revenue metric
         $metrics[self::GOAL_REVENUE_METRIC] = array(self::METRIC_TRANSLATION_KEY => 'Goals_ColumnRevenue', self::METRIC_EVOLUTION_COL_NAME_KEY => self::GOAL_REVENUE_METRIC . '_evolution', self::METRIC_RECORD_NAME_KEY => Piwik_Goals::getRecordName(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 => Piwik_Goals::getRecordName(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 => Piwik_Goals::getRecordName(self::GOAL_CONVERSION_METRIC, 0), 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 => Piwik_Goals::getRecordName(self::GOAL_REVENUE_METRIC, 0), self::METRIC_IS_ECOMMERCE_KEY => true);
         }
     }
     return $metrics;
 }
Example #11
0
 public function getRevenue($idSite, $period, $date, $segment = false, $idGoal = false)
 {
     return $this->getNumeric($idSite, $period, $date, $segment, Piwik_Goals::getRecordName('revenue', $idGoal));
 }
Example #12
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 (Piwik_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[] = Piwik_Goals::getRecordName($metric, $i);
             }
             $metricsToKeep[] = Piwik_Goals::getRecordName($metric);
             $metricsToKeep[] = Piwik_Goals::getRecordName($metric, Piwik_Tracker_GoalManager::IDGOAL_ORDER);
             $metricsToKeep[] = Piwik_Goals::getRecordName($metric, Piwik_Tracker_GoalManager::IDGOAL_CART);
         }
     }
     return $metricsToKeep;
 }