PostHog Product Analytics Specification
Purpose & Scope
This specification defines the implementation of PostHog for frontend product analytics, user behaviour tracking, and session recording for the Digital Lock On PWA.
PostHog will track user interactions within the modal-heavy PWA interface, providing insights into workflow completion rates, user journeys, and UX friction points that cannot be captured through traditional pageview analytics.
Requirements Implemented
- REQ-ANALYTICS-001: Product Analytics and System Monitoring - Frontend analytics requirements
Related Decisions
- [ ] To be created: ADR-XXX: Analytics and Monitoring Tool Selection
Architecture Overview
PostHog JavaScript SDK will be integrated into the React PWA to provide:
- Automatic pageview and click tracking
- Custom event tracking for modal workflows
- User identification and session tracking
- Session recordings for UX analysis
- Feature flags for controlled rollouts
- Offline event queuing with sync on reconnection
Integration Points
Automatic Capture:
- Pageviews (including virtual pageviews for modals)
- Button clicks
- Form interactions
- Link clicks
Custom Events:
- Modal lifecycle (open, complete, abandon)
- Workflow completions (lock-on, handover, protection changes)
- QR code scans
- Error encounters
- Offline/online state changes
Implementation Details
Installation & Configuration
- Install PostHog SDK
- Setup automated monitoring
- Setup custom events
Core Events to Track
Modal Lifecycle Events
These prove the core workflows are functioning:
protection_activated- PWA activatedprotection_authorised- PWA authorisedprotection_deactivated- PWA handed backsession_conflict- User signed in on other devicesession_conflict_handover- User continued on other devicesession_conflict_cancelled- User cancelled signed in on other deviceworksite_enabled- Worksite enabled (Protector locked on)worksite_disabled- Worksite disabled (Protector locked off)worker_qr_locked_on- Worker signed onworker_qr_locked_off- Worker signed off (shift ended)worker_manually_locked_on- Manual lock onworker_manually_locked_off- Manual lock offworker_locked_off_returning- Locked off returning to site (shift not ended)worker_forced_lock_off- Force lock offworker_competencies_selected- Competencies selectedworker_competencies_changed- Competencies updatedworker_under_supervision- Worker is being supervisedvisitor_locked_on- Visitor locked on to siteworker_fatigue_check- Worker locked on for 10+ hrsworker_fatigue_warning- Worker locked on for 11+ hrsworker_fatigue_alert- Worker locked on for 12+ hrsplant_qr_locked_on- Plant lock onplant_qr_locked_off- Plant lock offplant_manually_locked_on- Plant manual lock onplant_manually_locked_off- Plant manual lock offplant_operator_assigned- Operator assigned to Plantplant_mpc_assigned- MPC assigned to Plantplant_operator_reassigned- Plant Operator reassignedplant_mpc_reassigned- Plant MPC reassignedhandover_initiated- Handover startedhandover_completed- Handover succeededhandover_cancelled- Handover failed/cancelledpwa_handover_initiated- PWA Handover startedpwa_handover_completed- PWA Handover succeededpwa_handover_cancelled- Handover failed/cancelledemergency_handover_initiated- Emergency handover startedemergency_handover_completed- Emergency handover successfulemergency_handover_cancelled- Emergency handover failed/cancelledpwa_emergency_handover_initiated- PWA emergency handover startedpwa_emergency_handover_completed- PWA emergency handover successfulpwa_emergency_handover_cancelled- PWA emergency handover failed/cancellederror_occurred- Any error encounteredoffline_mode_entered- Device went offlineoffline_mode_exited- Device back online
Track significant modal opens as virtual pageviews to enable funnel analysis. This allows us to create funnels like:
- Worksite detail page
- Lock-on worker modal
- QR scan
- Worker confirmation
- Worker locked on
Performance Considerations
PostHog SDK adds approximately 50KB gzipped to bundle size. Lazy load PostHog if needed.
Non-blocking event capture:
- Events are captured asynchronously
- No impact on user interactions
- Failed captures fail silently
Offline Event Queuing
PostHog automatically queues events when offline. No additional configuration needed!
How it works:
- Events captured while offline are stored in localStorage
- When connection restored, queued events are sent automatically
- Events include timestamp from when they occurred (not when synced)
Event Naming Conventions
Standard Format
Use snake_case for event names:
typescript
✅ 'worker_locked_on'
❌ 'WorkerLockedOn'
❌ 'worker-locked-on'Use past tense verbs:
typescript
✅ 'modal_opened'
✅ 'handover_completed'
❌ 'open_modal'
❌ 'complete_handover'Include entity type prefix where relevant:
typescript
✅ 'worker_locked_on'
✅ 'plant_locked_off'
✅ 'protection_activated'Keep names concise but descriptive:
typescript
✅ 'handover_initiated'
❌ 'user_clicked_button_to_initiate_handover_process'Property Naming
Use snake_case for property names:
typescript
{
worksite_id: '123',
user_role: 'Site Protector',
duration_seconds: 45
}Use consistent units:
typescript
✅ duration_seconds: 45
✅ duration_ms: 45000
❌ duration: 45 // What unit?Include context properties:
typescript
{
// Always include these where relevant
worksite_id: worksiteId,
pwa_id: pwaId,
user_role: userRole,
// Event-specific properties
method: 'qr_code',
worker_role: 'C'
}Deployment Checklist
Pre-Production
- [ ] PostHog project created
- [ ] API key added to environment variables
- [ ] PostHog SDK installed and initialised
- [ ] User identification implemented (login/logout)
- [ ] Tier 1 critical events implemented
- [ ] Session recording privacy rules configured
- [ ] Testing in staging environment completed
- [ ] Event tracking validated in PostHog dashboard
Production Launch
- [ ] Production API key configured
- [ ] Debug mode disabled for production
- [ ] Session recording enabled
- [ ] Feature flags configured
- [ ] Team access to PostHog dashboard granted
- [ ] Initial dashboards created
- [ ] Documentation shared with team
Post-Launch (Week 1)
- [ ] Validate events are being captured
- [ ] Check event volume against free tier limits
- [ ] Adjust sampling rates if needed
- [ ] Create initial insights and funnels