PostHog

Installing

npm install posthog-js

In site.ts;

In imports;

import posthog from 'posthog-js'

In the setup() method;

e.g.;

posthog.init('phc_YOUR_KEY', { api_host: 'https://us.i.posthog.com', person_profiles: 'identified_only' })

Usage Notes

In the exec() method, you'd

Feature Flags

Good for;

  • Switching on or off specific features

  • Doing a limited deployment of a feature to a smaller audience

if (posthog.isFeatureEnabled('flag-key') ) {
    // Do something differently for this user

    // Optional: fetch the payload
    const matchedFlagPayload = posthog.getFeatureFlagPayload('flag-key')
}

Experiments (A/B tests)

  • Good for handling variants

if (posthog.getFeatureFlag('flag-key')  == 'variant-key') { // replace 'variant-key' with the key of your variant
    // Do something differently for this user
    
    // Optional: fetch the payload
    const matchedFlagPayload = posthog.getFeatureFlagPayload('flag-key')
}

Last updated