Panel Analytics Hooks
The six hooks over report figures, date ranges, charts and live counters.
Overview
Every number on the report pages passes through these hooks: summary figures, time series, distributions and finance totals. Beside them sits the read-only event of the live counters.
One rule runs through all of them: keep the shape. The chart library and the queries that follow expect particular keys; a missing or mismatched structure quietly yields an empty report.
Reference
Following a live metric poll
Runs when the live counters on the board refresh. It repeats at regular intervals while the page is open.
Hook::add('action:admin.analytics.realtime_polled', 10, function ($stats) {
// A copy is passed: changing it has no effect.
Acme::pushMetric('online', (int) ($stats['online_count'] ?? 0));
});Changing the report date range
Runs after the date range picked by the user is resolved. Every report query uses that range.
Hook::add('filter:admin.analytics.date_range', 10, function (&$result, $range) {
// Every key must stay: a missing one empties the report.
$result['start'] = Acme::fiscalStart($result['start']);
});Changing the summary figures
Runs once the summary figures at the top of a report page are worked out.
Hook::add('filter:admin.analytics.kpis', 10, function (&$kpis, $section, $range) {
// The shape follows the section: check it first.
if ($section !== 'clients') return;
$kpis['acme_churn'] = Acme::churnRate($range);
});Changing the time series
Runs once the chart data is worked out.
Hook::add('filter:admin.analytics.trend_series', 10, function (&$trend, $report, $range) {
// Labels and series must be the same length.
if ($report !== 'income') return;
$trend['acme_forecast'] = Acme::forecast(count($trend['labels'] ?? []));
});Changing the distribution data
Runs once the distribution chart data is worked out.
Hook::add('filter:admin.analytics.distribution', 10,
function (&$data, $entity, $type, $range) {
// Every item must carry a label and a value.
if ($entity === 'client' && $type === 'country')
$data = Acme::mergeSmallCountries($data);
});Changing the finance summary
Runs once the summary totals of a finance report are worked out.
Hook::add('filter:admin.analytics.invoice_report_totals', 10,
function (&$totals, $report, $range, $currency_id) {
// A zero currency means the base currency.
if ($report === 'income') $totals['acme_target'] = Acme::monthlyTarget($currency_id);
});Pitfalls
Every key in the range array is read by the queries that follow. Removing one or renaming it breaks the figure and chart queries; what appears on screen is not an error but an empty report, which makes it hard to spot.
If the label count and the value count part ways in a time series the chart axes slip. Build a new series to the length of the existing labels, and keep the series keys already there.
Related Articles
Vielen Dank für Ihre Rückmeldung!
Unser Support-Team ist rund um die Uhr für Sie da, wenn Sie oben nicht fündig werden.