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 = 50000evicts oldest entries inthis.assignmentsso long-lived servers never leak RAM - Unit tests covering deterministic bucketing & conversion tracking:
plugin-1/test.js↗
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)
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.
| Endpoint | Method | Purpose |
|---|---|---|
| /api/evaluate | GET | Assigns or fetches variant for userId and records an impression |
| /api/track | POST | Records a conversion event for an evaluated userId |
| /api/results | GET | Returns impressions, conversions, lift, and Bayesian win probabilities |
| /api/stream | GET | Server-Sent Events stream pushing live result snapshots |
| /api/simulate | POST | Generates 10–5000 synthetic visitors to test statistical convergence |
| /api/extract | GET | Fetches live flags from Flagsmith API or local experiment engine |
| /embed | GET | Lightweight real-time experiment card designed for iframe embedding |