← Back to Damru Overview
02 / A/B ENGINE, BAYESIAN STATS & SSE

Experiment

Plugin 1 pairs Flagsmith feature flags with deterministic SHA-256 local bucketing, Bayesian Beta-Binomial win probabilities, and a real-time Server-Sent Events (SSE) telemetry stream.

BUCKETING ENGINE

Flagsmith API Evaluation with Deterministic Local Fallback

The core FlagsmithExperimentClient class in plugin-1/flagsmith-experiment-plugin.js ↗ accepts a Flagsmith Environment API key (FLAGSMITH_ENV_KEY) and base URL (FLAGSMITH_API_URL). When configured, evaluate(userId) queries Flagsmith’s /identities/?identifier=... endpoint for the signup_form_variant flag.

If no environment key is present or the upstream API is unreachable, the client transparently falls back to bucketLocally(userId). It hashes ${flagName}:${userId} with SHA-256 and takes readUInt32BE(0) % variants.length, guaranteeing sticky, deterministic assignment across control_6_field and variant_3_field.

  • Identifier validation: SAFE_ID_REGEX = /^[a-zA-Z0-9_.-]{1,128}$/ prevents injection and oversized keys
  • Bounded memory: MAX_ASSIGNMENTS = 50000 evicts oldest entries in this.assignments so long-lived servers never leak RAM
  • Unit tests covering deterministic bucketing & conversion tracking: plugin-1/test.js ↗
STATISTICAL MODEL

Conversion Tracking & Bayesian Win Probability

Whenever a user submits the signup form or simulated traffic runs, trackConversion(userId, eventName) increments variant conversion counts only for recorded impressions, preventing phantom conversion inflation.

The getResults() method computes per-variant conversion rates, relative lift, and Bayesian Beta-Binomial win probabilities (recommendedWinner) in plugin-1/flagsmith-experiment-plugin.js ↗. Every evaluation, conversion, simulation, or reset triggers broadcastResults() in plugin-1/server.js ↗, pushing live JSON frames over GET /api/stream.

GET  /api/evaluate?userId=usr_123
POST /api/track     { "userId": "usr_123", "eventName": "signup_completed" }
GET  /api/stream    text/event-stream (live SSE updates)
HTTP API & EMBED

Endpoints & Live /embed Widget

The experimentation server in plugin-1/server.js ↗ exposes both JSON REST endpoints and an iframe-ready /embed micro-dashboard (served with X-Frame-Options: SAMEORIGIN) that subscribes directly to /api/stream.

EndpointMethodPurpose
/api/evaluateGETAssigns or fetches variant for userId and records an impression
/api/trackPOSTRecords a conversion event for an evaluated userId
/api/resultsGETReturns impressions, conversions, lift, and Bayesian win probabilities
/api/streamGETServer-Sent Events stream pushing live result snapshots
/api/simulatePOSTGenerates 10–5000 synthetic visitors to test statistical convergence
/api/extractGETFetches live flags from Flagsmith API or local experiment engine
/embedGETLightweight real-time experiment card designed for iframe embedding