npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cfg-kit-posthog

v0.0.3

Published

PostHog plugin for cfg-kit - Define PostHog feature flags as configuration

Readme

cfg-kit-posthog

A PostHog plugin for cfg-kit that allows you to define PostHog feature flags as configuration.

Features

  • Feature Flag Management: Define PostHog feature flags in your configuration
  • Idempotent Operations: Only creates feature flags if they don't already exist
  • Type Safety: Full TypeScript support with Zod validation
  • Environment Variables: Automatic environment variable generation

Installation

npm install cfg-kit-posthog posthog-node cfg-kit
# or
pnpm add cfg-kit-posthog posthog-node cfg-kit
# or
yarn add cfg-kit-posthog posthog-node cfg-kit

Usage

Basic Setup

import { definePostHogConfig, PostHogPlugin } from 'cfg-kit-posthog'

const config = definePostHogConfig({
  featureFlags: [
    {
      key: 'new-dashboard',
      name: 'New Dashboard',
      description: 'Enable the new dashboard design',
      active: true,
      filters: {
        groups: [
          {
            properties: [],
            rollout_percentage: 100
          }
        ]
      },
      tags: ['ui', 'dashboard']
    }
  ]
})

const plugin = new PostHogPlugin(config, {
  apiKey: process.env.POSTHOG_API_KEY!,
  projectId: process.env.POSTHOG_PROJECT_ID!,
  host: 'https://app.posthog.com' // optional, defaults to app.posthog.com
})

With cfg-kit

import { defineConfig } from 'cfg-kit'
import { PostHogPlugin, definePostHogConfig } from 'cfg-kit-posthog'

export default defineConfig({
  plugins: [
    new PostHogPlugin(
      definePostHogConfig({
        featureFlags: [
          {
            key: 'beta-features',
            name: 'Beta Features',
            description: 'Enable beta features for testing',
            active: true,
            filters: {
              groups: [
                {
                  properties: [
                    {
                      key: 'email',
                      operator: 'icontains',
                      value: '@company.com'
                    }
                  ],
                  rollout_percentage: 50
                }
              ]
            }
          }
        ]
      }),
      {
        apiKey: process.env.POSTHOG_API_KEY!,
        projectId: process.env.POSTHOG_PROJECT_ID!
      }
    )
  ]
})

Configuration

Environment Variables

The plugin requires the following environment variables:

  • POSTHOG_API_KEY: Your PostHog API key
  • POSTHOG_PROJECT_ID: Your PostHog project ID
  • POSTHOG_HOST: (optional) PostHog host URL, defaults to https://app.posthog.com

Feature Flag Schema

type PostHogFeatureFlag = {
  key: string                    // Unique identifier for the feature flag
  name: string                   // Display name
  description?: string           // Optional description
  active?: boolean              // Whether the flag is active (default: true)
  filters?: {                   // Targeting filters
    groups: Array<{
      properties: Array<{
        key: string
        operator?: 'exact' | 'is_not' | 'icontains' | 'not_icontains' | 'regex' | 'not_regex' | 'gt' | 'gte' | 'lt' | 'lte' | 'is_set' | 'is_not_set'
        value?: string | number | boolean | string[] | number[]
        type?: 'person' | 'event' | 'element' | 'static-cohort' | 'behavioral'
      }>
      rollout_percentage: number  // 0-100
      variant?: string
    }>
  }
  variants?: Array<{            // Optional feature flag variants
    key: string
    name?: string
    rollout_percentage: number
  }>
  tags?: string[]              // Optional tags for organization
}

Behavior

Feature Flag Creation

  • Create Only: The plugin only creates feature flags that don't already exist in PostHog
  • No Updates: If a feature flag with the same key already exists, it will NOT be updated
  • Dashboard Management: Use the PostHog dashboard for updates and additional configuration

Generated Environment Variables

For each feature flag, the plugin generates environment variables with the feature flag ID:

// For a feature flag with key 'new-dashboard'
// Generates: NEW_DASHBOARD with the PostHog feature flag ID

Error Handling

The plugin includes comprehensive error handling:

  • API connection failures
  • Invalid API keys or project IDs
  • Feature flag creation errors
  • Network timeouts

Examples

Simple Feature Flag

definePostHogConfig({
  featureFlags: [
    {
      key: 'maintenance-mode',
      name: 'Maintenance Mode',
      description: 'Enable maintenance mode banner',
      active: false
    }
  ]
})

Advanced Feature Flag with Targeting

definePostHogConfig({
  featureFlags: [
    {
      key: 'premium-features',
      name: 'Premium Features',
      description: 'Enable premium features for paid users',
      active: true,
      filters: {
        groups: [
          {
            properties: [
              {
                key: 'subscription_tier',
                operator: 'exact',
                value: 'premium'
              }
            ],
            rollout_percentage: 100
          }
        ]
      },
      tags: ['premium', 'features']
    }
  ]
})

Feature Flag with Variants

definePostHogConfig({
  featureFlags: [
    {
      key: 'checkout-flow',
      name: 'Checkout Flow Test',
      description: 'A/B test different checkout flows',
      active: true,
      variants: [
        {
          key: 'control',
          name: 'Control (Original)',
          rollout_percentage: 50
        },
        {
          key: 'variant-a',
          name: 'Variant A (Simplified)',
          rollout_percentage: 50
        }
      ]
    }
  ]
})

API Reference

PostHogPlugin

Main plugin class for integrating with cfg-kit.

Constructor

new PostHogPlugin(config: PostHogConfig, options: PostHogPluginOptions)

Parameters:

  • config: PostHog configuration object
  • options: Plugin options including API key and project ID

definePostHogConfig(config)

Helper function to define and validate PostHog configuration.

definePostHogFeatureFlag(featureFlag)

Helper function to define and validate a single feature flag.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.