PostHog Web
🚧 This is a reduced feature set package. Currently the only officially supported feature complete 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 officially supported posthog-js
.
You'd want to use this only if you're very conscious about package sizes, and this reduced feature set (only analytics and feature flags) works for your use case. The most common use case is in chrome extensions.
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.identify('my-unique-user-id', { $set: { email: 'exampke@posthog.com', name: 'Jane Doe' }, $set_once: { vip: true } })
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()