-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathposthog_dashboard.py
More file actions
535 lines (457 loc) · 17.8 KB
/
Copy pathposthog_dashboard.py
File metadata and controls
535 lines (457 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#!/usr/bin/env python3
"""Comprehensive PostHog analytics dashboard.
Queries PostHog HogQL API to build a full-funnel dashboard covering:
- Install funnel with conversion rates (7d and 30d)
- Engagement metrics (WQTU, DAU, avg completions)
- Feature popularity breakdowns
- Revenue metrics
- Retention (daily timer_completed, new vs returning)
Outputs JSON to stdout. Designed for CI (env vars for auth) or local (.env).
Usage:
python scripts/posthog_dashboard.py --days 30
python scripts/posthog_dashboard.py --days 7 --json-stdout
"""
from __future__ import annotations
import argparse
import datetime as dt
import json
import os
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional
SCRIPTS = Path(__file__).resolve().parent
REPO_ROOT = SCRIPTS.parent
sys.path.insert(0, str(SCRIPTS))
from repo_dotenv import load_repo_dotenv
POSTHOG_HOST = "https://us.posthog.com"
# Audience filter: non-debug, real device, not internal
PRAGMATIC_LIVE = """(
lower(coalesce(properties.build_type, 'release')) != 'debug'
AND lower(coalesce(properties.runtime_target, 'device')) NOT IN ('simulator', 'emulator')
AND coalesce(toString(properties.is_internal), 'false') != 'true'
AND coalesce(toString(properties.distribution_channel), 'legacy') NOT IN (
'testflight', 'non_play_install', 'dev', 'emulator', 'simulator', 'ui_test'
)
)"""
def _posthog_credentials() -> tuple[str, str]:
key = (
os.environ.get("POSTHOG_PERSONAL_API_KEY", "").strip()
or os.environ.get("POSTHOG_API_KEY", "").strip()
or os.environ.get("posthog_api_key", "").strip()
)
project_id = os.environ.get("POSTHOG_PROJECT_ID", "").strip()
return key, project_id
def _requests_module():
try:
import requests
return requests
except ImportError:
return None
def posthog_query(
sql: str, api_key: str, project_id: str, errors: List[str]
) -> Optional[Dict[str, Any]]:
"""Execute a HogQL query and return JSON payload."""
requests = _requests_module()
if requests is None:
errors.append("missing_dependency: requests")
return None
try:
response = requests.post(
f"{POSTHOG_HOST}/api/projects/{project_id}/query/",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
json={"query": {"kind": "HogQLQuery", "query": sql}},
timeout=30,
)
except requests.RequestException as exc:
errors.append(f"request_error: {exc}")
return None
if response.status_code >= 300:
errors.append(f"http_{response.status_code}: {response.text[:200]}")
return None
try:
return response.json()
except Exception as exc:
errors.append(f"json_parse_error: {exc}")
return None
def _scalar(
sql: str, api_key: str, project_id: str, errors: List[str]
) -> Optional[int]:
data = posthog_query(sql, api_key, project_id, errors)
if not data or not data.get("results"):
return None
row = data["results"][0]
if not row:
return None
try:
return int(row[0] or 0)
except (TypeError, ValueError):
return None
def _scalar_float(
sql: str, api_key: str, project_id: str, errors: List[str]
) -> Optional[float]:
data = posthog_query(sql, api_key, project_id, errors)
if not data or not data.get("results"):
return None
row = data["results"][0]
if not row:
return None
try:
return round(float(row[0] or 0), 2)
except (TypeError, ValueError):
return None
def _rows(
sql: str, api_key: str, project_id: str, errors: List[str]
) -> List[List[Any]]:
data = posthog_query(sql, api_key, project_id, errors)
if not data or not data.get("results"):
return []
return data["results"]
# ---------------------------------------------------------------------------
# Section builders
# ---------------------------------------------------------------------------
def _funnel_section(
api_key: str, project_id: str, errors: List[str]
) -> Dict[str, Any]:
"""Install funnel with conversion rates for 7d and 30d windows."""
f = PRAGMATIC_LIVE
funnel_events = [
("Application Installed", "application_installed"),
("first_open", "first_open"),
("first_timer_configured", "first_timer_configured"),
("timer_completed", "timer_completed"),
("paywall_viewed", "paywall_viewed"),
("paywall_purchase_success", "paywall_purchase_success"),
]
result: Dict[str, Any] = {}
for window_label, window_days in [("7d", 7), ("30d", 30)]:
win = f"{window_days} day"
steps = []
prev_count: Optional[int] = None
first_count: Optional[int] = None
for event_name, key in funnel_events:
count = _scalar(
f"SELECT count(DISTINCT person_id) FROM events "
f"WHERE event = '{event_name}' "
f"AND timestamp > now() - interval {win} AND {f}",
api_key, project_id, errors,
)
step: Dict[str, Any] = {
"event": event_name,
"distinct_users": count,
}
if first_count is not None and first_count > 0 and count is not None:
step["overall_conversion_pct"] = round(count / first_count * 100, 2)
if prev_count is not None and prev_count > 0 and count is not None:
step["step_conversion_pct"] = round(count / prev_count * 100, 2)
steps.append(step)
if first_count is None:
first_count = count
prev_count = count
result[window_label] = {"window_days": window_days, "steps": steps}
return result
def _engagement_section(
api_key: str, project_id: str, days: int, errors: List[str]
) -> Dict[str, Any]:
"""WQTU, DAU, avg timer_completed per user per week."""
f = PRAGMATIC_LIVE
# WQTU: distinct users with >=3 timer_completed in trailing 7 days
wqtu = _scalar(
f"SELECT count() FROM ("
f" SELECT person_id, count() AS c FROM events "
f" WHERE event = 'timer_completed' "
f" AND timestamp > now() - interval 7 day AND {f} "
f" GROUP BY person_id HAVING c >= 3"
f")",
api_key, project_id, errors,
)
# DAU: distinct users with any event today
dau = _scalar(
f"SELECT count(DISTINCT person_id) FROM events "
f"WHERE timestamp > now() - interval 1 day AND {f}",
api_key, project_id, errors,
)
# Average timer_completed per user per week (over the window)
avg_completions_per_user_week = _scalar_float(
f"SELECT avg(c) FROM ("
f" SELECT person_id, count() AS c FROM events "
f" WHERE event = 'timer_completed' "
f" AND timestamp > now() - interval 7 day AND {f} "
f" GROUP BY person_id"
f")",
api_key, project_id, errors,
)
# Weekly active users (any event in trailing 7d)
wau = _scalar(
f"SELECT count(DISTINCT person_id) FROM events "
f"WHERE timestamp > now() - interval 7 day AND {f}",
api_key, project_id, errors,
)
return {
"wqtu_7d": wqtu,
"dau": dau,
"wau_7d": wau,
"avg_timer_completed_per_user_7d": avg_completions_per_user_week,
"window_days": days,
}
def _feature_popularity_section(
api_key: str, project_id: str, days: int, errors: List[str]
) -> Dict[str, Any]:
"""Feature popularity breakdowns."""
f = PRAGMATIC_LIVE
win = f"{days} day"
# voice_gender_selected breakdown
voice_gender = _rows(
f"SELECT coalesce(toString(properties.gender), '(unknown)'), "
f"count(), count(DISTINCT person_id) FROM events "
f"WHERE event = 'voice_gender_selected' "
f"AND timestamp > now() - interval {win} AND {f} "
f"GROUP BY 1 ORDER BY 2 DESC LIMIT 10",
api_key, project_id, errors,
)
# feature_gate_hit breakdown
feature_gates = _rows(
f"SELECT coalesce(toString(properties.feature), toString(properties.`$feature_flag`), '(unknown)'), "
f"count(), count(DISTINCT person_id) FROM events "
f"WHERE event = 'feature_gate_hit' "
f"AND timestamp > now() - interval {win} AND {f} "
f"GROUP BY 1 ORDER BY 2 DESC LIMIT 20",
api_key, project_id, errors,
)
# settings_changed breakdown
settings_changed = _rows(
f"SELECT coalesce(toString(properties.setting), toString(properties.changed_property), '(unknown)'), "
f"count(), count(DISTINCT person_id) FROM events "
f"WHERE event = 'settings_changed' "
f"AND timestamp > now() - interval {win} AND {f} "
f"GROUP BY 1 ORDER BY 2 DESC LIMIT 20",
api_key, project_id, errors,
)
# Most common timer range (min/max)
timer_ranges = _rows(
f"SELECT "
f" coalesce(toString(properties.min_seconds), '?'), "
f" coalesce(toString(properties.max_seconds), '?'), "
f" count(), count(DISTINCT person_id) "
f"FROM events "
f"WHERE event IN ('timer_started', 'first_timer_configured') "
f"AND timestamp > now() - interval {win} AND {f} "
f"GROUP BY 1, 2 ORDER BY 3 DESC LIMIT 10",
api_key, project_id, errors,
)
return {
"voice_gender_selected": [
{"gender": r[0], "events": r[1], "distinct_users": r[2]}
for r in voice_gender
],
"feature_gate_hit": [
{"feature": r[0], "events": r[1], "distinct_users": r[2]}
for r in feature_gates
],
"settings_changed": [
{"setting": r[0], "events": r[1], "distinct_users": r[2]}
for r in settings_changed
],
"top_timer_ranges": [
{"min_seconds": r[0], "max_seconds": r[1], "events": r[2], "distinct_users": r[3]}
for r in timer_ranges
],
}
def _revenue_section(
api_key: str, project_id: str, days: int, errors: List[str]
) -> Dict[str, Any]:
"""Revenue: paywall_purchase_success count/sum, conversion from viewed."""
f = PRAGMATIC_LIVE
win = f"{days} day"
purchase_count = _scalar(
f"SELECT count() FROM events "
f"WHERE event = 'paywall_purchase_success' "
f"AND timestamp > now() - interval {win} AND {f}",
api_key, project_id, errors,
)
purchase_distinct = _scalar(
f"SELECT count(DISTINCT person_id) FROM events "
f"WHERE event = 'paywall_purchase_success' "
f"AND timestamp > now() - interval {win} AND {f}",
api_key, project_id, errors,
)
purchase_revenue = _scalar_float(
f"SELECT sum(toFloatOrZero(toString(coalesce(properties.revenue, toString(properties.price), '0')))) "
f"FROM events "
f"WHERE event = 'paywall_purchase_success' "
f"AND timestamp > now() - interval {win} AND {f}",
api_key, project_id, errors,
)
paywall_viewed = _scalar(
f"SELECT count(DISTINCT person_id) FROM events "
f"WHERE event = 'paywall_viewed' "
f"AND timestamp > now() - interval {win} AND {f}",
api_key, project_id, errors,
)
conversion_rate = None
if paywall_viewed and paywall_viewed > 0 and purchase_distinct is not None:
conversion_rate = round(purchase_distinct / paywall_viewed * 100, 2)
return {
"paywall_purchase_success_events": purchase_count,
"paywall_purchase_success_distinct_users": purchase_distinct,
"paywall_revenue_sum": purchase_revenue,
"paywall_viewed_distinct_users": paywall_viewed,
"paywall_conversion_rate_pct": conversion_rate,
"window_days": days,
}
def _retention_section(
api_key: str, project_id: str, errors: List[str]
) -> Dict[str, Any]:
"""Daily timer_completed over last 30 days + new vs returning users."""
f = PRAGMATIC_LIVE
# Daily timer_completed for last 30 days
daily_completions = _rows(
f"SELECT toDate(timestamp) AS day, count(), count(DISTINCT person_id) "
f"FROM events "
f"WHERE event = 'timer_completed' "
f"AND timestamp > now() - interval 30 day AND {f} "
f"GROUP BY day ORDER BY day",
api_key, project_id, errors,
)
# New users (first seen in last 7d) vs returning (first seen before last 7d)
# who had any event in last 7d
new_users_7d = _scalar(
f"SELECT count(DISTINCT e.person_id) FROM events e "
f"INNER JOIN ("
f" SELECT person_id, min(timestamp) AS first_seen FROM events "
f" WHERE {f} GROUP BY person_id"
f") f ON e.person_id = f.person_id "
f"WHERE e.timestamp > now() - interval 7 day AND {f} "
f"AND f.first_seen > now() - interval 7 day",
api_key, project_id, errors,
)
returning_users_7d = _scalar(
f"SELECT count(DISTINCT e.person_id) FROM events e "
f"INNER JOIN ("
f" SELECT person_id, min(timestamp) AS first_seen FROM events "
f" WHERE {f} GROUP BY person_id"
f") f ON e.person_id = f.person_id "
f"WHERE e.timestamp > now() - interval 7 day AND {f} "
f"AND f.first_seen <= now() - interval 7 day",
api_key, project_id, errors,
)
return {
"daily_timer_completed_30d": [
{"date": str(r[0]), "events": r[1], "distinct_users": r[2]}
for r in daily_completions
],
"new_users_7d": new_users_7d,
"returning_users_7d": returning_users_7d,
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def build_dashboard(
days: int = 30,
load_dotenv: bool = True,
) -> Dict[str, Any]:
if load_dotenv:
load_repo_dotenv(REPO_ROOT)
api_key, project_id = _posthog_credentials()
errors: List[str] = []
generated_at = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat()
if not api_key or not project_id:
return {
"generated_at": generated_at,
"status": "skipped",
"reason": "missing POSTHOG_PERSONAL_API_KEY or POSTHOG_PROJECT_ID",
}
dashboard: Dict[str, Any] = {
"generated_at": generated_at,
"status": "ok",
"source": "posthog_dashboard",
"host": POSTHOG_HOST,
"project_id": project_id,
"audience": "pragmatic_live",
"window_days": days,
"funnel": _funnel_section(api_key, project_id, errors),
"engagement": _engagement_section(api_key, project_id, days, errors),
"feature_popularity": _feature_popularity_section(api_key, project_id, days, errors),
"revenue": _revenue_section(api_key, project_id, days, errors),
"retention": _retention_section(api_key, project_id, errors),
}
if errors:
dashboard["query_errors"] = errors[:30]
return dashboard
def main() -> int:
parser = argparse.ArgumentParser(
description="Comprehensive PostHog analytics dashboard"
)
parser.add_argument(
"--days", type=int, default=30,
help="Lookback window in days (default: 30)",
)
parser.add_argument(
"--no-dotenv", action="store_true",
help="Skip loading .env from repo root",
)
parser.add_argument(
"--json-stdout", action="store_true",
help="Print full JSON to stdout (always enabled in CI)",
)
parser.add_argument(
"--output", type=str, default=None,
help="Write JSON to this file path (in addition to stdout)",
)
args = parser.parse_args()
# In CI, always output JSON to stdout
is_ci = os.environ.get("CI", "").lower() in ("true", "1")
json_stdout = args.json_stdout or is_ci
dashboard = build_dashboard(
days=args.days,
load_dotenv=not args.no_dotenv,
)
if args.output:
out_path = Path(args.output)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(dashboard, indent=2) + "\n", encoding="utf-8")
print(f"Dashboard written to {out_path}", file=sys.stderr)
if json_stdout:
print(json.dumps(dashboard, indent=2))
else:
# Human-readable summary
print("=" * 60)
print(" POSTHOG ANALYTICS DASHBOARD")
print("=" * 60)
print(f" Status: {dashboard.get('status')}")
print(f" Window: {dashboard.get('window_days')} days")
print(f" Generated: {dashboard.get('generated_at')}")
print()
eng = dashboard.get("engagement") or {}
print(f" WQTU (7d): {eng.get('wqtu_7d')}")
print(f" DAU: {eng.get('dau')}")
print(f" WAU (7d): {eng.get('wau_7d')}")
print(f" Avg completions/user/week: {eng.get('avg_timer_completed_per_user_7d')}")
print()
rev = dashboard.get("revenue") or {}
print(f" Purchases: {rev.get('paywall_purchase_success_events')}")
print(f" Revenue sum: {rev.get('paywall_revenue_sum')}")
print(f" Paywall conv %: {rev.get('paywall_conversion_rate_pct')}")
print()
funnel_30 = (dashboard.get("funnel") or {}).get("30d", {})
steps = funnel_30.get("steps") or []
if steps:
print(" FUNNEL (30d):")
for s in steps:
conv = s.get("step_conversion_pct", "-")
print(f" {s['event']:40s} users={s.get('distinct_users')} step_conv={conv}%")
print()
ret = dashboard.get("retention") or {}
print(f" New users (7d): {ret.get('new_users_7d')}")
print(f" Returning users (7d): {ret.get('returning_users_7d')}")
errs = dashboard.get("query_errors") or []
if errs:
print(f"\n Errors ({len(errs)}):")
for e in errs[:5]:
print(f" - {e}")
print("=" * 60)
return 0
if __name__ == "__main__":
raise SystemExit(main())