function funnelReport() { $idFunnel = Piwik_Common::getRequestVar('idFunnel', null, 'int'); if (!isset($this->funnels[$idFunnel])) { Piwik::redirectToModule('Funnels', 'index', array('idFunnel' => null)); } // Set up the view $view = Piwik_View::factory('single_funnel'); $this->setGeneralVariablesView($view); // Get the funnel and related goal data $funnelDefinition = $this->funnels[$idFunnel]; $idGoal = $funnelDefinition['idgoal']; $goal_request = new Piwik_API_Request("method=Goals.get&format=original&idGoal={$idGoal}"); $datatable = $goal_request->process(); $dataRow = $datatable->getFirstRow(); $view->goal_conversions = $dataRow->getColumn('nb_conversions'); $view->name = $funnelDefinition['goal_name']; // Get the data on each funnel step $funnel_data = $this->getMetricsForFunnel($idFunnel); foreach ($funnelDefinition['steps'] as &$step) { $recordName = Piwik_Funnels::getRecordName('nb_actions', $idFunnel, $step['idstep']); $step['nb_actions'] = $funnel_data->getColumn($recordName); $recordName = Piwik_Funnels::getRecordName('nb_next_step_actions', $idFunnel, $step['idstep']); $step['nb_next_step_actions'] = $funnel_data->getColumn($recordName); $recordName = Piwik_Funnels::getRecordName('percent_next_step_actions', $idFunnel, $step['idstep']); $step['percent_next_step_actions'] = round($funnel_data->getColumn($recordName), self::ROUNDING_PRECISION); $recordName = Piwik_Funnels::getRecordName('nb_entry', $idFunnel, $step['idstep']); $step['nb_entry'] = $funnel_data->getColumn($recordName); $step['referring_actions'] = array(); $refUrls = $this->getRefUrls($idFunnel, $step['idstep']); $refUrls->filter('Sort', array('value', 'desc')); $refUrls->filter('Limit', array(0, self::NUM_URLS_TO_DISPLAY)); foreach ($refUrls->getRows() as $row) { $label = $this->labelOrDefault($row->getColumn('label'), '(entrance)'); $step['referring_actions'][] = array('label' => $label, 'value' => $row->getColumn('value')); } $recordName = Piwik_Funnels::getRecordName('nb_exit', $idFunnel, $step['idstep']); $step['nb_exit'] = $funnel_data->getColumn($recordName); $step['next_actions'] = array(); $nextUrls = $this->getNextUrls($idFunnel, $step['idstep']); $nextUrls->filter('Sort', array('value', 'desc')); $nextUrls->filter('Limit', array(0, self::NUM_URLS_TO_DISPLAY)); foreach ($nextUrls->getRows() as $row) { $label = $this->labelOrDefault($row->getColumn('label'), '(exit)'); $step['next_actions'][] = array('label' => $label, 'value' => $row->getColumn('value')); } } // What percent of people who visited the first funnel step converted at the end of the funnel? $recordName = Piwik_Funnels::getRecordName('conversion_rate', $idFunnel, false); $view->conversion_rate = round($funnel_data->getColumn($recordName), self::ROUNDING_PRECISION); // Let the view access the funnel steps $view->steps = $funnelDefinition['steps']; echo $view->render(); }
function archivePeriod($notification) { $archiveProcessing = $notification->getNotificationObject(); $funnelMetricsToSum = array('nb_actions'); $stepMetricsToSum = array('nb_actions', 'nb_next_step_actions', 'nb_entry', 'nb_exit'); $stepDataTables = array('idaction_url_next', 'idaction_url_ref'); $funnels = Piwik_Funnels_API::getInstance()->getFunnels($archiveProcessing->idsite); $fieldsToSum = array(); $tablesToSum = array(); foreach ($funnels as $funnel) { $idFunnel = $funnel['idfunnel']; foreach ($funnelMetricsToSum as $metricName) { $fieldsToSum[] = self::getRecordName($metricName, $idFunnel, false); } foreach ($funnel['steps'] as $step) { $idStep = $step['idstep']; foreach ($stepMetricsToSum as $metricName) { $fieldsToSum[] = self::getRecordName($metricName, $idFunnel, $idStep); } foreach ($stepDataTables as $dataTable) { $tablesToSum[] = self::getRecordName($dataTable, $idFunnel, $idStep); } } } $numeric_records = $archiveProcessing->archiveNumericValuesSum($fieldsToSum); $rows_to_keep = Piwik_Funnels_Controller::NUM_URLS_TO_DISPLAY + 2; $blob_records = $archiveProcessing->archiveDataTable($tablesToSum, null, $rows_to_keep, null, 'value'); // also recording percent for each step going to next step, // conversion rate for funnel foreach ($funnels as $funnel) { $idFunnel = $funnel['idfunnel']; $i = 0; $funnel_start_actions = 0; foreach ($funnel['steps'] as $step) { $idStep = $step['idstep']; $nb_actions = $numeric_records[self::getRecordName('nb_actions', $idFunnel, $idStep)]->value; if ($i == 0) { $funnel_start_actions = $nb_actions; } $nb_next_step_actions = $numeric_records[self::getRecordName('nb_next_step_actions', $idFunnel, $idStep)]->value; $percent_next_step_actions = $this->percent($nb_next_step_actions, $nb_actions); $recordName = self::getRecordName('percent_next_step_actions', $idFunnel, $idStep); $archiveProcessing->insertNumericRecord($recordName, $percent_next_step_actions); $i++; } $recordName = Piwik_Funnels::getRecordName('conversion_rate', $idFunnel, false); $archiveProcessing->insertNumericRecord($recordName, $this->percent($nb_next_step_actions, $funnel_start_actions)); } }
public function get($idSite, $period, $date, $idFunnel = false, $columns = array()) { Piwik::checkUserHasViewAccess($idSite); $archive = Piwik_Archive::build($idSite, $period, $date); if (!empty($columns)) { $toFetch = $columns; } else { $toFetch = array(); $funnels = $this->getFunnels($idSite); $stepNumericColumnNames = array('nb_entry', 'nb_exit', 'nb_actions', 'nb_next_step_actions', 'percent_next_step_actions'); foreach ($stepNumericColumnNames as $columnName) { if (!empty($idFunnel)) { $funnel = $funnels[$idFunnel]; foreach ($funnel['steps'] as $step) { $toFetch[] = Piwik_Funnels::getRecordName($columnName, $idFunnel, $step['idstep']); } } } $funnelColumnNames = array('nb_actions', 'conversion_rate'); foreach ($funnelColumnNames as $columnName) { if (!empty($idFunnel)) { $funnel = $funnels[$idFunnel]; $toFetch[] = Piwik_Funnels::getRecordName($columnName, $idFunnel, false); } } } $dataTable = $archive->getDataTableFromNumeric($toFetch); return $dataTable; }