Guides / SaaS

How to Track Feature Flag Events in Your App

3 min readยทSaaS

Why log feature flag events

Feature flags are invisible by default. Logging them lets you:

  • Know exactly when a flag was enabled in production
  • Correlate a spike in errors with a recent flag change
  • Track which users got the experimental version

Wrap your flag client

ts
// lib/flags.ts
import { logit } from "@/lib/logit";

interface FlagEval {
  flagKey: string;
  value: boolean | string;
  userId: string;
  source: "launchdarkly" | "growthbook" | "posthog" | "custom";
}

export async function trackFlagEval({ flagKey, value, userId, source }: FlagEval) {
  // Only log enabled flags to avoid noise
  if (value === false) return;

  await logit.now("feature-flags", {
    event: "Feature flag enabled",
    description: `${flagKey} โ†’ ${String(value)} for user ${userId}`,
    icon: "๐Ÿšฉ",
    notify: false,
    tags: { flag: flagKey, source },
    metadata: { flagKey, value: String(value), userId, source },
  });
}

Log flag rollouts (admin action)

ts
// When you toggle a flag in your admin panel
export async function trackFlagToggle(
  flagKey: string,
  newValue: boolean,
  toggledBy: string
) {
  await logit.now("feature-flags", {
    event: newValue ? "Flag enabled" : "Flag disabled",
    description: `${flagKey} toggled by ${toggledBy}`,
    icon: newValue ? "๐ŸŸข" : "๐Ÿ”ด",
    notify: true,
    tags: { flag: flagKey, action: newValue ? "enable" : "disable" },
    metadata: { flagKey, newValue, toggledBy, timestamp: new Date().toISOString() },
  });
}

With LaunchDarkly

ts
import * as LaunchDarkly from "@launchdarkly/node-server-sdk";

const ldClient = await LaunchDarkly.init(process.env.LD_SDK_KEY!);

const flagValue = await ldClient.variation("new-checkout", { key: userId }, false);
await trackFlagEval({ flagKey: "new-checkout", value: flagValue, userId, source: "launchdarkly" });

With PostHog Feature Flags

ts
import { PostHog } from "posthog-node";
const posthog = new PostHog(process.env.POSTHOG_API_KEY!);

const isEnabled = await posthog.isFeatureEnabled("new-checkout", userId);
if (isEnabled) {
  await trackFlagEval({ flagKey: "new-checkout", value: true, userId, source: "posthog" });
}

Try LogIt free

7-day trial. No credit card required.

Start free