posthog-performance-tuning
Optimize PostHog API performance with caching, batching, and connection pooling. Use when experiencing slow API responses, implementing caching strategies, or optimizing request throughput for PostHog integrations. Trigger with phrases like "posthog performance", "optimize posthog", "posthog latency", "posthog caching", "posthog slow", "posthog batch".
- License 许可
- MIT
- 最近更新
- 2026-05-26
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- posthog-performance-tuning
- author
- Jeremy Longshore <jeremy@intentsolutions.io>
- license
- MIT
- version
- 1.0.0
- description
- Optimize PostHog API performance with caching, batching, and connection pooling. Use when experiencing slow API responses, implementing caching strategies, or optimizing request throughput for PostHog integrations. Trigger with phrases like "posthog performance", "optimize posthog", "posthog latency", "posthog caching", "posthog slow", "posthog batch".
- allowed-tools
- Read, Write, Edit
- compatible-with
- claude-code, codex, openclaw
03 SKILL.md 正文
PostHog Performance Tuning
Overview
Optimize PostHog event capture, feature flag evaluation, and analytics queries. Focus on client-side batching, local flag evaluation to eliminate network calls, event sampling for high-volume apps, and efficient HogQL queries.
Prerequisites
- PostHog project with API key
posthog-nodeSDK installed- Understanding of PostHog event model
- Feature flags configured (if applicable)
Instructions
Step 1: Configure Optimal Client Batching
import { PostHog } from 'posthog-node';
const posthog = new PostHog(process.env.POSTHOG_API_KEY!, {
host: 'https://us.i.posthog.com',
flushAt: 20, // Batch size before sending (default 20)
flushInterval: 5000, // Max wait time in ms (default 10000) # 5000: 10000: 5 seconds in ms
requestTimeout: 10000, # 10 seconds in ms
maxRetries: 3,
// Disable for tests
...(process.env.NODE_ENV === 'test' && { enable: false }),
});
// Ensure events are sent before process exits
process.on('SIGTERM', async () => {
await posthog.shutdown();
process.exit(0);
});
Step 2: Local Feature Flag Evaluation
// Fetch flag definitions once, evaluate locally (no network per-call)
let flagDefinitions: any = null;
let lastFetch = 0;
const CACHE_TTL = 30000; // 30 seconds # 30000: 30 seconds in ms
async function getFeatureFlag(
flagKey: string,
distinctId: string,
properties?: Record<string, any>
) {
// Refresh definitions periodically
if (!flagDefinitions || Date.now() - lastFetch > CACHE_TTL) {
flagDefinitions = await posthog.getAllFlags(distinctId, {
personProperties: properties,
});
lastFetch = Date.now();
}
return flagDefinitions[flagKey] ?? false;
}
// For boolean flags in hot paths
async function isFeatureEnabled(flagKey: string, userId: string) {
const flags = await posthog.getAllFlags(userId);
return !!flags[flagKey];
}
Step 3: Event Sampling for High-Volume Capture
function shouldSample(eventName: string): boolean {
const sampleRates: Record<string, number> = {
'$pageview': 1.0, // Capture all pageviews
'button_clicked': 1.0, // Capture all clicks
'api_call': 0.1, // Sample 10% of API calls
'scroll_depth': 0.05, // Sample 5% of scroll events
};
const rate = sampleRates[eventName] ?? 0.5;
return Math.random() < rate;
}
function captureWithSampling(
distinctId: string,
event: string,
properties?: Record<string, any>
) {
if (!shouldSample(event)) return;
posthog.capture({
distinctId,
event,
properties: {
...properties,
$sample_rate: getSampleRate(event),
},
});
}
Step 4: Efficient HogQL Queries
async function queryPostHog(hogql: string) {
const response = await fetch(
`https://us.i.posthog.com/api/projects/${process.env.POSTHOG_PROJECT_ID}/query/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}`,
},
body: JSON.stringify({
query: { kind: 'HogQLQuery', query: hogql },
}),
}
);
return response.json();
}
// Optimized: filter early, limit results
const efficientQuery = `
SELECT
properties.$current_url AS url,
count() AS views,
uniq(distinct_id) AS unique_visitors
FROM events
WHERE event = '$pageview'
AND timestamp > now() - interval 7 day
GROUP BY url
ORDER BY views DESC
LIMIT 50
`;
Error Handling
| Issue | Cause | Solution |
|---|---|---|
| Events dropped | Flush not called on exit | Add shutdown hook with posthog.shutdown() |
| Flag evaluation slow | Network call per evaluation | Use getAllFlags with caching |
| High event volume cost | Capturing everything | Implement sampling for noisy events |
| HogQL timeout | Unfiltered full-table scan | Add date filters and LIMIT |
Examples
Feature Flag A/B Test Tracking
async function trackExperiment(userId: string, experimentKey: string) {
const variant = await posthog.getFeatureFlag(experimentKey, userId);
posthog.capture({
distinctId: userId,
event: 'experiment_viewed',
properties: {
experiment: experimentKey,
variant: variant || 'control',
},
});
return variant;
}
Resources
Output
- Configuration files or code changes applied to the project
- Validation report confirming correct implementation
- Summary of changes made and their rationale
04 同仓库更多 Skills
zustand-store-ts
Create Zustand stores with TypeScript, subscribeWithSelector middleware, and proper state/action separation. Use when building React state management, creating global stores, or implementing reactive state patterns with Zustand.
zustand-store-creator
Zustand Store Creator - Auto-activating skill for Frontend Development. Triggers on: zustand store creator, zustand store creator Part of the Frontend Development skill category.
zero-trust-config-helper
Zero Trust Config Helper - Auto-activating skill for Security Advanced. Triggers on: zero trust config helper, zero trust config helper Part of the Security Advanced skill category.
zapier-integration-helper
Zapier Integration Helper - Auto-activating skill for Business Automation. Triggers on: zapier integration helper, zapier integration helper Part of the Business Automation skill category.
zai-cli
Execute z.AI CLI providing vision, search, reader, and GitHub exploration via CLI and MCP. Use when user needs image/video analysis, OCR, UI-to-code conversion, error diagnosis, real-time web search, web page to markdown extraction, or GitHub code exploration. Trigger with phrases like "analyze this image", "search the web for", "read this page", "explore this repo", or "use zai". Requires Z_AI_API_KEY.
yt-research
Research competitor YouTube channels, niches, and trending topics for your content strategy. Use this skill whenever the user says "research channels", "analyze competitors", "find trending topics", "niche analysis", "competitive research", "what are other creators doing", "scrape YouTube channels", or wants to understand the competitive landscape for a specific tool or topic area. Use when working with yt research. Trigger with 'yt', 'research'.