Optimize PostHog costs through tier selection, sampling, and usage monitoring. Use when analyzing PostHog billing, reducing API costs, or implementing usage monitoring and budget alerts. Trigger with...
Measure the live billable volume for each enabled PostHog product, trace changes to instrumentation releases, and reduce only traffic that the product owner has classified as noise or safely sampleable. Pricing, free allowances, and product entitlements are live facts; read them from the billing dashboard, pricing calculator, and approved product limits at execution time.
Do not copy prices or free allowances into implementation code. Record the billing-dashboard timestamp, current product limit, projected usage, unit price from the live calculator, and the owner who approved any data-loss tradeoff. Recheck those values before applying a change.
Use Read to inspect the relevant configuration and implementation before proposing changes. Use Grep to locate initialization, capture, flag, and credential boundaries.
set -euo pipefail
# See which events consume the most quota (last 30 days)
curl "https://us.posthog.com/api/projects/$POSTHOG_PROJECT_ID/query/" \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": {
"kind": "HogQLQuery",
"query": "SELECT event, count() AS total FROM events WHERE timestamp > now() - interval 30 day GROUP BY event ORDER BY total DESC LIMIT 20"
}
}' | jq '.results[] | {event: .[0], count: .[1]}'
$autocapture is often the largest event volume. Restrict it to only useful interactions.
import posthog from 'posthog-js';
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: 'https://us.i.posthog.com',
autocapture: {
// Only capture click and submit events (skip change, scroll, etc.)
dom_event_allowlist: ['click', 'submit'],
// Only capture meaningful elements
element_allowlist: ['a', 'button', 'form', 'input[type=submit]'],
// Only capture elements with this CSS class
css_selector_allowlist: ['.track-click', '[data-track]'],
// Skip internal/admin pages entirely
url_ignorelist: ['/admin', '/health', '/api/internal', '/_next'],
},
});
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: 'https://us.i.posthog.com',
before_send: (event) => {
// 1. Always send business-critical events (no sampling)
const critical = ['purchase', 'signup', 'subscription_started', 'subscription_canceled', 'error'];
if (critical.includes(event.event)) return event;
// 2. Drop bot traffic entirely
const ua = navigator?.userAgent?.toLowerCase() || '';
if (/bot|crawler|spider|scrapy|headless|phantom|puppeteer/i.test(ua)) {
return null;
}
// 3. Apply only the sample rate approved for this event class.
if (event.event === '$pageview') {
const highTraffic = ['/', '/pricing', '/blog'];
const url = event.properties?.$current_url || '';
if (highTraffic.some(p => url.endsWith(p))) {
return Math.random() < approvedPageviewSampleRate ? event : null;
}
return event;
}
// 4. Use a separately approved rate for autocapture.
if (event.event === '$autocapture') {
return Math.random() < approvedAutocaptureSampleRate ? event : null;
}
return event; // Keep all other events
},
});
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: 'https://us.i.posthog.com',
session_recording: {
// Load the reviewed rate from configuration; document analysis impact.
sampleRate: approvedSessionRecordingSampleRate,
// Don't record sessions shorter than 5 seconds (bounces)
minimumDurationMilliseconds: 5000,
// Record 100% of sessions with errors (most valuable for debugging)
// This is configured in PostHog dashboard under Session Replay settings
},
});
// Alternatively: disable recording for non-paying users
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
api_host: 'https://us.i.posthog.com',
disable_session_recording: true, // Start disabled
});
// Enable only for paying users
if (user.plan !== 'free') {
posthog.startSessionRecording();
}
set -euo pipefail
# Check current month's event usage
curl "https://us.posthog.com/api/projects/$POSTHOG_PROJECT_ID/query/" \
-H "Authorization: Bearer $POSTHOG_PERSONAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"query": {
"kind": "HogQLQuery",
"query": "SELECT count() AS total_events, uniq(distinct_id) AS unique_users, count() / dateDiff('"'"'day'"'"', toStartOfMonth(now()), now()) AS events_per_day FROM events WHERE timestamp > toStartOfMonth(now())"
}
}' | jq '.results[0] | {
total_events: .[0],
unique_users: .[1],
avg_events_per_day: .[2],
projected_monthly: (.[2] * 30)
}'
// Automated cost monitoring
async function checkPostHogBudget() {
const result = await queryPostHog(`
SELECT count() AS events_this_month
FROM events
WHERE timestamp > toStartOfMonth(now())
`);
const eventsThisMonth = result.results[0][0];
const approvedMonthlyLimit = Number(process.env.POSTHOG_MONTHLY_EVENT_LIMIT);
if (!Number.isFinite(approvedMonthlyLimit)) {
throw new Error('Set POSTHOG_MONTHLY_EVENT_LIMIT from the current billing configuration');
}
const projectedMonthly = eventsThisMonth / (new Date().getDate() / 30);
if (projectedMonthly > approvedMonthlyLimit * 0.8) {
await sendSlackAlert(`PostHog usage alert: ${Math.round(projectedMonthly)} events projected against reviewed limit ${approvedMonthlyLimit}`);
}
}
For each proposed filter or sample, record the baseline count, affected event or product, chosen rate, expected analytical limitation, rollback trigger, and measured result after one full review window. Never report generic savings estimates as if they were measured for the target project.
| Issue | Cause | Solution |
|---|---|---|
| Event volume spike | New feature without volume estimate | Forecast events before launch |
| Bill higher than expected | Bot traffic | Add bot filtering in before_send |
| Missing critical events | Sampling too aggressive | Exclude revenue events from sampling |
| Approved product limit reached mid-month | Volume exceeds the reviewed budget | Apply the least destructive approved control and preserve conversion events |
For an autocapture spike, compare billing-dashboard volume with the release timeline, preserve named conversion events, narrow noisy autocapture, and set a reviewed product limit. Report expected data loss explicitly; do not invent savings percentages.
See official PostHog references for current authority and verification boundaries.