/** * General method to get transitions for an action * * @param $actionName * @param $actionType "url"|"title" * @param $idSite * @param $period * @param $date * @param bool $segment * @param bool $limitBeforeGrouping * @param string $parts * @param bool $returnNormalizedUrls * @return array * @throws Exception */ public function getTransitionsForAction($actionName, $actionType, $idSite, $period, $date, $segment = false, $limitBeforeGrouping = false, $parts = 'all', $returnNormalizedUrls = false) { Piwik::checkUserHasViewAccess($idSite); // get idaction of the requested action $idaction = $this->deriveIdAction($actionName, $actionType); if ($idaction < 0) { throw new Exception('NoDataForAction'); } // prepare archive processing that can be used by the archiving code $archiveProcessing = new Piwik_ArchiveProcessing_Day(); $archiveProcessing->setSite(new Piwik_Site($idSite)); $archiveProcessing->setPeriod(Piwik_Period::advancedFactory($period, $date)); $archiveProcessing->setSegment(new Piwik_Segment($segment, $idSite)); $archiveProcessing->initForLiveUsage(); // prepare the report $report = array('date' => Piwik_Period_Day::advancedFactory($period, $date)->getLocalizedShortString()); // add data to the report $transitionsArchiving = new Piwik_Transitions(); if ($returnNormalizedUrls) { $transitionsArchiving->returnNormalizedUrls(); } $partsArray = explode(',', $parts); if ($parts == 'all' || in_array('internalReferrers', $partsArray)) { $this->addInternalReferrers($transitionsArchiving, $archiveProcessing, $report, $idaction, $actionType, $limitBeforeGrouping); } if ($parts == 'all' || in_array('followingActions', $partsArray)) { $includeLoops = $parts != 'all' && !in_array('internalReferrers', $partsArray); $this->addFollowingActions($transitionsArchiving, $archiveProcessing, $report, $idaction, $actionType, $limitBeforeGrouping, $includeLoops); } if ($parts == 'all' || in_array('externalReferrers', $partsArray)) { $this->addExternalReferrers($transitionsArchiving, $archiveProcessing, $report, $idaction, $actionType, $limitBeforeGrouping); } // derive the number of exits from the other metrics if ($parts == 'all') { $report['pageMetrics']['exits'] = $report['pageMetrics']['pageviews'] - $transitionsArchiving->getTotalTransitionsToFollowingActions() - $report['pageMetrics']['loops']; } // replace column names in the data tables $reportNames = array('previousPages' => true, 'previousSiteSearches' => false, 'followingPages' => true, 'followingSiteSearches' => false, 'outlinks' => true, 'downloads' => true); foreach ($reportNames as $reportName => $replaceLabel) { if (isset($report[$reportName])) { $columnNames = array(Piwik_Archive::INDEX_NB_ACTIONS => 'referrals'); if ($replaceLabel) { $columnNames[Piwik_Archive::INDEX_NB_ACTIONS] = 'referrals'; } $report[$reportName]->filter('ReplaceColumnNames', array($columnNames)); } } return $report; }
public function getProcessedReport($idSite, $period, $date, $apiModule, $apiAction, $segment = false, $apiParameters = false, $idGoal = false, $language = false, $showTimer = true, $hideMetricsDoc = false, $idSubtable = false) { $timer = new Piwik_Timer(); if ($apiParameters === false) { $apiParameters = array(); } if (!empty($idGoal) && empty($apiParameters['idGoal'])) { $apiParameters['idGoal'] = $idGoal; } // Is this report found in the Metadata available reports? $reportMetadata = $this->getMetadata($idSite, $apiModule, $apiAction, $apiParameters, $language, $period, $date, $hideMetricsDoc, $showSubtableReports = true); if (empty($reportMetadata)) { throw new Exception("Requested report {$apiModule}.{$apiAction} for Website id={$idSite} not found in the list of available reports. \n"); } $reportMetadata = reset($reportMetadata); // Generate Api call URL passing custom parameters $parameters = array_merge($apiParameters, array('method' => $apiModule . '.' . $apiAction, 'idSite' => $idSite, 'period' => $period, 'date' => $date, 'format' => 'original', 'serialize' => '0', 'language' => $language, 'idSubtable' => $idSubtable)); if (!empty($segment)) { $parameters['segment'] = $segment; } $url = Piwik_Url::getQueryStringFromParameters($parameters); $request = new Piwik_API_Request($url); try { /** @var Piwik_DataTable */ $dataTable = $request->process(); } catch (Exception $e) { throw new Exception("API returned an error: " . $e->getMessage() . "\n"); } list($newReport, $columns, $rowsMetadata) = $this->handleTableReport($idSite, $dataTable, $reportMetadata, isset($reportMetadata['dimension'])); foreach ($columns as $columnId => &$name) { $name = ucfirst($name); } $website = new Piwik_Site($idSite); // $segment = new Piwik_Segment($segment, $idSite); $period = Piwik_Period::advancedFactory($period, $date); $period = $period->getLocalizedLongString(); $return = array('website' => $website->getName(), 'prettyDate' => $period, 'metadata' => $reportMetadata, 'columns' => $columns, 'reportData' => $newReport, 'reportMetadata' => $rowsMetadata); if ($showTimer) { $return['timerMillis'] = $timer->getTimeMs(0); } return $return; }
/** * Add transitions data to the report. * Fake ArchiveProcessing to do the queries live. */ private function addLiveTransitionsDataToReport(&$report, $pageUrl, $idSite, $period, $date, $segment, $limitBeforeGrouping) { // get idaction of page url $actionsPlugin = new Piwik_Actions(); $idaction = $actionsPlugin->getIdActionFromSegment($pageUrl, 'idaction'); // prepare archive processing that can be reused by the archiving code $archiveProcessing = new Piwik_ArchiveProcessing_Day(); $archiveProcessing->setSite(new Piwik_Site($idSite)); $archiveProcessing->setPeriod(Piwik_Period::advancedFactory($period, $date)); $archiveProcessing->setSegment(new Piwik_Segment($segment, $idSite)); $archiveProcessing->initForLiveUsage(); // launch the archiving code - but live $transitionsArchiving = new Piwik_Transitions(); $data = $transitionsArchiving->queryInternalReferrers($idaction, $archiveProcessing, $limitBeforeGrouping); $report['previousPages'] =& $data['previousPages']; $report['pageMetrics']['loops'] = intval($data['loops']); $data = $transitionsArchiving->queryFollowingActions($idaction, $archiveProcessing, $limitBeforeGrouping); foreach ($data as $tableName => $table) { $report[$tableName] = $table; } $data = $transitionsArchiving->queryExternalReferrers($idaction, $archiveProcessing, $limitBeforeGrouping); $report['pageMetrics']['entries'] = 0; $report['referrers'] = array(); foreach ($data->getRows() as $row) { $referrerId = $row->getColumn('label'); $visits = $row->getColumn(Piwik_Archive::INDEX_NB_VISITS); if ($visits) { // load details (i.e. subtables) $details = array(); if ($idSubTable = $row->getIdSubDataTable()) { $subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable); foreach ($subTable->getRows() as $subRow) { $details[] = array('label' => $subRow->getColumn('label'), 'referrals' => $subRow->getColumn(Piwik_Archive::INDEX_NB_VISITS)); } } $report['referrers'][] = array('label' => $this->getReferrerLabel($referrerId), 'shortName' => Piwik_getRefererTypeFromShortName($referrerId), 'visits' => $visits, 'details' => $details); $report['pageMetrics']['entries'] += $visits; } } // if there's no data for referrers, Piwik_API_ResponseBuilder::handleMultiDimensionalArray // does not detect the multi dimensional array and the data is rendered differently, which // causes an exception. if (count($report['referrers']) == 0) { $report['referrers'][] = array('label' => $this->getReferrerLabel(Piwik_Common::REFERER_TYPE_DIRECT_ENTRY), 'shortName' => Piwik_getRefererTypeLabel(Piwik_Common::REFERER_TYPE_DIRECT_ENTRY), 'visits' => 0); } }