tessl install tessl/npm-posthog-js@1.335.0PostHog Browser JS Library is a comprehensive browser analytics and feature management SDK that enables developers to capture user events, track product analytics, manage feature flags, record session replays, and implement feedback mechanisms like surveys and conversations in web applications.
Get started with PostHog.js in minutes.
npm install posthog-jsimport posthog from 'posthog-js';
posthog.init('<your-project-api-key>', {
api_host: 'https://us.i.posthog.com',
loaded: (posthog) => {
console.log('PostHog loaded!');
}
});// Capture custom events
posthog.capture('button_clicked', {
button_id: 'signup',
location: 'header'
});// After user logs in
posthog.identify('user-123', {
email: 'user@example.com',
name: 'John Doe',
plan: 'pro'
});// Check if feature is enabled
if (posthog.isFeatureEnabled('new-dashboard')) {
showNewDashboard();
} else {
showOldDashboard();
}// Enable session recording
posthog.startSessionRecording();import { useEffect } from 'react';
import posthog from 'posthog-js';
function App() {
useEffect(() => {
posthog.init('your-api-key', {
api_host: 'https://us.i.posthog.com',
capture_pageview: 'history_change'
});
}, []);
return <YourApp />;
}// On signup
function handleSignup(userId: string, email: string) {
posthog.alias(userId); // Link anonymous events
posthog.identify(userId, { email });
}
// On logout
function handleLogout() {
posthog.reset(); // Clear user state
}// Wait for flags to load
posthog.onFeatureFlags((flags) => {
if (flags['new-feature']) {
enableNewFeature();
}
});posthog.init('your-api-key', {
// API endpoint
api_host: 'https://us.i.posthog.com', // or 'https://eu.i.posthog.com'
// Event capture
autocapture: true,
capture_pageview: 'history_change',
// Privacy
opt_out_capturing_by_default: false,
// Session recording
session_recording: {
enabled: true,
maskAllInputs: true
},
// Person profiles
person_profiles: 'identified_only'
});// Enable debug mode
posthog.debug(true);
// Check status
console.log('Is capturing:', posthog.is_capturing());
console.log('Config:', posthog.config);// Check if flags loaded
console.log('Flags loaded:', posthog.featureFlags.hasLoadedFlags);
// Force reload
posthog.reloadFeatureFlags();posthog.debug(true)