Skip to main content

Stream every authenticated 302-proxy download event to an external webhook

Premium Beginner

Benecaster records every authenticated episode download. Those records are what the analytics screens are built from, but they are only read on a schedule — so if you want a download to reach something else the moment it happens, the log is the wrong place to wait.

benecaster_download_logged fires the instant a download has been recorded, and carries the same details the record holds. Send it to a webhook, a Slack channel, or a real-time pipeline without going near the download path itself — which matters here more than usual, because that path is what serves audio to subscribers, and code added to it is code that can break playback.

This reacts to the record after it exists. To put your own data into the record, see Tag download log rows with extra data.

Code

<?php
add_action(
    'benecaster_download_logged',
    function ( int $episode_id, int $user_id, array $log_entry ): void {
        wp_remote_post(
            'https://example.com/webhooks/benecaster-downloads',
            [
                'timeout'  => 5,
                'blocking' => false,
                'body'     => wp_json_encode( [
                    'episode_id'   => $episode_id,
                    'user_id'      => $user_id,
                    'tier_slug'    => $log_entry['tier_slug'] ?? null,
                    'requested_at' => gmdate( 'c' ),
                ] ),
            ]
        );
    },
    10,
    3
);

View on GitHub →

Hooks Used

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