Tyagi // Engineering Log
note

Keeping sandbox traffic out of production analytics

Why analytics initialization should check environment/host before sending any events, not just before reporting them.

| 2 min read
analytics amplitude patterns

A common analytics bug isn’t sending the wrong event — it’s sending any event from a non-production context. Staging environments, local development, and QA testing can all generate real-looking user behavior that, if it reaches production analytics, quietly skews every funnel and retention number downstream.

The fix is usually simple: gate analytics provider initialization itself on environment/host checks, rather than trying to filter sandbox events out after they’ve already been sent. An explicit override (e.g., a forceSandbox flag) is still useful for intentionally testing analytics in a non-production environment — but it should be an opt-in exception, not the default.

Initializing an analytics SDK unconditionally and only gating

individual event calls later is a common half-fix — it still risks autocapture features (page views, session tracking) firing from non-production hosts before any manual gating logic runs.

A related trap is determining “production” from a value that’s easy to misconfigure per environment, like an environment variable that defaults to production unless explicitly overridden. That inverts the safe default: a missing or mistyped config value should fail toward not sending analytics, not toward silently treating an unrecognized environment as production. Detecting environment from something harder to get wrong — the actual hostname, a build-time flag baked in at compile time — is more robust than trusting a runtime string that every new environment has to remember to set correctly.

The same principle generalizes past analytics: any system with a “send this externally” side effect (webhooks, outbound emails, payment calls) benefits from the same fail-closed posture in non-production environments, since the cost of a missed signal is almost always lower than the cost of a leaked one.