You can use the methods in this class within {@link Piwik\Plugin\Archiver Archiver} descendants
to aggregate log data without having to write SQL queries.
### Aggregation Dimension
All aggregation methods accept a **dimension** parameter. These parameters are important as
they control how rows in a table are aggregated together.
A **_dimension_** is just a table column. Rows that have the same values for these columns are
aggregated together. The result of these aggregations is a set of metrics for every recorded value
of a **dimension**.
_Note: A dimension is essentially the same as a **GROUP BY** field._
### Examples
**Aggregating visit data**
$archiveProcessor = // ...
$logAggregator = $archiveProcessor->getLogAggregator();
get metrics for every used browser language of all visits by returning visitors
$query = $logAggregator->queryVisitsByDimension(
$dimensions = array('log_visit.location_browser_lang'),
$where = 'log_visit.visitor_returning = 1',
also count visits for each browser language that are not located in the US
$additionalSelects = array('sum(case when log_visit.location_country <> 'us' then 1 else 0 end) as nonus'),
we're only interested in visits, unique visitors & actions, so don't waste time calculating anything else
$metrics = array(Metrics::INDEX_NB_UNIQ_VISITORS, Metrics::INDEX_NB_VISITS, Metrics::INDEX_NB_ACTIONS),
);
if ($query === false) {
return;
}
while ($row = $query->fetch()) {
$uniqueVisitors = $row[Metrics::INDEX_NB_UNIQ_VISITORS];
$visits = $row[Metrics::INDEX_NB_VISITS];
$actions = $row[Metrics::INDEX_NB_ACTIONS];
... do something w/ calculated metrics ...
}
**Aggregating conversion data**
$archiveProcessor = // ...
$logAggregator = $archiveProcessor->getLogAggregator();
get metrics for ecommerce conversions for each country
$query = $logAggregator->queryConversionsByDimension(
$dimensions = array('log_conversion.location_country'),
$where = 'log_conversion.idgoal = 0', // 0 is the special ecommerceOrder idGoal value in the table
also calculate average tax and max shipping per country
$additionalSelects = array(
'AVG(log_conversion.revenue_tax) as avg_tax',
'MAX(log_conversion.revenue_shipping) as max_shipping'
)
);
if ($query === false) {
return;
}
while ($row = $query->fetch()) {
$country = $row['location_country'];
$numEcommerceSales = $row[Metrics::INDEX_GOAL_NB_CONVERSIONS];
$numVisitsWithEcommerceSales = $row[Metrics::INDEX_GOAL_NB_VISITS_CONVERTED];
$avgTaxForCountry = $row['avg_tax'];
$maxShippingForCountry = $row['max_shipping'];
... do something with aggregated data ...
}