Piwik\Plugin\Metric::getMetric PHP Méthode

getMetric() public static méthode

Helper method that will access a metric in a {@link Piwik\DataTable\Row} or array either by its name or by its special numerical index value.
public static getMetric ( Row | array $row, string $columnName, int[] | null $mappingNameToId = null ) : mixed
$row Piwik\DataTable\Row | array
$columnName string
$mappingNameToId int[] | null A custom mapping of metric names to special index values. By default {@link Metrics::getMappingFromNameToId()} is used.
Résultat mixed The metric value or false if none exists.
    public static function getMetric($row, $columnName, $mappingNameToId = null)
    {
        if ($row instanceof Row) {
            $value = $row->getColumn($columnName);
            if ($value === false) {
                if (empty($mappingNameToId)) {
                    $mappingNameToId = Metrics::getMappingFromNameToId();
                }
                if (isset($mappingNameToId[$columnName])) {
                    return $row->getColumn($mappingNameToId[$columnName]);
                }
            }
            return $value;
        } elseif (!empty($row)) {
            if (array_key_exists($columnName, $row)) {
                return $row[$columnName];
            } else {
                if (empty($mappingNameToId)) {
                    $mappingNameToId = Metrics::getMappingFromNameToId();
                }
                if (isset($mappingNameToId[$columnName])) {
                    $columnName = $mappingNameToId[$columnName];
                    if (array_key_exists($columnName, $row)) {
                        return $row[$columnName];
                    }
                }
            }
        }
        return null;
    }

Usage Example

 private function deleteRowsWithNoVisit(DataTable $table)
 {
     foreach ($table->getRows() as $key => $row) {
         $nbVisits = Metric::getMetric($row, 'nb_visits');
         $nbActions = Metric::getMetric($row, 'nb_actions');
         if ($nbVisits == 0 && $nbActions == 0) {
             // case of keyword/website/campaign with a conversion for this day, but no visit, we don't show it
             $table->deleteRow($key);
         }
     }
 }
All Usage Examples Of Piwik\Plugin\Metric::getMetric