All skills
launchdarkly avatar

/launchdarkly-metric-instrument

@4fdba42 official

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.

Use this Skill: https://skilld.dev/gh/launchdarkly/agent-skills/launchdarkly-metric-instrument

This session only. Nothing lands on disk.

referencessdk-track-patterns.md

≈2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

SDK Track Patterns

How to call track() in each LaunchDarkly SDK. Use this reference to match the patterns already in use in the codebase — and to add the right call when starting fresh.

The key distinction across all SDKs: server-side SDKs require a context per call; client-side SDKs do not.


JavaScript / TypeScript — Node.js (Server-side)

Package: @launchdarkly/node-server-sdk (v9+) or launchdarkly-node-server-sdk (v6–v8)

npm install @launchdarkly/node-server-sdk
import * as ld from '@launchdarkly/node-server-sdk';

const client = ld.init(process.env.LD_SDK_KEY!);
await client.waitForInitialization();

// Count / occurrence metric (no metricValue)
client.track('checkout-completed', context);

// Value metric — pass the measurement as metricValue
client.track('api-response-time', context, null, responseTimeMs);

// With custom data payload
client.track('item-purchased', context, { itemId: 'abc123', category: 'apparel' }, purchaseAmount);

// Flush explicitly in tests / short-lived processes
await client.flush();

JavaScript / TypeScript — Browser (Client-side)

Package: launchdarkly-js-client-sdk

npm install launchdarkly-js-client-sdk
import * as ld from 'launchdarkly-js-client-sdk';

const client = ld.initialize(clientSideId, context);
await client.waitForInitialization();

// Count / occurrence metric — no context, no metricValue
client.track('signup-completed');

// Value metric
client.track('page-load-time', null, performanceMs);

// With custom data
client.track('item-added-to-cart', { itemId: 'abc123' }, itemPrice);

// Flush (useful in tests or before navigating away)
await client.flush();

React (Client-side)

Package: launchdarkly-react-client-sdk

npm install launchdarkly-react-client-sdk
import { useLDClient } from 'launchdarkly-react-client-sdk';

function CheckoutButton() {
  const ldClient = useLDClient();

  const handleSubmit = async () => {
    await processCheckout();

    // Count / occurrence metric
    ldClient?.track('checkout-completed');

    // Value metric
    ldClient?.track('checkout-revenue', null, orderTotal);
  };

  return <button onClick={handleSubmit}>Complete Order</button>;
}

Initialization (typically in the app root via LDProvider or asyncWithLDProvider):

import { LDProvider } from 'launchdarkly-react-client-sdk';

// Wrap your app — context is set here, not in each track() call
<LDProvider clientSideID={clientSideId} context={userContext}>
  <App />
</LDProvider>

Python (Server-side)

Package: launchdarkly-server-sdk

pip install launchdarkly-server-sdk
import ldclient
from ldclient.config import Config

ldclient.set_config(Config(sdk_key))
client = ldclient.get()

# Count / occurrence metric
client.track('checkout-completed', context)

# Value metric
client.track('api-response-time', context, None, response_time_ms)

# With data payload
client.track('item-purchased', context, {'item_id': 'abc123'}, purchase_amount)

# Flush
client.flush()

Go (Server-side)

Package: github.com/launchdarkly/go-server-sdk/v7

go get github.com/launchdarkly/go-server-sdk/v7
import (
    ld "github.com/launchdarkly/go-server-sdk/v7"
    "github.com/launchdarkly/go-server-sdk/v7/ldcontext"
)

client, _ := ld.MakeClient(sdkKey, 5*time.Second)
defer client.Close()

// Count / occurrence metric
client.TrackEvent("checkout-completed", context)

// Value metric
client.TrackMetric("api-response-time", context, responseTimeMs, nil)

// With data payload
data := ldvalue.BuildObject().Set("itemId", ldvalue.String("abc123")).Build()
client.TrackData("item-purchased", context, data)

Java (Server-side)

Package: com.launchdarkly:launchdarkly-java-server-sdk

<!-- Maven -->
<dependency>
  <groupId>com.launchdarkly</groupId>
  <artifactId>launchdarkly-java-server-sdk</artifactId>
  <version>7.x.x</version>
</dependency>
LDClient client = new LDClient(sdkKey);

// Count / occurrence metric
client.track("checkout-completed", context);

// Value metric
client.trackMetric("api-response-time", context, responseTimeMs);

// With data payload
LDValue data = LDValue.buildObject().put("itemId", "abc123").build();
client.trackData("item-purchased", context, data);
client.trackMetric("item-purchased", context, purchaseAmount);

// Flush
client.flush();
client.close();

Ruby (Server-side)

Package: launchdarkly-server-sdk

gem install launchdarkly-server-sdk
require 'ldclient-rb'

client = LaunchDarkly::LDClient.new(sdk_key)

# Count / occurrence metric
client.track('checkout-completed', context)

# Value metric
client.track('api-response-time', context, nil, response_time_ms)

# With data payload
client.track('item-purchased', context, { item_id: 'abc123' }, purchase_amount)

# Flush
client.flush

.NET / C# (Server-side)

Package: LaunchDarkly.ServerSdk

dotnet add package LaunchDarkly.ServerSdk
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Server;

var client = new LdClient(sdkKey);

// Count / occurrence metric
client.Track("checkout-completed", context);

// Value metric
client.Track("api-response-time", context, LdValue.Null, responseTimeMs);

// With data payload
var data = LdValue.BuildObject().Add("itemId", "abc123").Build();
client.Track("item-purchased", context, data, purchaseAmount);

// Flush
client.Flush();

iOS / Swift (Client-side)

Package: LaunchDarkly via Swift Package Manager or CocoaPods

import LaunchDarkly

// Context set during LDClient.start() — not required per track call

// Count / occurrence metric
LDClient.get()!.trackEvent(key: "checkout-completed")

// Value metric
LDClient.get()!.trackEvent(key: "api-response-time", metricValue: responseTimeMs)

// With data payload
LDClient.get()!.trackEvent(key: "item-purchased", data: ["itemId": "abc123"], metricValue: purchaseAmount)

// Flush
LDClient.get()!.flush()

Android / Kotlin (Client-side)

Package: com.launchdarkly:launchdarkly-android-client-sdk

// Context set during LDClient.init() — not required per track call
val client = LDClient.get()

// Count / occurrence metric
client.trackEvent("checkout-completed")

// Value metric
client.trackMetric("api-response-time", null, responseTimeMs)

// With data payload
val data = LDValue.buildObject().put("itemId", "abc123").build()
client.trackData("item-purchased", data, purchaseAmount)

// Flush
client.flush()

Common Wrapper Patterns

Many teams abstract LaunchDarkly calls behind a service or utility. Search for these patterns before adding a raw ldClient.track() call:

// Service wrappers
analyticsService.track('event-key');
featureFlagService.trackEvent('event-key', context);
tracking.record('event-key', metricValue);

// Module-level wrappers
import { trackEvent } from '../lib/launchdarkly';
trackEvent('event-key', context, metricValue);

// Class-based
this.ldService.track('event-key');
LDWrapper.getInstance().track('event-key');

If any of these exist, add the new call through the same wrapper — don't bypass it.


metricValue Quick Reference

Metric type Pass metricValue? Example
count No — omit it client.track('button-clicked', context)
occurrence No — omit it client.track('signup-completed', context)
value (average) Yes — the measurement client.track('page-load', context, null, 342)
value (sum) Yes — the per-event amount client.track('revenue', context, null, 49.99)

The data parameter (before metricValue) is for arbitrary metadata — order IDs, category names, etc. It does not affect metric calculations.

Source: SKILL.md on GitHub

1 alert3mo4 checks · Risk SAFE
  • Gen Agent Trust Hub3mo

    The skill provides safe and standard instructions for adding LaunchDarkly metric tracking to a codebase. It correctly identifies and uses official LaunchDarkly SDKs and standard package management tools.

  • Socket3mo

    No alerts

  • Snyk3mo

    Risk: HIGH · 1 issue

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4fdba42. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 6 months ago
compatibility
Requires the remotely hosted LaunchDarkly MCP server
metadata
{
  "author": "launchdarkly",
  "version": "1.0.0-experimental"
}
  • launchdarkly
  • instrumentation
  • tracking
  • events
  • metrics
  • analytics
  • server-side
  • client-side
  • sdk

README badge

README badge for launchdarkly/agent-skills/launchdarkly-metric-instrument

Adds a track() call to instrument a LaunchDarkly metric event in your codebase, detecting the SDK type (server-side or client-side), placing the call correctly, and verifying events reach LaunchDarkly via the MCP server. Use this when wiring up event tracking for experiments or metrics.

Generated from the current SKILL.md.

Does this skill work with client-side SDKs like React?
Yes. Client-side SDKs (React, browser JS) use a simpler `track()` signature without a context parameter, since context is set at initialization. The skill detects the SDK type and writes the correct call for both client and server implementations.
What if the LaunchDarkly SDK isn't already installed?
The skill will detect the package manager from lockfiles, install the appropriate SDK, fetch the SDK key via the LaunchDarkly MCP server, and add initialization code following patterns already in your codebase.
How do I know the event is actually flowing to LaunchDarkly?
After adding the track() call, the skill uses the `list-metric-events` MCP tool to query your project and confirm the event key appears. If it doesn't show up, the skill walks through a checklist (wrong casing, SDK not initialized, wrong context, etc.).
Do I need to pass metricValue for all metrics?
No. Only pass `metricValue` for value metrics that measure numeric data (e.g. latency in milliseconds, purchase amount). For count and occurrence metrics, omit it entirely.
Does this skill require the LaunchDarkly MCP server?
Yes. The skill requires the remotely hosted LaunchDarkly MCP server configured in your environment to fetch project details and verify events are flowing.

Generated from the current SKILL.md. These answers refresh after source changes.