posthog-reference-architecture
1
总安装量
1
周安装量
#54229
全站排名
安装命令
npx skills add https://github.com/jeremylongshore/claude-code-plugins-plus-skills --skill posthog-reference-architecture
Agent 安装分布
mcpjam
1
claude-code
1
kilo
1
junie
1
windsurf
1
zencoder
1
Skill 文档
PostHog Reference Architecture
Overview
Production-ready architecture patterns for PostHog integrations.
Prerequisites
- Understanding of layered architecture
- PostHog SDK knowledge
- TypeScript project setup
- Testing framework configured
Project Structure
my-posthog-project/
âââ src/
â âââ posthog/
â â âââ client.ts # Singleton client wrapper
â â âââ config.ts # Environment configuration
â â âââ types.ts # TypeScript types
â â âââ errors.ts # Custom error classes
â â âââ handlers/
â â âââ webhooks.ts # Webhook handlers
â â âââ events.ts # Event processing
â âââ services/
â â âââ posthog/
â â âââ index.ts # Service facade
â â âââ sync.ts # Data synchronization
â â âââ cache.ts # Caching layer
â âââ api/
â â âââ posthog/
â â âââ webhook.ts # Webhook endpoint
â âââ jobs/
â âââ posthog/
â âââ sync.ts # Background sync job
âââ tests/
â âââ unit/
â â âââ posthog/
â âââ integration/
â âââ posthog/
âââ config/
â âââ posthog.development.json
â âââ posthog.staging.json
â âââ posthog.production.json
âââ docs/
âââ posthog/
âââ SETUP.md
âââ RUNBOOK.md
Layer Architecture
âââââââââââââââââââââââââââââââââââââââââââ
â API Layer â
â (Controllers, Routes, Webhooks) â
âââââââââââââââââââââââââââââââââââââââââââ¤
â Service Layer â
â (Business Logic, Orchestration) â
âââââââââââââââââââââââââââââââââââââââââââ¤
â PostHog Layer â
â (Client, Types, Error Handling) â
âââââââââââââââââââââââââââââââââââââââââââ¤
â Infrastructure Layer â
â (Cache, Queue, Monitoring) â
âââââââââââââââââââââââââââââââââââââââââââ
Key Components
Step 1: Client Wrapper
// src/posthog/client.ts
export class PostHogService {
private client: PostHogClient;
private cache: Cache;
private monitor: Monitor;
constructor(config: PostHogConfig) {
this.client = new PostHogClient(config);
this.cache = new Cache(config.cacheOptions);
this.monitor = new Monitor('posthog');
}
async get(id: string): Promise<Resource> {
return this.cache.getOrFetch(id, () =>
this.monitor.track('get', () => this.client.get(id))
);
}
}
Step 2: Error Boundary
// src/posthog/errors.ts
export class PostHogServiceError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly retryable: boolean,
public readonly originalError?: Error
) {
super(message);
this.name = 'PostHogServiceError';
}
}
export function wrapPostHogError(error: unknown): PostHogServiceError {
// Transform SDK errors to application errors
}
Step 3: Health Check
// src/posthog/health.ts
export async function checkPostHogHealth(): Promise<HealthStatus> {
try {
const start = Date.now();
await posthogClient.ping();
return {
status: 'healthy',
latencyMs: Date.now() - start,
};
} catch (error) {
return { status: 'unhealthy', error: error.message };
}
}
Data Flow Diagram
User Request
â
â¼
âââââââââââââââ
â API â
â Gateway â
ââââââââ¬âââââââ
â
â¼
âââââââââââââââ âââââââââââââââ
â Service âââââ¶â Cache â
â Layer â â (Redis) â
ââââââââ¬âââââââ âââââââââââââââ
â
â¼
âââââââââââââââ
â PostHog â
â Client â
ââââââââ¬âââââââ
â
â¼
âââââââââââââââ
â PostHog â
â API â
âââââââââââââââ
Configuration Management
// config/posthog.ts
export interface PostHogConfig {
apiKey: string;
environment: 'development' | 'staging' | 'production';
timeout: number;
retries: number;
cache: {
enabled: boolean;
ttlSeconds: number;
};
}
export function loadPostHogConfig(): PostHogConfig {
const env = process.env.NODE_ENV || 'development';
return require(`./posthog.${env}.json`);
}
Instructions
Step 1: Create Directory Structure
Set up the project layout following the reference structure above.
Step 2: Implement Client Wrapper
Create the singleton client with caching and monitoring.
Step 3: Add Error Handling
Implement custom error classes for PostHog operations.
Step 4: Configure Health Checks
Add health check endpoint for PostHog connectivity.
Output
- Structured project layout
- Client wrapper with caching
- Error boundary implemented
- Health checks configured
Error Handling
| Issue | Cause | Solution |
|---|---|---|
| Circular dependencies | Wrong layering | Separate concerns by layer |
| Config not loading | Wrong paths | Verify config file locations |
| Type errors | Missing types | Add PostHog types |
| Test isolation | Shared state | Use dependency injection |
Examples
Quick Setup Script
# Create reference structure
mkdir -p src/posthog/{handlers} src/services/posthog src/api/posthog
touch src/posthog/{client,config,types,errors}.ts
touch src/services/posthog/{index,sync,cache}.ts
Resources
Flagship Skills
For multi-environment setup, see posthog-multi-env-setup.