/** * Given a segment, will return a list of the most used values for this particular segment. * @param $segmentName * @param $idSite * @throws \Exception * @return array */ public function getSuggestedValuesForSegment($segmentName, $idSite) { if (empty(Config::getInstance()->General['enable_segment_suggested_values'])) { return array(); } Piwik::checkUserHasViewAccess($idSite); $maxSuggestionsToReturn = 30; $segment = $this->findSegment($segmentName, $idSite); // if segment has suggested values callback then return result from it instead $suggestedValuesCallbackRequiresTable = false; if (isset($segment['suggestedValuesCallback'])) { $suggestedValuesCallbackRequiresTable = $this->doesSuggestedValuesCallbackNeedData($segment['suggestedValuesCallback']); if (!$suggestedValuesCallbackRequiresTable) { return call_user_func($segment['suggestedValuesCallback'], $idSite, $maxSuggestionsToReturn); } } // if period=range is disabled, do not proceed if (!Period\Factory::isPeriodEnabledForAPI('range')) { return array(); } if (!empty($segment['unionOfSegments'])) { $values = array(); foreach ($segment['unionOfSegments'] as $unionSegmentName) { $unionSegment = $this->findSegment($unionSegmentName, $idSite); try { $result = $this->getSuggestedValuesForSegmentName($idSite, $unionSegment, $maxSuggestionsToReturn); if (!empty($result)) { $values = array_merge($result, $values); } } catch (\Exception $e) { // we ignore if there was no data found for $unionSegmentName } } if (empty($values)) { throw new \Exception("There was no data to suggest for {$segmentName}"); } } else { $values = $this->getSuggestedValuesForSegmentName($idSite, $segment, $maxSuggestionsToReturn); } $values = $this->getMostFrequentValues($values); $values = array_slice($values, 0, $maxSuggestionsToReturn); $values = array_map(array('Piwik\\Common', 'unsanitizeInputValue'), $values); return $values; }
/** * Given a segment, will return a list of the most used values for this particular segment. * @param $segmentName * @param $idSite * @throws \Exception * @return array */ public function getSuggestedValuesForSegment($segmentName, $idSite) { if (empty(Config::getInstance()->General['enable_segment_suggested_values'])) { return array(); } Piwik::checkUserHasViewAccess($idSite); $maxSuggestionsToReturn = 30; $segmentsMetadata = $this->getSegmentsMetadata($idSite, $_hideImplementationData = false); $segmentFound = false; foreach ($segmentsMetadata as $segmentMetadata) { if ($segmentMetadata['segment'] == $segmentName) { $segmentFound = $segmentMetadata; break; } } if (empty($segmentFound)) { throw new \Exception("Requested segment not found."); } // if segment has suggested values callback then return result from it instead if (isset($segmentFound['suggestedValuesCallback'])) { return call_user_func($segmentFound['suggestedValuesCallback'], $idSite, $maxSuggestionsToReturn); } // if period=range is disabled, do not proceed if (!Period\Factory::isPeriodEnabledForAPI('range')) { return array(); } $startDate = Date::now()->subDay(60)->toString(); $requestLastVisits = "method=Live.getLastVisitsDetails\n &idSite={$idSite}\n &period=range\n &date={$startDate},today\n &format=original\n &serialize=0\n &flat=1"; // Select non empty fields only // Note: this optimization has only a very minor impact $requestLastVisits .= "&segment={$segmentName}" . urlencode('!='); // By default Live fetches all actions for all visitors, but we'd rather do this only when required if ($this->doesSegmentNeedActionsData($segmentName)) { $requestLastVisits .= "&filter_limit=400"; } else { $requestLastVisits .= "&doNotFetchActions=1"; $requestLastVisits .= "&filter_limit=800"; } $request = new Request($requestLastVisits); $table = $request->process(); if (empty($table)) { throw new \Exception("There was no data to suggest for {$segmentName}"); } // Cleanup data to return the top suggested (non empty) labels for this segment $values = $table->getColumn($segmentName); // Select also flattened keys (custom variables "page" scope, page URLs for one visit, page titles for one visit) $valuesBis = $table->getColumnsStartingWith($segmentName . ColumnDelete::APPEND_TO_COLUMN_NAME_TO_KEEP); $values = array_merge($values, $valuesBis); $values = $this->getMostFrequentValues($values); $values = array_slice($values, 0, $maxSuggestionsToReturn); $values = array_map(array('Piwik\\Common', 'unsanitizeInputValue'), $values); return $values; }