private function getCustomDimensionsInScope($scope, Request $request)
 {
     $dimensions = self::getCachedCustomDimensions($request);
     $params = $request->getParams();
     $values = array();
     foreach ($dimensions as $dimension) {
         if ($dimension['scope'] !== $scope) {
             continue;
         }
         $field = self::buildCustomDimensionTrackingApiName($dimension);
         $dbField = Dao\LogTable::buildCustomDimensionColumnName($dimension);
         $value = Common::getRequestVar($field, '', 'string', $params);
         if ($value !== '') {
             $values[$dbField] = $value;
             continue;
         }
         $extractions = $dimension['extractions'];
         if (is_array($extractions)) {
             foreach ($extractions as $extraction) {
                 if (!array_key_exists('dimension', $extraction) || !array_key_exists('pattern', $extraction) || empty($extraction['pattern'])) {
                     continue;
                 }
                 $extraction = new Extraction($extraction['dimension'], $extraction['pattern']);
                 $extraction->setCaseSensitive($dimension['case_sensitive']);
                 $value = $extraction->extract($request);
                 if (!isset($value) || '' === $value) {
                     continue;
                 }
                 $values[$dbField] = $value;
                 break;
             }
         }
     }
     return $values;
 }
 protected function configureSegments()
 {
     $idSite = Common::getRequestVar('idSite', 0, 'int');
     if (empty($idSite)) {
         return array();
     }
     $configuration = StaticContainer::get('Piwik\\Plugins\\CustomDimensions\\Dao\\Configuration');
     $dimensions = $configuration->getCustomDimensionsForSite($idSite);
     foreach ($dimensions as $dimension) {
         if (!$dimension['active']) {
             continue;
         }
         $segment = new Segment();
         $segment->setSegment(CustomDimensionsRequestProcessor::buildCustomDimensionTrackingApiName($dimension));
         $segment->setType(Segment::TYPE_DIMENSION);
         $segment->setName($dimension['name']);
         $columnName = LogTable::buildCustomDimensionColumnName($dimension);
         if ($dimension['scope'] === CustomDimensions::SCOPE_ACTION) {
             $segment->setSqlSegment('log_link_visit_action. ' . $columnName);
             $segment->setCategory('General_Actions');
             $segment->setSuggestedValuesCallback(function ($idSite, $maxValuesToReturn) use($dimension) {
                 $autoSuggest = new AutoSuggest();
                 return $autoSuggest->getMostUsedActionDimensionValues($dimension, $idSite, $maxValuesToReturn);
             });
         } elseif ($dimension['scope'] === CustomDimensions::SCOPE_VISIT) {
             $segment->setSqlSegment('log_visit. ' . $columnName);
             $segment->setCategory('General_Visit');
         } else {
             continue;
         }
         $this->addSegment($segment);
     }
 }
 /**
  * @param array $dimension
  * @param int $idSite
  * @param int $maxValuesToReturn
  * @return array
  */
 public function getMostUsedActionDimensionValues($dimension, $idSite, $maxValuesToReturn)
 {
     $maxValuesToReturn = (int) $maxValuesToReturn;
     $idSite = (int) $idSite;
     $startDate = Date::now()->subDay(60)->toString();
     $name = LogTable::buildCustomDimensionColumnName($dimension);
     $table = Common::prefixTable('log_link_visit_action');
     $query = "SELECT {$name}, count({$name}) as countName FROM {$table}\n                  WHERE idsite = ? and server_time > {$startDate} and {$name} is not null\n                  GROUP by {$name}\n                  ORDER BY countName DESC LIMIT {$maxValuesToReturn}";
     $rows = Db::get()->fetchAll($query, array($idSite));
     $values = array();
     foreach ($rows as $row) {
         $values[] = $row[$name];
     }
     return $values;
 }
 public function getCustomDimensionValues($configuredVisitDimensions)
 {
     $values = array();
     foreach ($configuredVisitDimensions as $dimension) {
         if ($dimension['active'] && $dimension['scope'] === CustomDimensions::SCOPE_VISIT) {
             // field in DB, eg custom_dimension_1
             $field = LogTable::buildCustomDimensionColumnName($dimension);
             // field for user, eg dimension1
             $column = CustomDimensionsRequestProcessor::buildCustomDimensionTrackingApiName($dimension);
             if (array_key_exists($field, $this->details)) {
                 $values[$column] = $this->details[$field];
             } else {
                 $values[$column] = null;
             }
         }
     }
     return $values;
 }
 public function aggregateDayReport()
 {
     $dimensions = $this->getActiveCustomDimensions();
     foreach ($dimensions as $dimension) {
         $this->dataArray = new DataArray();
         $valueField = LogTable::buildCustomDimensionColumnName($dimension);
         $dimensions = array($valueField);
         if ($dimension['scope'] === CustomDimensions::SCOPE_VISIT) {
             $this->aggregateFromVisits($valueField, $dimensions, " log_visit.{$valueField} is not null");
             $this->aggregateFromConversions($valueField, $dimensions, " log_conversion.{$valueField} is not null");
         } elseif ($dimension['scope'] === CustomDimensions::SCOPE_ACTION) {
             $this->aggregateFromActions($valueField);
         }
         $this->dataArray->enrichMetricsWithConversions();
         $table = $this->dataArray->asDataTable();
         $blob = $table->getSerialized($this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $columnToSort = Metrics::INDEX_NB_VISITS);
         $recordName = self::buildRecordNameForCustomDimensionId($dimension['idcustomdimension']);
         $this->getProcessor()->insertBlobRecord($recordName, $blob);
     }
 }
 public function addVisitFieldsToPersist(&$fields)
 {
     if (!$this->isInstalled()) {
         return;
     }
     $indexes = $this->getCachedInstalledIndexesForScope(self::SCOPE_VISIT);
     $fields[] = 'last_idlink_va';
     foreach ($indexes as $index) {
         $fields[] = LogTable::buildCustomDimensionColumnName($index);
     }
 }
 public function test_buildCustomDimensionColumnName()
 {
     $this->assertNull(LogTable::buildCustomDimensionColumnName('0'));
     $this->assertNull(LogTable::buildCustomDimensionColumnName(''));
     $this->assertNull(LogTable::buildCustomDimensionColumnName(null));
     $this->assertNull(LogTable::buildCustomDimensionColumnName(array()));
     $this->assertNull(LogTable::buildCustomDimensionColumnName(array('index' => '')));
     $this->assertSame('custom_dimension_1', LogTable::buildCustomDimensionColumnName('1'));
     $this->assertSame('custom_dimension_1', LogTable::buildCustomDimensionColumnName('1'));
     $this->assertSame('custom_dimension_99', LogTable::buildCustomDimensionColumnName('99'));
     $this->assertSame('custom_dimension_94', LogTable::buildCustomDimensionColumnName('94te'));
     $this->assertSame('custom_dimension_95', LogTable::buildCustomDimensionColumnName(array('index' => '95')));
 }