PostHog Web
🚧 This is a WIP. Currently the only officially supported way of using PostHog on the web is posthog-js
This package is currently published to npm as posthog-js-lite and is a simplified version of the recommended and offically supported posthog-js
.
Installation
npm i -s posthog-js-lite
yarn add posthog-js-lite
It is entirely written in Typescript and has a minimal API as follows:
import PostHog from 'posthog-js-lite'
const posthog = new PostHog('my-api-key', {
})
posthog.capture('my-event', { myProperty: 'foo' })
posthog.identify('my-unique-user-id', { email: 'exampke@posthog.com', name: 'Jane Doe' })
posthog.reset()
posthog.register({ itemsInBasket: 3 })
posthog.unregister('itemsInBasket')
posthog.group('organisations', 'org-1')
posthog.group({ organisations: 'org-1', project: 'project-1' })
if (posthog.isFeatureEnabled('my-feature-flag')) {
renderFlaggedFunctionality()
} else {
renderDefaultFunctionality()
}
if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'variant1') {
renderVariant1()
} else if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'variant2') {
renderVariant1()
} else if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'control') {
renderControl()
}
posthog.overrideFeatureFlag('my-feature-flag', true)
posthog.onFeatureFlag('my-feature-flag', (value) => {
respondToFeatureFlagChange(value)
})
posthog.optOut()
posthog.optIn()