Contents

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

 1npm i -s posthog-js-lite
 2# or
 3yarn add posthog-js-lite

It is entirely written in Typescript and has a minimal API as follows:

 1import PostHog from 'posthog-js-lite'
 2
 3const posthog = new PostHog('my-api-key', {
 4  /* options, e.g. for self-hosted users */
 5  // host: "https://my-posthog.app.com"
 6})
 7
 8// Capture generic events
 9posthog.capture('my-event', { myProperty: 'foo' })
10
11// Identify a user (e.g. on login)
12posthog.identify('my-unique-user-id', { email: 'example@posthog.com', name: 'Jane Doe' })
13// ...or with Set Once additional properties
14posthog.identify('my-unique-user-id', { $set: { email: 'example@posthog.com', name: 'Jane Doe' }, $set_once: { vip: true } })
15
16// Reset a user (e.g. on logout)
17posthog.reset()
18
19// Register properties to be sent with all subsequent events
20posthog.register({ itemsInBasket: 3 })
21// ...or get rid of them if you don't want them anymore
22posthog.unregister('itemsInBasket')
23
24// Add the user to a group
25posthog.group('organisations', 'org-1')
26// ...or multiple groups at once
27posthog.group({ organisations: 'org-1', project: 'project-1' })
28
29// Simple feature flags
30if (posthog.isFeatureEnabled('my-feature-flag')) {
31  renderFlaggedFunctionality()
32} else {
33  renderDefaultFunctionality()
34}
35
36// Multivariate feature flags
37if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'variant1') {
38  renderVariant1()
39} else if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'variant2') {
40  renderVariant1()
41} else if (posthog.getFeatureFlag('my-feature-flag-with-variants') === 'control') {
42  renderControl()
43}
44
45// Override a feature flag for a specific user (e.g. for testing or user preference)
46posthog.overrideFeatureFlag('my-feature-flag', true)
47
48// Listen reactively to feature flag changes
49posthog.onFeatureFlag('my-feature-flag', (value) => {
50  respondToFeatureFlagChange(value)
51})
52
53// Opt users in or out, persisting across sessions (default is they are opted in)
54posthog.optOut() // Will stop tracking
55posthog.optIn() // Will start tracking

History API Navigation Tracking

Single-page applications (SPAs) typically use the History API (pushState, replaceState) for navigation instead of full page loads. By default, PostHog only tracks the initial page load.

To automatically track navigation events in SPAs, enable the captureHistoryEvents option:

 1const posthog = new PostHog('my-api-key', {
 2  captureHistoryEvents: true
 3})

When enabled, PostHog will:

  • Track calls to history.pushState() and history.replaceState()
  • Track popstate events (browser back/forward navigation)
  • Send these as $pageview events with the current URL and pathname
  • Include the navigation type (pushState, replaceState, or popstate) as a property

This ensures accurate page tracking in modern web applications without requiring manual pageview capture calls.