What is posthog-js?
The posthog-js npm package is a JavaScript library for integrating PostHog analytics into web applications. It allows you to track user interactions, capture events, and analyze user behavior in real-time.
What are posthog-js's main functionalities?
Initialization
Initialize the PostHog library with your API key and optional configuration settings.
const posthog = require('posthog-js');
posthog.init('YOUR_API_KEY', { api_host: 'https://app.posthog.com' });
Capture Events
Capture custom events with properties to track user interactions and behaviors.
posthog.capture('event_name', { property1: 'value1', property2: 'value2' });
Identify Users
Identify users with unique IDs and associate them with properties like email and name.
posthog.identify('user_id', { email: 'user@example.com', name: 'John Doe' });
Set User Properties
Set properties for identified users to enrich user profiles with additional information.
posthog.people.set({ property1: 'value1', property2: 'value2' });
Feature Flags
Check if a feature flag is enabled for the current user to implement feature toggling.
const isEnabled = posthog.isFeatureEnabled('feature_flag_key');
Other packages similar to posthog-js
mixpanel-browser
Mixpanel is a powerful analytics tool that offers similar functionalities to PostHog, such as event tracking, user identification, and property setting. Mixpanel also provides advanced features like A/B testing and user segmentation.
amplitude-js
Amplitude is an analytics platform focused on product intelligence. It offers event tracking, user identification, and behavioral analytics. Amplitude provides advanced features like cohort analysis and user journey mapping, which can be more detailed than PostHog's offerings.
PostHog.js
Please see the main PostHog docs.
Specifically, the JS integration details.
Testing
Run yarn test
Releasing a new version
To release a new version, make sure you're logged in to NPM (npm login
)
We tend to follow the following steps:
- Merge your changes into master
- Release changes as a beta version
npm version 1.x.x-beta.0
npm publish --tag beta
git push --tags
- Create a PR linking to this version in the main repo
- Once deployed and tested, write up CHANGELOG.md, and commit.
- Release a new version
npm version 1.x.x
npm publish
git push --tags
- Create a PR linking to this version in the main repo