launchdarkly-metric-instrument
launchdarkly/agent-skills
Add LaunchDarkly metric event tracking to your codebase with a single track() call.
What is launchdarkly-metric-instrument?
Instrument a LaunchDarkly metric event by inserting a track() call in the right place in your code. Use this when you need to wire up event tracking for a metric, instrument an action for experimentation, or verify that events are flowing to LaunchDarkly.
- Detect the LaunchDarkly SDK type (server-side or client-side) already in your codebase
- Locate the correct placement for the track() call based on user actions and existing patterns
- Generate the properly formatted track() call matching your SDK's signature and context requirements
- Verify that events are successfully flowing to LaunchDarkly using the list-metric-events tool
- Handle value metrics with numeric measurements and count/occurrence metrics appropriately
How to install launchdarkly-metric-instrument
npx skills add https://github.com/launchdarkly/agent-skills --skill launchdarkly-metric-instrument- LaunchDarkly MCP server must be configured in your environment
- LaunchDarkly SDK already installed in the codebase, or package manager and SDK key available for installation
- Access to the codebase to locate placement and write the track() call
How to use launchdarkly-metric-instrument
- 1.Search the codebase for existing track() calls to identify the SDK type and call signature pattern
- 2.If no track() calls exist, check package.json, requirements.txt, or equivalent for the LD SDK dependency and determine if it's server-side or client-side
- 3.Identify the location in code where the user action occurs (form submission, button click, API completion, etc.)
- 4.Confirm the placement location with the user before writing the track() call
- 5.Write the track() call using the correct signature for your SDK type, including context for server-side SDKs
- 6.Trigger the action in your local or staging environment and use list-metric-events to verify the event key appears
- 7.If the event doesn't appear, check event key casing, SDK initialization, context correctness, and environment selection
Use cases
- Add event tracking to a form submission or button click for experiment measurement
- Instrument an API endpoint completion to track latency or performance metrics
- Wire up a purchase or checkout action with revenue value for a value metric
- Verify that an instrumented event is reaching LaunchDarkly after deployment
- Add tracking to an existing feature flag evaluation to measure user behavior
- Backend engineers instrumenting server-side SDKs (Node, Python, Go, Java, Ruby, .NET)
- Frontend engineers using client-side SDKs (React, browser JavaScript)
- DevOps and platform teams setting up experiment tracking infrastructure
- Product engineers validating that metrics are flowing before launching experiments
launchdarkly-metric-instrument FAQ
No. Client-side SDKs (React, browser JS) set the context at initialization, so track() calls do not include context. Server-side SDKs (Node, Python, Go, etc.) require context to be passed with each track() call.
Only for value metrics that measure a numeric quantity (e.g., latency in ms, revenue in USD). For count and occurrence metrics, omit metricValue entirely.
Check: (1) event key casing matches exactly, (2) SDK is initialized before track() runs, (3) context is correct for server-side SDKs, (4) you're querying the right environment, (5) allow up to 5 minutes for data to appear, or call ldClient.flush() to force event delivery.
Events are ingested but won't appear in experiment results unless a flag variation() call was made first from the same context. The variation() call is what correlates the event to an experiment participant.
If the codebase already wraps LD calls (e.g., featureFlags.track() or analytics.ldTrack()), use that wrapper. Otherwise, call ldClient.track() directly, matching the existing pattern in the codebase.
Full instructions (SKILL.md)
Source of truth, from launchdarkly/agent-skills.
name: launchdarkly-metric-instrument description: "Instrument a LaunchDarkly metric event in a codebase by adding a track() call. Use when the user wants to wire up an event, instrument an action for a metric, add tracking to a feature, or confirm that an event is flowing to LaunchDarkly." license: Apache-2.0 compatibility: Requires the remotely hosted LaunchDarkly MCP server metadata: author: launchdarkly version: "1.0.0-experimental"
LaunchDarkly Metric Instrument
You're using a skill that will guide you through adding a track() 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:
list-metric-events— verify events are flowing after instrumentation
Optional MCP tools (enhance workflow):
get-project— 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
track()calls. This is the fastest signal:- Look for
ldClient.track(,.track(,ld.track( - If any exist, they tell you the SDK type, call signature, and context pattern in one shot — mirror those exactly.
- Look for
-
Search for SDK imports and initialization if no
track()calls exist:- Check
package.json,requirements.txt,go.mod,Gemfile,*.csprojfor an LD SDK dependency - Look for
LDClient,ldclient,launchdarkly-server-sdk,launchdarkly-node-server-sdk,launchdarkly-react-client-sdk, etc. - Find the initialization block to understand how the client is accessed across the codebase
- Check
-
Determine client-side or server-side. This is the most critical distinction — it determines the
track()signature:SDK type track()signatureNotes 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:
package-lock.json/yarn.lock/pnpm-lock.yaml→ npm/yarn/pnpm;Pipfile.lock/poetry.lock→ pip/poetry;go.sum→ go modules;Gemfile.lock→ 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
get-project— fetch the project and choose the key for the environment the user wants to instrument (typicallyproductionorstagingfor 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
track()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 (
segment.track(),mixpanel.track(),gtag()) — these are often co-located with where LD track calls should go - Comments like
// TODO: track this
-
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 track() Call
Write the call following the patterns found in Step 1.
Server-side SDKs — context is required:
ldClient.track('checkout-completed', context);
Client-side SDKs — context is implicit:
ldClient.track('checkout-completed');
For value metrics — include metricValue with the numeric measurement:
// 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
variation()calls) and use the same one. This is how LD correlates the event to the right experiment participant. metricValueonly forvaluemetrics. Forcountandoccurrencemetrics, omitmetricValueentirely.- Respect wrapper patterns. If the codebase wraps LD calls behind a utility (
featureFlags.track(),analytics.ldTrack()), add the new call through that wrapper — not by callingldClientdirectly. - Match the event key exactly.
track()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 list-metric-events 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 track() call match the metric's event key exactly? |
| SDK not initialized | Is ldClient initialized before the track() call runs? |
| Server-side: wrong context | Is the context passed to track() the same context used for variation() calls? |
| Client-side: no flag evaluation first | Has the SDK initialized and identified the user before track() is called? |
| Wrong environment | Is list-metric-events querying the same environment where the action was triggered? |
| Data delay | list-metric-events 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
track()calls only count in experiments when a flag is evaluated first. The event is correlated to an experiment participant because LD saw avariation()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
ldClient.flush()explicitly to see events appear immediately. - Server-side SDKs also buffer events. Calling
ldClient.flush()aftertrack()in development ensures the event is sent before the process exits or the test ends. metricValueunits must match the metric definition. If the metric was created with unitms, pass milliseconds. Passing seconds into a milliseconds metric will produce silently wrong results.- The
dataparameter is for custom metadata, not the metric value. Pass extra context (order ID, category, etc.) indata. Pass the numeric measurement inmetricValue.
References
- SDK Track Patterns —
track()call syntax, initialization, and package names for every supported SDK
Related skills
More from launchdarkly/agent-skills and the wider catalog.

mcp-configure
Configure LaunchDarkly's hosted MCP server for feature flag management in your coding agent.

migrate
Migrate an application with hardcoded LLM prompts to a full LaunchDarkly AgentControl implementation in five stages: audit the code, wrap the call, move the tools, add tracking, attach evaluators. Use when the user wants to externalize model/prompt configuration, move from direct provider calls (OpenAI, Anthropic, Bedrock, Gemini, Strands) to a managed config, or stage a full hardcoded-to-LaunchDarkly migration.

first-flag
Create and toggle your first LaunchDarkly feature flag end-to-end with evaluation code.

online-evals
Attach judges to config variations for automatic LLM-as-a-judge evaluation. Create custom judges, configure sampling rates, and monitor quality scores.

plan
Generate a minimal LaunchDarkly SDK integration plan from your detected stack.

projects
Guide for setting up LaunchDarkly projects in your codebase. Helps you assess your stack, choose the right approach, and integrate project management that makes sense for your architecture.