Skip to main content

Chart Listener Support donations beside membership revenue

Free Intermediate

Monthly Listener Support donations for a show, per currency, in the same unit as Chart built-in membership revenue from what was actually charged, so donations and membership revenue can share one chart.

It reads benecaster_get_listener_support_donations() for the last twelve months. Read donations through that function rather than the donations table: the table stores major units, and every other money figure Benecaster hands you is in minor units.

Code

<?php
function my_monthly_donations( int $show_id ): array {
    $rows   = benecaster_get_listener_support_donations( $show_id, [
        'date_from' => gmdate( 'Y-m-01', strtotime( '-11 months' ) ),
    ] );
    $totals = [];
    $counts = [];
    foreach ( $rows as $row ) {
        $month = substr( $row['donated_at'], 0, 7 );   // UTC month
        $counts[ $month ] = ( $counts[ $month ] ?? 0 ) + 1;
        if ( null === $row['amount_donated'] ) {
            continue;                                   // a donation with no amount: counted, never summed as zero
        }
        $currency = $row['currency'];                   // never sum across currencies
        $totals[ $currency ][ $month ] = ( $totals[ $currency ][ $month ] ?? 0 ) + $row['amount_donated']; // minor units
    }
    return [ 'by_currency' => $totals, 'donations_per_month' => $counts ];
}

View on GitHub →

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