Tracking Events
The bundle also ships with basis to easily implement a tracker that tracks events to an Elasticsearch/Opensearch index.
Supports Elasticsearch versions 8. Supports Opensearch versions 2.
Implement Tracker
To implement a tracker, extend the AbstractElasticsearchTracker or AbstractOpensearchTracker and implement all abstract methods:
getIndexName: Define the name of index.buildMappingArray: Define mapping for index as array.trackEvent: Method for actual event tracking that is called by outside world. It takes a parameter array, prepares data for tracking (e.g. extracting information, adding timestamp, etc.) and builds structure expected by the mapping, and then callsdoTrackEvent(which does the data backend communication).
The abstract base class takes care of all the generic index creation, alias management and data backend communication.
A sample implementation can be found in DummyTracker.
Tracking Events
For each event type, a tracker needs to be implemented. The tracker implementations then need to be registered as services and then can be injected to any service or controller.
By calling $tracker->trackEvent($parameters) the events get tracked.
Index Structure
For performance and data housekeeping reasons, the tracker implements some kind of index rotating.
This means data is tracked to an index named <INDEX_NAME>__<YEAR>_<MONTH> which always corresponds to the
current month. If the index doesn't exist, it is created automatically.
In addition to creation of the indices, there is also an alias <INDEX_NAME> is created. This alias shows to
all existing rolled indices and can be used for querying the statistics.
The abstract base class for the tracker takes care of all necessary steps. There are only two things to consider:
- When implementing tracker is to extend from the abstract base class and implement the abstract methods.
- When setting up a data source for the explorer, the index alias needs to be used as index name.
Housekeeping itself needs to be done by the administrator of the data backend cluster - by closing/archiving deleting old indices that are not needed anymore for statistics and updating the index alias.
Reporting on Tracked Events with the Report Builder
To make the events of a tracker available as a dataset of the Report Builder, register the tracker's alias with a
service that implements EventDatasetProviderInterface. Autoconfigured services are tagged automatically, otherwise
tag them with pimcore.statistics_explorer.report_builder.event_dataset_provider.
use Pimcore\Bundle\StatisticsExplorerBundle\ReportBuilder\Event\EventDataset;
use Pimcore\Bundle\StatisticsExplorerBundle\ReportBuilder\Event\EventDatasetProviderInterface;
use Pimcore\Bundle\StatisticsExplorerBundle\ReportBuilder\Event\EventIdField;
final class DownloadDatasetProvider implements EventDatasetProviderInterface
{
public function getEventDatasets(): iterable
{
yield new EventDataset(
name: 'app_downloads',
label: 'Downloads',
index: 'app_statistics_download',
timeField: 'timestamp',
fieldLabels: ['asset.id' => 'Asset', 'userId' => 'User', 'channel' => 'Channel'],
idFields: [
'asset.id' => EventIdField::assets(), // assets(multiple: true) for lists of ids
'userId' => EventIdField::users(),
'channel' => EventIdField::labels(['web' => 'Website', 'app' => 'Mobile app']),
],
hiddenFields: ['asset.path'],
filter: [['exists' => ['field' => 'asset.id']]],
);
}
}
nameis the dataset name in report configurations ({"type": "events", "name": "app_downloads"}).indexis the alias of the tracker, the name itsgetIndexName()returns, which covers all monthly indices. The index must be in the search cluster of the Generic Data Index.- The fields come from the index mapping: keywords (and texts with a
keywordsub-field), numbers, dates and booleans. Analyzed texts without keyword, fields in nested documents and paths that report configurations cannot name (for example@timestamp) are not offered. Fields without a label show their path. An index without events has no mapping yet: its time field and id fields are offered from the registration. timeFieldis the date field of the event time (a plain field name, not@timestamp). List reports show the newest events first unless they define a sort.idFieldsdefine how ids are shown. They must be indexed as numbers or keywords:EventIdField::dataObjects('PortalUser'),EventIdField::assets()andEventIdField::documents(): related elements, shown by their key. Their fields are offered as lookups, for exampleasset.id.mimetype(assets offer the same lookups as relations of data objects). Elements the viewer may not see are shown as restricted. Passmultiple: truefor fields that hold a list of ids, so lists show all of them. If the class ofdataObjects('PortalUser')does not exist, the ids are shown as they are.EventIdField::users(): Pimcore users, shown by their name.EventIdField::labels([...]): fixed labels by id or code, for example portal names.
hiddenFieldslists fields that are not offered (with everything below an object field). All other fields of the index are offered, also those that the tracker adds later. Hide fields that show what the id field of the same event restricts, for example the path of an asset.indexmust not be an alias of the Generic Data Index (for examplepimcore_asset): its elements would be shown without workspace checks.filteris an optional list of query clauses that every report on the dataset applies.permissionis an optional user permission, for exampleusers, that every access to the dataset needs.
Reports on event datasets are protected by the permissions of the report and the permission of the registration.
The events and their plain fields are not filtered by workspaces; only related elements of id fields are shown with
the workspace permissions of the viewer.
Index Settings
New indices are created with number_of_replicas set to 0. Without it, the data backend falls back to its own
default of one replica, which can never be allocated on a single node cluster and leaves every monthly index
permanently yellow. On a multi node cluster, configure the number of replicas you want:
pimcore_statistics_explorer:
tracker:
number_of_replicas: 1
The option is applied to trackers registered as services (autoconfiguration) and only to indices that are created
afterwards - existing monthly indices keep their setting and have to be updated on the cluster, e.g.
PUT <INDEX_NAME>__<YEAR>_<MONTH>/_settings with {"index": {"number_of_replicas": 0}}.
A tracker that is not registered as a service (created with new) can call setNumberOfReplicas() itself.
A tracker that overrides createIndexSettings() defines the settings of its indices on its own. To keep the
configured number of replicas, build on the parent settings:
array_replace(parent::createIndexSettings(), $ownSettings).