How to identify and track users on PostHog
Identify and track users in PostHog by implementing the identify() method with unique user IDs and properties, then capture events using capture() methods. This allows you to link anonymous sessions to known users and track their behavior across sessions.
Prerequisites
- PostHog account with project setup
- Basic JavaScript or SDK knowledge
- Website or application with PostHog installed
- Understanding of user identification concepts
Step-by-Step Instructions
Install and Initialize PostHog
npm install posthog-jsInitialize PostHog with your project API key:
import posthog from 'posthog-js'
posthog.init('your-api-key', { api_host: 'https://app.posthog.com' })Identify Users with Unique IDs
identify() method to associate a unique identifier with the current user. Call this method when a user logs in or when you have identifying information:posthog.identify('user123', {
email: 'user@example.com',
name: 'John Doe',
plan: 'premium'
})The first parameter is the unique user ID, and the second parameter contains user properties.
Set User Properties
setPersonProperties() to enrich user profiles with demographic, behavioral, or account information:posthog.setPersonProperties({
company: 'Acme Corp',
role: 'admin',
signup_date: '2026-01-15',
trial_expires: '2026-02-15'
})These properties will be associated with the identified user and available for analysis.
Track Custom Events
capture() method to track behaviors and interactions:posthog.capture('button_clicked', {
button_name: 'signup',
page: 'homepage',
timestamp: new Date().toISOString()
})Include relevant event properties to provide context for analysis and segmentation.
Handle User Sessions and Aliases
alias() to link them:posthog.alias('user123')This connects the current anonymous session with the permanent user ID. Call this immediately after identifying a user for the first time to maintain session continuity.
Verify Tracking in PostHog Dashboard
- User identification events appear with correct IDs
- Custom events show with proper properties
- User properties are updated in person profiles
Go to People to view individual user profiles and verify data accuracy.
Configure Event Tracking for Key Actions
- User registration and login
- Feature usage and interactions
- Purchase or subscription events
- Page views and navigation
Implement these systematically across your application using consistent naming and properties.
Common Issues & Troubleshooting
Users not appearing in PostHog dashboard
Verify your API key is correct and check the browser network tab for failed PostHog requests. Ensure posthog.init() is called before any tracking methods.
Duplicate user profiles being created
Check that you're using consistent user IDs and only calling alias() once per user. Avoid identifying with different IDs for the same user.
Events not showing user properties
Ensure identify() is called before capturing events. User properties are only associated after identification occurs.
Missing event data or properties
Verify event names and properties are strings or simple data types. Check for JavaScript errors that might prevent PostHog calls from executing.