<?php
$page_title = 'Step 8 — Account stats';
require 'header.php';
?>

<p>
    Two useful "how am I doing" endpoints:
    <span class="endpoint">/users/info</span> gives you the account-level
    reputation and lifetime counters, and
    <span class="endpoint">/tags/all-time-series</span> gives hourly stats
    for the last 30 days across all tags. Together they cover "am I healthy?"
    and "what's been happening recently?"
</p>

<h2>Fetch both</h2>

<form method="post">
    <button type="submit" name="run">Fetch account info &amp; recent activity</button>
</form>

<?php
if (isset($_POST['run']) && is_configured()) {

    // --- Call 1: user info -----------------------------------
    $info_result = mandrill_call('/users/info');

    // --- Call 2: 30-day time series across all tags ----------
    $stats_result = mandrill_call('/tags/all-time-series');
    // ---------------------------------------------------------

    if (!mandrill_is_error($info_result) && is_array($info_result['decoded'])) {
        $info = $info_result['decoded'];

        echo '<h3>Account</h3>';
        echo '<table>';
        echo '<tr><th>Username</th>        <td>' . h($info['username']   ?? '') . '</td></tr>';
        echo '<tr><th>Public ID</th>       <td><code>' . h($info['public_id'] ?? '') . '</code></td></tr>';
        echo '<tr><th>Account created</th> <td>' . h($info['created_at'] ?? '') . '</td></tr>';
        echo '<tr><th>Reputation</th>      <td>' . h((string)($info['reputation'] ?? '')) . ' / 100</td></tr>';
        echo '<tr><th>Hourly quota</th>    <td>' . h((string)($info['hourly_quota'] ?? '')) . '</td></tr>';
        echo '<tr><th>Backlog</th>         <td>' . h((string)($info['backlog'] ?? '')) . '</td></tr>';
        echo '</table>';

        if (isset($info['stats']) && is_array($info['stats'])) {
            echo '<h3>Lifetime counters</h3>';
            echo '<table><tr><th>Window</th><th>Sent</th><th>Hard bounces</th><th>Soft bounces</th><th>Rejects</th><th>Complaints</th><th>Unsubs</th><th>Opens</th><th>Clicks</th></tr>';
            foreach ($info['stats'] as $window => $s) {
                echo '<tr>';
                echo '<td><strong>' . h($window) . '</strong></td>';
                echo '<td>' . h((string)($s['sent'] ?? 0))            . '</td>';
                echo '<td>' . h((string)($s['hard_bounces'] ?? 0))    . '</td>';
                echo '<td>' . h((string)($s['soft_bounces'] ?? 0))    . '</td>';
                echo '<td>' . h((string)($s['rejects'] ?? 0))         . '</td>';
                echo '<td>' . h((string)($s['complaints'] ?? 0))      . '</td>';
                echo '<td>' . h((string)($s['unsubs'] ?? 0))          . '</td>';
                echo '<td>' . h((string)($s['unique_opens'] ?? 0))    . '</td>';
                echo '<td>' . h((string)($s['unique_clicks'] ?? 0))   . '</td>';
                echo '</tr>';
            }
            echo '</table>';
            echo '<p class="muted">Windows: <code>today</code>, <code>last_7_days</code>, <code>last_30_days</code>, <code>last_60_days</code>, <code>last_90_days</code>, <code>all_time</code>.</p>';
        }
    }

    if (!mandrill_is_error($stats_result) && is_array($stats_result['decoded'])) {
        $series = $stats_result['decoded'];

        if (count($series) === 0) {
            echo '<h3>Recent activity (last 30 days)</h3>';
            echo '<div class="callout info">No activity in the last 30 days.</div>';
        } else {
            // Show the most recent 20 hours with activity
            echo '<h3>Recent activity (most recent first)</h3>';
            $series = array_reverse($series);
            $series = array_slice($series, 0, 20);
            echo '<table><tr><th>Hour</th><th>Sent</th><th>Bounces</th><th>Rejects</th><th>Opens</th><th>Clicks</th></tr>';
            foreach ($series as $s) {
                echo '<tr>';
                echo '<td>' . h($s['time'] ?? '') . '</td>';
                echo '<td>' . h((string)($s['sent'] ?? 0))         . '</td>';
                echo '<td>' . h((string)(($s['hard_bounces'] ?? 0) + ($s['soft_bounces'] ?? 0))) . '</td>';
                echo '<td>' . h((string)($s['rejects'] ?? 0))      . '</td>';
                echo '<td>' . h((string)($s['unique_opens'] ?? 0)) . '</td>';
                echo '<td>' . h((string)($s['unique_clicks'] ?? 0)) . '</td>';
                echo '</tr>';
            }
            echo '</table>';
        }
    }

    echo '<h3>Raw responses</h3>';
    render_debug($info_result);
    render_debug($stats_result);
}
?>

<h2>The calls</h2>

<pre><code>// Account-level info
$info = mandrill_call('/users/info');
echo $info['decoded']['reputation'];          // 0-100
echo $info['decoded']['stats']['last_30_days']['sent'];

// Hourly stats, last 30 days, aggregated across all tags
$series = mandrill_call('/tags/all-time-series');
foreach ($series['decoded'] as $hour) {
    echo $hour['time'] . ': sent=' . $hour['sent'];
}</code></pre>

<h2>What the numbers mean</h2>

<table>
<tr><th>Field</th><th>Means</th></tr>
<tr><td><code>reputation</code></td><td>Mandrill's 0&ndash;100 score for your account. Below ~25 and you'll hit throttling; over ~80 is healthy.</td></tr>
<tr><td><code>hourly_quota</code></td><td>The maximum number of messages you can send per hour right now. Grows as your reputation grows.</td></tr>
<tr><td><code>backlog</code></td><td>How many messages are queued but not yet sent. Should usually be 0.</td></tr>
<tr><td><code>hard_bounces</code></td><td>Permanent delivery failures (bad address). These land the recipient on your denylist.</td></tr>
<tr><td><code>soft_bounces</code></td><td>Temporary failures (mailbox full, out of office). Retried automatically.</td></tr>
<tr><td><code>rejects</code></td><td>Messages Mandrill refused to send (unsigned domain, denylisted recipient, etc.)</td></tr>
<tr><td><code>complaints</code></td><td>Recipients who marked you as spam. The one to watch — keep well below 0.1% of sent.</td></tr>
</table>

<h2>Other useful stats endpoints</h2>

<table>
<tr><th>Endpoint</th><th>What it gives you</th></tr>
<tr><td><code>/tags/list</code></td><td>All tags with lifetime counters.</td></tr>
<tr><td><code>/tags/time-series</code></td><td>Hourly 30-day stats for <em>one</em> tag.</td></tr>
<tr><td><code>/senders/list</code></td><td>Per-sender (from_email) stats &mdash; useful if you send from multiple addresses.</td></tr>
<tr><td><code>/messages/search</code></td><td>Query individual sent messages by date / tag / sender / recipient.</td></tr>
</table>

<h2>Where to go from here</h2>

<p>
    For production, the next things to wire up are <strong>webhooks</strong>
    (so Mandrill can POST to your server when a message is delivered, opened,
    clicked, bounced or complained) and <strong>idempotency</strong> (don't
    re-send the same transactional email if your app retries a request). The
    reference has full docs for both at
    <a href="https://mailchimp.com/developer/transactional/api/webhooks/" target="_blank" rel="noopener">
        /developer/transactional/api/webhooks/
    </a>.
</p>

<p><a href="index.php">← Back to the overview</a></p>

<?php require 'footer.php'; ?>
