Frontend System Design · Lesson 86 of 86 · 5 min
Experiment Data Collection and Analytics
This is how you prove you think about the business impact of your code, not just the code itself.
After you've used a feature flag to ship an experiment, you have to measure it. This is where data collection comes in. In an interview, this topic connects the technical details of the frontend to the business goals. Your ability to design a system that produces clean, reliable data for experiment analysis is a strong signal of seniority.
Define your instrumentation strategy
Before you write a line of tracking code, you must know what you're measuring. An analytics strategy for an experiment has two core event types.
Exposure events: These fire when a user is exposed to a variant of an experiment. This event tells your analytics system, "User 123 saw the 'control' version of the new-checkout-button experiment." Without this, you can't segment users into experiment groups.
Metric events: These track the user behaviors you believe the experiment will affect. For a new checkout button, the primary metric event might be order_completed. You might also track secondary metrics like payment_info_submitted or shipping_address_error.
The trap: Don't just start tracking everything. This creates noise, costs money, and can slow down the client. Conversely, failing to track the one key metric makes the entire experiment useless. A senior answer always starts with collaborating with product and data science to define a clear hypothesis and a minimal, sufficient set of events.
A good strategy is codified in a data taxonomy or schema. This is a document that defines event names, their triggers, and the properties they include. For example:
experiment_viewedexperiment_name: (string) e.g., "new-checkout-button"variant: (string) e.g., "control", "variant_A"
order_completedorder_id: (string)order_total: (number)currency: (string)
This schema becomes the contract between the frontend, backend, and the data analysis platform.
Choose your transport: client-direct vs. server-proxy
Once you know what to send, you have to decide how to send it. There are two main architectures.
Client-direct collection is the simplest approach. The frontend app integrates an analytics SDK (from Google Analytics, Amplitude, etc.) and calls it directly. The SDK handles batching, retries, and sending data to the analytics provider's API. This is fast to implement and works for many use cases. The main drawbacks are performance impact from the SDK and data loss from ad blockers, which block requests to known analytics domains.
Server-proxy collection (also called a "beacon" or "gateway") adds a middleman: your own backend. The frontend sends a lightweight request to an endpoint on your domain (e.g., /api/log). This backend service then validates, enriches, and forwards the data to one or more third-party analytics platforms.
This solves the ad blocker problem, as the initial request goes to your own domain. It also gives you more control. You can enrich events with server-side data (like geo-IP lookups), validate schemas to prevent bad data from ever reaching your analytics tool, and fan out events to multiple destinations (e.g., a data warehouse and a marketing tool) without adding more SDKs to the client. The trade-off is increased complexity and infrastructure cost.
In an interview, show you understand this trade-off. A good way to frame it is by stage of company. "For an early-stage product, I'd use a client-direct SDK to move quickly. For a mature e-commerce platform where every conversion matters and data quality is paramount, I would invest in a server-proxy to guarantee delivery and bypass ad blockers."
Implement a resilient tracking client
Regardless of the transport, the client-side implementation should be clean and robust. The best pattern is to create your own wrapper or facade around the tracking mechanism. This insulates your application from the specific analytics provider and provides a single place to add global context.
Your wrapper should automatically attach contextual data to every event, especially active experiments. This is how you connect a user's actions to the experiment variants they were exposed to.
Here is a minimal, effective analytics wrapper. It abstracts the final sending logic and automatically enriches events with context from a feature flagging system.
import { featureFlags } from './feature-flags';
// The context that gets added to every event.
function getGlobalContext() {
return {
page_url: window.location.href,
timestamp: new Date().toISOString(),
// Get active experiments from your feature flag system
active_experiments: featureFlags.getActiveExperiments(),
};
}
/**
* Tracks an analytics event.
* In a real app, this would send data to an SDK or a server-proxy endpoint.
*/
export function trackEvent(
eventName: string,
properties: Record<string, any>
): void {
const eventPayload = {
event: eventName,
properties: {
...properties,
...getGlobalContext(),
},
};
console.log('[ANALYTICS]', JSON.stringify(eventPayload, null, 2));
// Example: sending to a server-proxy
// navigator.sendBeacon('/api/log', JSON.stringify(eventPayload));
}
This approach makes your component code simple: trackEvent('add_to_cart_clicked', { productId: 'abc' }). All the contextual boilerplate is handled for you.
For events that fire on page unload, like checkout_abandoned, use navigator.sendBeacon(). It's a non-blocking API designed to send small payloads before a page unloads, making it more reliable than a standard fetch.
What a good answer sounds like
When asked to design a system for collecting experiment data, structure your answer to show you think through the problem from end to end.
"First, I'd clarify the goals with the product manager. What is the primary success metric for this experiment? Let's say we're testing a new sign-up button, and the goal is to increase registrations.
We'll need two key events. An
experiment_viewedevent with properties forexperiment_name: 'new-signup-button'and thevariant. And asignup_completedevent.For the architecture, given that sign-ups are critical, I'd advocate for a server-proxy. This avoids data loss from ad blockers. The client will send events to our
/api/analyticsendpoint.On the frontend, I'd create a lightweight
AnalyticsServicewrapper. This service would have a singletrackmethod. It would automatically pull the active experiment variant from our feature flagging service and attach it to every outgoing event payload. This ensures we can always attribute user actions back to the experiment group they were in.Finally, for any events that might fire as the user is leaving the page, I'd use
navigator.sendBeaconin our service to ensure reliable delivery."
This answer covers strategy, architecture, implementation, and reliability. It demonstrates a mature understanding of the entire system.
Try it
Write a 20-line analytics wrapper function in TypeScript. It should take an eventName string and a properties object. Inside the function, add timestamp and page_url properties to the payload, then log the final object to the console. This simulates the core logic of an analytics facade.
Next: This was the final lesson. It's time to put it all together in a mock interview.
Lock it in — apply this lesson under interview conditions.