LaunchDarkly Metric Instrument
You're using a skill that will guide you through adding a
call to a codebase so a LaunchDarkly metric can measure it. Your job is to detect the SDK in use, find the right place in code to add the call, write it correctly, and verify that events are reaching LaunchDarkly.
Prerequisites
This skill requires the remotely hosted LaunchDarkly MCP server to be configured in your environment.
Required MCP tools:
- — verify events are flowing after instrumentation
Optional MCP tools (enhance workflow):
- — retrieve the SDK key for the right environment when SDK initialization is needed
Workflow
Step 1: Detect the SDK
Before writing any code, understand the LaunchDarkly setup already in this codebase.
-
Search for existing calls. This is the fastest signal:
- Look for , ,
- If any exist, they tell you the SDK type, call signature, and context pattern in one shot — mirror those exactly.
-
Search for SDK imports and initialization if no
calls exist:
- Check , , , , for an LD SDK dependency
- Look for , , ,
launchdarkly-node-server-sdk
, launchdarkly-react-client-sdk
, etc.
- Find the initialization block to understand how the client is accessed across the codebase
-
Determine client-side or server-side. This is the most critical distinction — it determines the
signature:
| SDK type | signature | Notes |
|---|
| Server-side (Node, Python, Go, Java, Ruby, .NET) | ldClient.track(eventKey, context, data?, metricValue?)
| Context required per call |
| Client-side (React, browser JS) | ldClient.track(eventKey, data?, metricValue?)
| Context set at init, not per call |
See SDK Track Patterns for full examples by language.
Step 2: Install & Initialize (if SDK not present)
Skip this step if the SDK is already in the codebase.
-
Detect the package manager from lockfiles:
/
/
→ npm/yarn/pnpm;
/
→ pip/poetry;
→ go modules;
→ bundler.
-
Install the appropriate SDK using the detected package manager. See SDK Track Patterns for the right package name per language.
-
Get the SDK key using
— fetch the project and choose the key for the environment the user wants to instrument (typically
or
for initial testing).
-
Add SDK initialization following the patterns already in this codebase. If there's a central config or service layer, add the LD client there. See SDK Track Patterns for initialization examples.
Step 3: Find the Right Placement
Locate where in the code the user action or event occurs.
-
Ask if you're not sure where the action happens. Don't guess at placement — a
call in the wrong location (e.g. a render method instead of a submit handler) produces misleading data.
-
Look for signals of the right location:
- Form submissions, button click handlers, API route completions, mutation hooks
- Existing analytics calls (, , ) — these are often co-located with where LD track calls should go
- Comments like
-
Show the candidate location to the user before writing anything:
I'll add the track() call here, in the checkout submit handler (src/checkout/CheckoutForm.tsx, line 47).
Does that look right?
-
Proceed once confirmed (or if you're confident enough from codebase signals).
Step 4: Write the Call
Write the call following the patterns found in Step 1.
Server-side SDKs — context is required:
typescript
ldClient.track('checkout-completed', context);
Client-side SDKs — context is implicit:
typescript
ldClient.track('checkout-completed');
For metrics — include
with the numeric measurement:
typescript
// Server-side: latency metric (ms)
ldClient.track('api-response-time', context, null, responseTimeMs);
// Client-side: revenue metric
ldClient.track('purchase-completed', { orderId }, purchaseAmountUSD);
Key rules:
- Match the existing context. Don't construct a new context inline. Find where the codebase already builds its context/user object (used for calls) and use the same one. This is how LD correlates the event to the right experiment participant.
- only for metrics. For and metrics, omit entirely.
- Respect wrapper patterns. If the codebase wraps LD calls behind a utility (, ), add the new call through that wrapper — not by calling directly.
- Match the event key exactly. event keys are case-sensitive. Use the exact string that the metric was created with.
See SDK Track Patterns for full per-language examples.
Step 5: Verify
Guide the user to trigger the action in their local or staging environment. Then use
to confirm the event key appears:
list-metric-events(projectKey, environmentKey)
If the event key appears: confirm success and show a summary.
If the event key is absent after triggering, work through this checklist:
| Problem | Check |
|---|
| Wrong event key casing | Does the call match the metric's event key exactly? |
| SDK not initialized | Is initialized before the call runs? |
| Server-side: wrong context | Is the context passed to the same context used for calls? |
| Client-side: no flag evaluation first | Has the SDK initialized and identified the user before is called? |
| Wrong environment | Is querying the same environment where the action was triggered? |
| Data delay | shows the last 90 days with up to ~5 min delay — try again in a moment |
Surface a summary once verified:
✓ Event flowing: checkout-completed
Seen in: production
Next: this event is now ready to back a metric. Use the metric-create skill to set one up,
or attach an existing metric to your experiment.
Important Context
- calls only count in experiments when a flag is evaluated first. The event is correlated to an experiment participant because LD saw a call from that context. If the user triggers the action without evaluating any flag, the event may still be ingested but won't appear in experiment results.
- Client-side SDKs flush events on an interval (default ~30 seconds) or on page unload. In tests, you may need to call explicitly to see events appear immediately.
- Server-side SDKs also buffer events. Calling after in development ensures the event is sent before the process exits or the test ends.
- units must match the metric definition. If the metric was created with unit , pass milliseconds. Passing seconds into a milliseconds metric will produce silently wrong results.
- The parameter is for custom metadata, not the metric value. Pass extra context (order ID, category, etc.) in . Pass the numeric measurement in .
References
- SDK Track Patterns — call syntax, initialization, and package names for every supported SDK