Skip to content

Declaring Metrics

padams edited this page Dec 2, 2026 · 1 revision

A metric is a number a report shows: page views, sessions, revenue, engagement rate. Every metric is computed from a Property's reporting cube, one row per event. A metric is declared with a name, a column of the cube and a kind of aggregate. It has no SQL and runs no query of its own.

Every metric OWA ships, and how each is computed, is on Metrics and Dimensions. The cube's columns are on Data Model.

Where metrics are declared

A module declares its metrics in config/metrics.php, which returns the entity they are computed from and the metrics, by name:

<?php

return [

    'entity' => 'base.event',

    'metrics' => [

        'downloads' => [
            'label'       => 'Downloads',
            'description' => 'The number of files downloaded.',
            'group'       => 'Site Usage',
            'metric_type' => 'count',
            'data_type'   => 'integer',
            'column'      => 'id',
            'condition'   => [ 'column' => 'event_type', 'value' => 'file_download' ],
        ],

        'downloadsPerSession' => [
            'label'       => 'Downloads Per Session',
            'description' => 'The average number of files downloaded per session.',
            'group'       => 'Site Usage',
            'metric_type' => 'ratio',
            'data_type'   => 'decimal',
            'numerator'   => 'downloads',
            'denominator' => 'sessions',
            'precision'   => 2,
        ],
    ],
];

The file is read when the module is constructed. A file that does not return an entity and a metrics array is ignored, with the notice <file> must return an array with an entity and a metrics array; ignoring it.

entity must be base.event, the cube. A metric declared on any other entity is ignored, with the notice The metric "<name>" is registered on <entity> and is ignored: metrics are computed from base.event.

A metric can also be declared from code with $this->registerMetricDefinition( $definition ), the definition carrying name and entity as keys.

Either way, a definition is refused at registration, with a notice, when:

It lacks Notice
name or metric_type A metric definition is ignored: it names no name. (or metric_type)
entity or column, on any kind but calculated, ratio and difference The metric "<name>" is ignored: a <metric_type> metric names the entity and the column it reads.

A definition with no label is labelled with its name.

The metric's name is what a report definition, a custom report and the REST API ask for. A name declared twice is computed from the last declaration.

The keys

Key
label What a report's column is headed. Translated
description One sentence for the metric pickers and the REST metadata. Translated
group The heading the metric is listed under: OWA uses Site Usage, Goals and Ecommerce
metric_type The kind: see below
data_type How the value is formatted: integer, decimal, percentage, currency, milliseconds, string, url, yyyymmdd or timestamp. Any other value is ignored with a notice, and the metric is shown unformatted
column The cube column aggregated
condition Restricts the rows aggregated. See below
numerator, denominator, precision For a ratio
minuend, subtrahend For a difference
subtrahend_column For avg_difference

Any other key is dropped.

Kinds

metric_type Computes Example
count The rows, by counting column eventCount: every event
distinct_count The distinct values of column sessions: distinct session_id
sum The sum of column. A row holding NULL adds 0, and no rows gives 0 totalEngagementTime: engagement_msec
boolean_true_count The rows where column is true. NULL counts as false No OWA metric uses it
avg_difference The average of column less subtrahend_column, rounded No OWA metric uses it
ratio One metric divided by another, rounded to precision decimal places. A zero or missing denominator gives 0 engagementRate: engagedSessions ÷ sessions
difference One metric less another. A missing side counts as 0 netRevenue: transactionRevenue − refundAmount

A ratio or difference names other metrics, not columns. Its two sides are computed by the same query, and the division or subtraction is done afterwards.

Conditions

A condition makes a count, distinct_count or sum read only the rows that pass one test:

'condition' => [ 'column' => 'is_engaged_session', 'value' => 1 ],
Key
column A cube column
value A number, a boolean, or a string of letters, digits, _, ., - and spaces
operator = (the default), !=, <>, >, <, >= or <=

A value that cannot be written as a SQL literal leaves the metric out of the query, and the error log says Metric "<name>" has a condition value (<value>) that cannot be a SQL literal. Its column will be missing from the query. Any other operator is replaced by =, with an error naming it.

Most of OWA's metrics are a condition on event_type or on a flag the cube build sets on each row: pageViews counts rows where event_type is page_view, entrances rows where is_entrance is 1, newUsers distinct visitor_id where prior_sessions is 0.

Session and user metrics

A session or user metric is a distinct_count of session_id or visitor_id, with a condition on a session or visitor value. Such a value is on every row of the session, or of the visitor. engagedSessions is the distinct sessions where is_engaged_session is 1. Grouped by any dimension, it counts the sessions with at least one row in each group.

What a metric can count

Only columns of the cube: raw's columns, and those a cube build derives. A value the tracker sends that is not a column is in params and cannot be counted until it is registered as a custom dimension, which gives each Property's cube its own column. A new column for every cube is added by a Base schema update.

Changes from 1.x

1.x 2.0
registerMetricDefinition() calls in registerMetrics() config/metrics.php. registerMetrics() is gone; registerMetricDefinition() still works
A metric declared against a fact table (base.request, base.session, …), several times for several tables One declaration, against base.event
Metric classes in a module's Metric/ directory, registered with registerMetric(), for metrics that ran their own query Removed. Every metric is a declaration
calculated metrics with a formula ratio and difference. A formula with child_metrics is refused with a notice
1.x metric names (visits, uniqueVisitors, actions, …) Renamed. A saved custom report is rewritten during the upgrade

Clone this wiki locally