Middleware
Use this when you have neither server logs nor a log drain: Vercel Hobby, Netlify, Render, Railway, Fly. The app sends one line per request to Growbeat, in the background, so pages never wait for it.
When to choose it
- Your host doesn't give you the access log, and your domain is not on Cloudflare (if it is, the Worker sees more, because it also counts requests that never reach your app).
- You can change the app's code, or ask your AI to.
What it sends
One JSON line per request: time, IP, method, path, status, user agent, referrer and host. The IP is hashed with the user agent into a daily visitor id on our side; we don't build profiles. The format is the same as the ingest API.
What it can't see
Requests answered before your code runs, such as files a CDN serves from its cache, may not reach the middleware. If your CSS and JS don't pass through it, the «loaded CSS/JS» check can't run and visitors are counted by user agent alone. How we count explains what changes.
Code
Next.js (middleware.ts) — Netlify, Render, Railway, any Node host
import { NextResponse, type NextRequest, type NextFetchEvent } from "next/server";
export function middleware(req: NextRequest, ev: NextFetchEvent) {
ev.waitUntil(fetch("https://getgrowbeat.com/api/v1/ingest", {
method: "POST",
headers: { Authorization: "Bearer tl_in_YOUR_TOKEN", "Content-Type": "application/x-ndjson" },
body: JSON.stringify({
ts: new Date().toISOString(),
ip: req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || "",
method: req.method, path: req.nextUrl.pathname + req.nextUrl.search,
status: 200, // middleware runs before the page: status is assumed
ua: req.headers.get("user-agent") || "", ref: req.headers.get("referer") || "",
host: req.nextUrl.host, src: "middleware",
}) + "\n",
}).catch(() => {}));
return NextResponse.next();
}
export const config = { matcher: ["/((?!_next/image|favicon.ico).*)"] };
Django
# settings.py → MIDDLEWARE += ["yourapp.growbeat.GrowbeatMiddleware"]
import json, threading, time, urllib.request
from datetime import datetime, timezone
ENDPOINT, TOKEN = "https://getgrowbeat.com/api/v1/ingest", "tl_in_YOUR_TOKEN"
def _send(line):
req = urllib.request.Request(ENDPOINT, data=line.encode(), method="POST", headers={
"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/x-ndjson"})
try:
urllib.request.urlopen(req, timeout=3)
except Exception:
pass
class GrowbeatMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
t0 = time.monotonic()
response = self.get_response(request)
m = request.META
line = json.dumps({
"ts": datetime.now(timezone.utc).isoformat(),
"ip": (m.get("HTTP_X_FORWARDED_FOR") or m.get("REMOTE_ADDR", "")).split(",")[0].strip(),
"method": request.method, "path": request.get_full_path(), "status": response.status_code,
"ua": m.get("HTTP_USER_AGENT", ""), "ref": m.get("HTTP_REFERER", ""),
"host": request.get_host(), "rt": round(time.monotonic() - t0, 4), "src": "middleware"}) + "\n"
threading.Thread(target=_send, args=(line,), daemon=True).start()
return response
Middleware sends one request per hit: fine for small sites. Above ~20 req/s prefer the agent or a log drain, which batch.
Check it works
Deploy, open any page of your site, and the install screen in Growbeat switches to «receiving» within a minute. If nothing arrives, check the token and that outgoing requests to https://getgrowbeat.com are allowed by your host.