Skip to main content

Read the Analytics Dashboard feed engagement buckets

Premium Intermediate

The Analytics Dashboard add-on’s REST namespace is benecaster-analytics/v1, separate from Benecaster’s own benecaster/v1 analytics routes. Its routes need manage_options and a wp_rest nonce in X-WP-Nonce, and answer 403 benecaster_analytics_not_active for a show where the add-on is not switched on.

GET /analytics/feed-engagement reports how recently each tier’s active subscribers last polled their feed, as of now. It takes show_id, an optional tier_slug and an optional scope (show, the default, or all; see Read every Analytics Dashboard route for All my shows), and no date range: a date_from or date_to is ignored. The bands are each show’s own feed inactivity threshold (Show Settings → Subscription, read with benecaster_get_engagement_threshold()) and twice it, exactly as the Subscribers screen draws them, so the two screens agree. On Benecaster’s admin page the add-on publishes its own REST base as window.benecasterAnalyticsDashboard.apiUrl, which works under every permalink setting:

const url = window.benecasterAnalyticsDashboard.apiUrl + 'analytics/feed-engagement?show_id=12';
const engagement = await ( await fetch( url, { headers: { 'X-WP-Nonce': window.benecasterAdmin.nonce } } ) ).json();

To change the counts for every caller, filter them, as below.

  • The keys are not the screen’s labels: at_risk is the band labelled Low (amber), and lapsed the band labelled At risk (red).
  • awaiting counts tokens not yet polled but still inside the never-set-up grace period. The Subscribers bar leaves them out, so total is the sum of all five buckets.
  • The Analytics Dashboard’s own Feed engagement by tier panel draws the keys under the Subscribers screen’s words: active Active, at_risk Low, lapsed At risk, never Never set up, and awaiting as a fifth band, Not checked in yet. Label your own display the same way, so a band means one thing on every screen.

Code

<?php
// A weekly-active percentage for one tier, the way the weekly digest reads it.
add_filter( 'benecaster_analytics_feed_engagement', function ( array $data, array $args ): array {
    foreach ( $data['tiers'] as &$tier ) {
        $tier['active_7d_pct'] = $tier['total'] > 0 ? round( $tier['active_7d'] * 100 / $tier['total'], 1 ) : null;
    }
    return $data;
}, 10, 2 );

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →