Writing Custom Funnel and Cohort SQL Queries on Raw Event Data
Pre-built analytics dashboards like Google Analytics or Mixpanel are great for quick summaries, but they lock you into predefined schemas and charge hefty premium fees for custom funnel queries. Once your raw mobile events land in a SQL warehouse you control — exported from your analytics vendor on a schedule — you can write precise query scripts to unlock granular customer lifecycle insights.
1. Calculating multi-step conversion funnel drop-offs
To analyze drop-offs in your onboarding funnel, you need to count how many users completed each subsequent action. We can achieve this in SQL using CTEs (Common Table Expressions) and conditional aggregation:
WITH user_steps AS (
SELECT
user_id,
MAX(CASE WHEN event_name = 'app_installed' THEN 1 ELSE 0 END) as step_1,
MAX(CASE WHEN event_name = 'onboarding_completed' THEN 1 ELSE 0 END) as step_2,
MAX(CASE WHEN event_name = 'paywall_presented' THEN 1 ELSE 0 END) as step_3
FROM telemetry_events
GROUP BY user_id
)
SELECT
SUM(step_1) as installed,
SUM(step_2) as onboarding_completed,
SUM(step_3) as paywall_presented,
ROUND(100.0 * SUM(step_2) / SUM(step_1), 2) as onboarding_conversion_pct,
ROUND(100.0 * SUM(step_3) / SUM(step_2), 2) as paywall_view_conversion_pct
FROM user_steps;
2. Generating N-Day Retention Cohorts
Retention cohorts track if users return to your app over subsequent days or weeks. By comparing the registration date with event dates, we can calculate daily cohort retention:
WITH cohorts AS (
SELECT
user_id,
DATE_TRUNC('day', created_at) as cohort_date
FROM users
),
activity AS (
SELECT
user_id,
DATE_TRUNC('day', timestamp) as activity_date
FROM telemetry_events
)
SELECT
c.cohort_date,
COUNT(DISTINCT c.user_id) as cohort_size,
COUNT(DISTINCT CASE WHEN a.activity_date = c.cohort_date + INTERVAL '1 day' THEN a.user_id END) as day_1_retained,
COUNT(DISTINCT CASE WHEN a.activity_date = c.cohort_date + INTERVAL '7 day' THEN a.user_id END) as day_7_retained
FROM cohorts c
LEFT JOIN activity a ON c.user_id = a.user_id
GROUP BY c.cohort_date
ORDER BY c.cohort_date DESC;
Optimizing SQL performance at scale
Running window functions and table joins over millions of telemetry logs can be slow. To optimize performance:
- Composite Indexes: Set up composite indexes on
(user_id, event_name, timestamp). This ensures that user action chains are retrieved using fast index scans. - Materialized Views: Set up materialized views to aggregate event counts hourly. A dashboard query then scans pre-aggregated rows instead of the raw event table, so the cost drops roughly in proportion to how much the aggregation collapses — measure it on your own data rather than trusting a headline multiple.
- ClickHouse optimization: In ClickHouse, define your schema using the
ReplacingMergeTreeengine, which deduplicates event payloads automatically during disk mergers.
Get the BillDog newsletter
Practical guides on subscription pricing, paywalls and app growth, about twice a month. No spam.
