Boolean Build

A traffic report that silently lost half its pageviews

A morning traffic read said 2 paths, 3 pageviews. The same window held five paths and seven pageviews. The script exited 0, printed no warning, and both halves of that sentence came from the same program — which is exactly why the number was believed.

The report made two API calls per run. /stats/total produced the headline pageview count; /stats/hits produced the per-path list. Both asked for the same range, and neither result was ever checked against the other.

Why it was silent

A second error was hiding in the same output. Every smoke check and event ping this site fires lands on a path containing zz-, and the counter treats it as a visitor. On 11-17, 4 of 11 pageviews were our own test traffic. So the number was wrong in both directions at once, and both errors read as ordinary variance.

The one-line check

Ask both endpoints for the same window and compare. They describe the same set of requests, so the totals should agree within the hits limit:


      curl -s -H "Authorization: Bearer $GOATCOUNTER_TOKEN" \
        "https://booleanbros.goatcounter.com/api/v0/stats/total?start=2026-11-16T00:00:00Z&end=2026-11-22T23:59:59Z" | jq .total
      
      curl -s -H "Authorization: Bearer $GOATCOUNTER_TOKEN" \
        "https://booleanbros.goatcounter.com/api/v0/stats/hits?limit=100&start=2026-11-16T00:00:00Z&end=2026-11-22T23:59:59Z" \
        | jq '[.hits[].count] | add'
      

Three against seven is the bug, not a trend. Any gap larger than a few pageviews means one of the two responses is partial. The fix is not to average them.

The fix

Retry the status codes that mean "try again" and fail on the ones that mean "you are wrong":


      const RETRYABLE = new Set([404, 429, 500, 502, 503, 504]);
      
      if (!RETRYABLE.has(res.status)) throw new Error(msg);
      await new Promise((r) => setTimeout(r, 400 * (attempt + 1) ** 2));
      

Two retries, 400 ms and then 1.6 s, clears a rate limit and costs nothing in a read-only tool. A 400 or 401 still throws on the first response, because retrying a bad token only hides the cause.

Second, the headline has to survive a failed totals call without inventing a number. The fallback is the summed path count, labelled every time it is used:


      console.log(`pageviews: ~${realSum} (FALLBACK — /stats/total failed: ${totalsError})`);
      console.log('  summed path count, undercounts past the hits limit; quote it with the ~');
      

The tilde and the parenthetical travel with the number. Whoever copies it into a brief inherits the caveat, which is the only dependable way to stop an estimate from hardening into a fact two steps downstream.

Third, diff the two numbers and say so when they disagree:


      const gap = Math.abs(realTotal - realSum);
      if (realSum > 0 && gap > Math.max(3, realTotal * 0.2)) {
        console.log(`! totals and path sum disagree by ${gap} — one response is partial`);
      }
      

Test artifacts come out of both sides before any of that arithmetic, matching /zz-/i against the path, so the comparison is between real traffic numbers rather than a real total and a polluted sum.

The gate

The change was verified against a stub server instead of the live API, one failure per run: a 500 on totals printed the fallback line, a total of 30 against a path sum of 6 printed the divergence warning, and a bad token printed paths: unavailable and exited 1. Then the live run: seven pageviews, five paths, exit 0.

None of that would have caught the original problem by itself. The rule that does is smaller: never print two numbers describing the same set without diffing them in the same breath. A metric that can halve itself with no error and no warning is not a measurement — it is a guess with a timestamp.