Skip to main content

Name and order a show’s tiers the way the Analytics Dashboard does

Premium Beginner

The Analytics Dashboard’s panel routes return tier slugs only. GET benecaster-analytics/v1/analytics/tiers returns a show’s tiers with their names, highest first: { show_id, tiers: [ { tier_slug, name, rank } ] }, rank 0 being the highest. It reads benecaster_get_show_tiers() (lowest first) and reverses it, so its order is Benecaster’s tier ranking.

The route takes show_id and an optional scope (show, the default, or all; see Read every Analytics Dashboard route for All my shows). Like every Analytics Dashboard route it needs manage_options and a wp_rest nonce in X-WP-Nonce, and answers 403 benecaster_analytics_not_active for a show where the add-on is not switched on. On Benecaster’s admin page the add-on publishes its own REST base as window.benecasterAnalyticsDashboard.apiUrl, which works under every permalink setting.

  • A slug in a panel route’s answer may be missing here: a tier deleted or renamed since keeps its history. Fall back to the slug rather than dropping the row.
  • Server-side, call benecaster_get_show_tiers() directly; this route exists for the admin screen.

Code

const base = window.benecasterAnalyticsDashboard.apiUrl; // the add-on's own REST base, on Benecaster's admin page
const headers = { 'X-WP-Nonce': window.benecasterAdmin.nonce };
const { tiers } = await ( await fetch( base + 'analytics/tiers?show_id=12', { headers } ) ).json();
const names = Object.fromEntries( tiers.map( ( t ) => [ t.tier_slug, t.name ] ) );

View on GitHub →

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