Yellow Theme

Next start · October 2026

Notes / 19

2026-09-15

Analytics that wait for a click (or eight seconds)

We dropped CloudWatch RUM into <head>. Lighthouse fell. The footer already promised anonymous CloudWatch. Engineering had to match that claim without blocking first paint.

Thesis: RUM is not a first-paint dependency. Load the bundle after the first pointer or key, or eight seconds after load — then still wait for idle before init.

What we shipped first

A synchronous or early deferred RUM script competed with fonts and theme boot. Scores tanked. Quiet visitors who never clicked still never needed the agent on the critical path — yet we paid for it on every cold load.

The footer already said anonymous CloudWatch usage analytics. Shipping a head script that hurt Lighthouse while the copy claimed a light touch was the wrong green.

The working shape

A tiny loader listens once for pointerdown and keydown. On load, a timer fires at eight seconds if nobody interacted. The injected bundle still uses requestIdleCallback before AwsRum init. CSP must allow the RUM endpoints or the defer buys nothing.

// Defer analytics: first input, else 8s after load — not a blocking <script> in <head>
(function () {
  if (window.__analyticsLoader) return;
  window.__analyticsLoader = true;
  var started = false;
  function load() {
    if (started) return;
    started = true;
    var s = document.createElement("script");
    s.src = "/assets/rum-bundle.js";
    s.defer = true;
    document.body.appendChild(s);
  }
  window.addEventListener("pointerdown", load, { once: true, capture: true, passive: true });
  window.addEventListener("keydown", load, { once: true, capture: true });
  window.addEventListener("load", function () {
    setTimeout(load, 8000);
  });
})();

// Inside the bundle: still wait for idle before AwsRum init
// if (typeof requestIdleCallback === "function") {
//   requestIdleCallback(initRum, { timeout: 2000 });
// }

Checklist

Related: late font faces still swap after paint. How we deliver: methodology.

Engineering commentary only — not audit, legal, or certification advice.