1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-21 22:47:43 +00:00
Files
matomo/plugins/ExamplePlugin/RecordBuilders/ExampleMetric2.php
Stefan Giehl 75290a04f4 Use https URLs (#23072)
* Use https URLs

* Build vue files

* use matomo.org instead of piwik.org in some links

* updates expected UI test file

---------

Co-authored-by: innocraft-automation <innocraft-automation@users.noreply.github.com>
2025-02-27 16:34:08 +01:00

85 خطوط
2.9 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\ExamplePlugin\RecordBuilders;
use Piwik\ArchiveProcessor;
use Piwik\ArchiveProcessor\Record;
use Piwik\ArchiveProcessor\RecordBuilder;
use Piwik\Option;
/**
* The RecordBuilder class processes raw data into ready ro read reports.
* It must implement two methods, one for aggregating daily reports
* aggregate() and another returning information about the record.
*
* Plugins can have more than one RecordBuilder, and should try to divide them
* up into the smallest number they can, while still performing as few total log aggregation
* queries as possible (this results in improved performance overall).
*
* For more detailed information about RecordBuilders please visit Matomo developer guide
* https://developer.matomo.org/api-reference/Piwik/ArchiveProcessor/RecordBuilder
*/
class ExampleMetric2 extends RecordBuilder
{
public const EXAMPLEPLUGIN_CONST_METRIC_NAME = 'ExamplePlugin_example_metric2';
/**
* This method should return the list of records this RecordBuilder creates. This example
* archives one metric, so we return some information about them.
*/
public function getRecordMetadata(ArchiveProcessor $archiveProcessor): array
{
return [
Record::make(Record::TYPE_NUMERIC, self::EXAMPLEPLUGIN_CONST_METRIC_NAME),
];
}
/**
* inside this method you can implement your LogAggregator usage
* to process daily reports. this code for example, uses idvisitor to group results:
*
* ```
* $record = new DataTable();
*
* $query = $archiveProcessor->getLogAggregator()->queryVisitsByDimension(['idvisitor']);
* while ($row = $query->fetch()) {
* $label = $row['idvisitor'];
* unset($row['idvisitor']);
* $record->sumRowWithLabel($label, $row);
* }
*
* return [self::EXAMPLEPLUGIN_ARCHIVE_RECORD => $record];
* ```
*
* non-day periods will automatically be aggregated together
*/
protected function aggregate(ArchiveProcessor $archiveProcessor): array
{
$records = [];
$callCount = $this->getAndIncrementArchiveCallCount($archiveProcessor);
$metricValue = $callCount > 0 ? 1 : 0;
$records[self::EXAMPLEPLUGIN_CONST_METRIC_NAME] = $metricValue;
return $records;
}
private function getAndIncrementArchiveCallCount(ArchiveProcessor $archiveProcessor)
{
$params = $archiveProcessor->getParams();
$optionName = 'ExamplePlugin.metricValue.' . md5($params->getSite()->getId() . '.' . $params->getPeriod()->getRangeString()
. '.' . $params->getPeriod()->getLabel() . '.' . $params->getSegment()->getHash());
$value = (int) Option::get($optionName);
Option::set($optionName, $value + 1);
return $value;
}
}