Execute PostHog primary workflow: Core Workflow A.
Use when implementing primary use case,
building main features, or core integration tasks.
Trigger with phrases like "posthog main workflow",
"primary task with posthog".
Primary PostHog workflow covering event capture, user identification, group analytics, and person properties. This is the foundation for all PostHog analytics: capturing what users do, linking events to identified users, and grouping users by company/team for B2B analytics.
// After user logs in — links anonymous events to this user
posthog.identify('user-456', {
// $set properties (persist, overwrite on change)
email: '[email protected]',
name: 'Jane Smith',
plan: 'enterprise',
signup_date: '2025-06-15',
});
// Update properties later without re-identifying
posthog.people.set({
last_active: new Date().toISOString(),
total_items: 42,
});
// Set properties only if not already set ($set_once)
posthog.people.set_once({
first_seen: new Date().toISOString(),
original_referrer: document.referrer,
});
// Unset properties
posthog.people.unset(['deprecated_field']);
// Reset on logout (clears distinct_id, starts new anonymous session)
posthog.reset();
Step 4: Group Analytics (B2B Company Tracking)
// Associate user with a company group
posthog.group('company', 'company-789', {
name: 'Acme Corp',
industry: 'SaaS',
plan: 'enterprise',
employee_count: 150,
arr: 250000,
});
// Events now automatically include company context
posthog.capture(EVENTS.FEATURE_USED, {
feature_name: 'bulk-export',
});
// This event is attributed to both user-456 AND company-789
// Multiple group types
posthog.group('team', 'team-alpha', { name: 'Alpha Team' });
Step 5: Server-Side Event Capture (posthog-node)
import { PostHog } from 'posthog-node';
const posthog = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
host: 'https://us.i.posthog.com',
});
// Server-side capture (e.g., in API route or webhook handler)
function trackServerEvent(userId: string, event: string, properties?: Record<string, any>) {
posthog.capture({
distinctId: userId,
event,
properties: {
...properties,
source: 'api',
},
});
}
// Identify with server-side properties
posthog.identify({
distinctId: 'user-456',
properties: {
subscription_status: 'active',
mrr: 99,
},
});
// Group identify from server
posthog.groupIdentify({
groupType: 'company',
groupKey: 'company-789',
properties: {
plan: 'enterprise',
total_seats: 50,
},
});
// CRITICAL: Flush in serverless/edge functions
await posthog.flush();
Step 6: Create Annotations for Context
set -euo pipefail
# Mark a deployment or product change in PostHog
curl -X POST "https://app.posthog.com/api/projects/$POSTHOG_PROJECT_ID/annotations/" \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"content": "v2.5.0 deployed — new checkout flow",
"date_marker": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'",
"scope": "project"
}'
Error Handling
Error
Cause
Solution
Events not appearing
posthog.init not called
Ensure init runs before any capture
Anonymous/identified split
Different distinct_id across platforms
Use consistent user ID from your auth system
Group data missing
posthog.group() not called
Call group() before capture for group attribution
Server events lost
No flush() in serverless
Always call await posthog.flush() before response
Properties not updating
Using $set_once for mutable data
Use posthog.people.set() for values that change
Output
Typed event taxonomy for consistent tracking
Browser event capture with user identification
B2B group analytics linking users to companies
Server-side event capture with proper flushing
Annotations marking deployments and product changes