-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaywall_conversion_report.py
More file actions
660 lines (604 loc) · 22.6 KB
/
Copy pathpaywall_conversion_report.py
File metadata and controls
660 lines (604 loc) · 22.6 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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#!/usr/bin/env python3
"""Weekly paywall conversion and non-conversion diagnostics.
``paywall_purchase_attempt`` means native purchase flow start (see
``docs/POSTHOG_ANALYTICS.md`` § Paywall funnel semantics), not arbitrary CTA taps.
"""
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
_SCRIPTS = Path(__file__).resolve().parent
if str(_SCRIPTS) not in sys.path:
sys.path.insert(0, str(_SCRIPTS))
from repo_dotenv import load_repo_dotenv
from store_downloads_snapshot import LIVE_EVENTS_PREDICATE, posthog_query
PLAY_STORE_CATALOG_FILTER = (
"AND coalesce(toString(properties.distribution_channel), 'legacy') IN ('play_store', 'legacy') "
"AND coalesce(toString(properties.billing_ready), 'true') = 'true'"
)
# Include legacy paywall_purchase_result failures (pre-reason instrumentation).
_PURCHASE_FAILURE_EVENTS = (
"('paywall_purchase_fail_reason', 'purchase_failed', 'paywall_purchase_result')"
)
_PURCHASE_FAILURE_RESULT_FILTER = """
AND (
event != 'paywall_purchase_result'
OR (
lower(coalesce(toString(properties.success), '')) != 'true'
AND lower(coalesce(toString(properties.result), '')) NOT IN (
'success', 'restored', 'already_unlocked'
)
)
)
"""
def _safe_int(value: Any) -> int:
try:
return int(value or 0)
except (TypeError, ValueError):
return 0
def _rate(numerator: int, denominator: int) -> float:
if denominator <= 0:
return 0.0
return round(float(numerator) / float(denominator), 4)
def _scalar(query: str, api_key: str, project_id: str, errors: List[str]) -> int:
result = posthog_query(query, api_key, project_id, errors)
if not result or not result.get("results"):
return 0
row = result["results"][0]
if not row:
return 0
return _safe_int(row[0])
def _table(
query: str,
api_key: str,
project_id: str,
errors: List[str],
) -> list[list[Any]]:
result = posthog_query(query, api_key, project_id, errors)
rows = (result or {}).get("results")
if not isinstance(rows, list):
return []
return rows
def _funnel_counts(api_key: str, project_id: str, days: int, errors: List[str]) -> Dict[str, int]:
win = f"{days} day"
views = _scalar(
f"""
SELECT count()
FROM events
WHERE event IN ('paywall_view', 'paywall_viewed')
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
""",
api_key,
project_id,
errors,
)
offer_selects = _scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_offer_select'
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
""",
api_key,
project_id,
errors,
)
purchase_attempts = _scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_attempt'
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
""",
api_key,
project_id,
errors,
)
purchase_successes = _scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_success'
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
""",
api_key,
project_id,
errors,
)
return {
"views": views,
"offer_selects": offer_selects,
"purchase_attempts": purchase_attempts,
"purchase_successes": purchase_successes,
}
def _failure_reasons(api_key: str, project_id: str, days: int, errors: List[str]) -> list[dict[str, Any]]:
win = f"{days} day"
rows = _table(
f"""
SELECT
coalesce(
toString(coalesce(properties.reason, properties.result, 'unknown')),
'unknown'
) AS reason,
count() AS failures
FROM events
WHERE event IN {_PURCHASE_FAILURE_EVENTS}
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
{_PURCHASE_FAILURE_RESULT_FILTER}
GROUP BY reason
ORDER BY failures DESC
LIMIT 12
/* top_failure_reasons */
""",
api_key,
project_id,
errors,
)
return [{"reason": str(row[0] or "unknown"), "count": _safe_int(row[1])} for row in rows]
def _failure_breakdown(api_key: str, project_id: str, days: int, errors: List[str]) -> list[dict[str, Any]]:
win = f"{days} day"
rows = _table(
f"""
SELECT
coalesce(toString(properties.platform), 'unknown') AS platform,
coalesce(toString(properties.product_id), 'unknown') AS product_id,
coalesce(toString(coalesce(properties.reason, properties.result, 'unknown')), 'unknown') AS reason,
count() AS failures,
count(DISTINCT person_id) AS users
FROM events
WHERE event IN {_PURCHASE_FAILURE_EVENTS}
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
{_PURCHASE_FAILURE_RESULT_FILTER}
GROUP BY platform, product_id, reason
ORDER BY failures DESC
LIMIT 25
/* failure_breakdown */
""",
api_key,
project_id,
errors,
)
return [
{
"platform": str(row[0] or "unknown"),
"product_id": str(row[1] or "unknown"),
"reason": str(row[2] or "unknown"),
"failures": _safe_int(row[3]),
"users": _safe_int(row[4]),
}
for row in rows
]
def _product_funnel(api_key: str, project_id: str, days: int, errors: List[str]) -> list[dict[str, Any]]:
win = f"{days} day"
rows = _table(
f"""
SELECT
coalesce(toString(properties.platform), 'unknown') AS platform,
coalesce(toString(properties.product_id), 'unknown') AS product_id,
countIf(event = 'paywall_offer_select') AS offer_selects,
countIf(event = 'paywall_purchase_attempt') AS attempts,
countIf(event = 'paywall_purchase_success') AS successes
FROM events
WHERE event IN ('paywall_offer_select', 'paywall_purchase_attempt', 'paywall_purchase_success')
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
GROUP BY platform, product_id
ORDER BY attempts DESC, offer_selects DESC
LIMIT 25
/* product_funnel */
""",
api_key,
project_id,
errors,
)
out: list[dict[str, Any]] = []
for row in rows:
offer_selects = _safe_int(row[2])
attempts = _safe_int(row[3])
successes = _safe_int(row[4])
out.append(
{
"platform": str(row[0] or "unknown"),
"product_id": str(row[1] or "unknown"),
"offer_selects": offer_selects,
"attempts": attempts,
"successes": successes,
"select_to_attempt_rate": _rate(attempts, offer_selects),
"attempt_to_success_rate": _rate(successes, attempts),
}
)
return out
def _product_catalog_failures(
api_key: str,
project_id: str,
days: int,
errors: List[str],
*,
play_store_only: bool = False,
) -> list[dict[str, Any]]:
win = f"{days} day"
channel_filter = PLAY_STORE_CATALOG_FILTER if play_store_only else ""
query_tag = "product_catalog_failures_play_store" if play_store_only else "product_catalog_failures"
rows = _table(
f"""
SELECT
coalesce(toString(properties.platform), 'unknown') AS platform,
coalesce(toString(properties.product_id), 'unknown') AS product_id,
count() AS failures,
count(DISTINCT person_id) AS users
FROM events
WHERE event = 'billing_product_not_found'
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
{channel_filter}
GROUP BY platform, product_id
ORDER BY failures DESC
LIMIT 25
/* {query_tag} */
""",
api_key,
project_id,
errors,
)
return [
{
"platform": str(row[0] or "unknown"),
"product_id": str(row[1] or "unknown"),
"failures": _safe_int(row[2]),
"users": _safe_int(row[3]),
}
for row in rows
]
def _entry_point_funnel(api_key: str, project_id: str, days: int, errors: List[str]) -> list[dict[str, Any]]:
win = f"{days} day"
rows = _table(
f"""
SELECT
coalesce(toString(properties.entry_point), 'unknown') AS entry_point,
countIf(event IN ('paywall_view', 'paywall_viewed')) AS views,
countIf(event = 'paywall_purchase_attempt') AS attempts,
countIf(event = 'paywall_purchase_success') AS successes
FROM events
WHERE event IN ('paywall_view', 'paywall_viewed', 'paywall_purchase_attempt', 'paywall_purchase_success')
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
GROUP BY entry_point
ORDER BY views DESC
LIMIT 15
/* entry_point_funnel */
""",
api_key,
project_id,
errors,
)
out: list[dict[str, Any]] = []
for row in rows:
views = _safe_int(row[1])
attempts = _safe_int(row[2])
successes = _safe_int(row[3])
out.append(
{
"entry_point": str(row[0] or "unknown"),
"views": views,
"attempts": attempts,
"successes": successes,
"view_to_attempt_rate": _rate(attempts, views),
"attempt_to_success_rate": _rate(successes, attempts),
}
)
return out
def _settings_hotspots(api_key: str, project_id: str, days: int, errors: List[str]) -> list[dict[str, Any]]:
win = f"{days} day"
rows = _table(
f"""
SELECT
coalesce(toString(properties.setting_name), 'unknown') AS setting_name,
count() AS changes,
count(DISTINCT person_id) AS users
FROM events
WHERE event = 'settings_changed'
AND timestamp > now() - interval {win}
AND {LIVE_EVENTS_PREDICATE}
GROUP BY setting_name
ORDER BY changes DESC
LIMIT 15
/* settings_hotspots */
""",
api_key,
project_id,
errors,
)
return [
{
"setting_name": str(row[0] or "unknown"),
"changes": _safe_int(row[1]),
"users": _safe_int(row[2]),
}
for row in rows
]
def _leaky_entry_points(entry_points: list[dict[str, Any]]) -> list[dict[str, Any]]:
return [
row
for row in entry_points
if _safe_int(row.get("views")) >= 20 and _safe_int(row.get("attempts")) == 0
]
def _data_quality_warnings(
counts: dict[str, int],
entry_points: list[dict[str, Any]],
settings_hotspots: list[dict[str, Any]],
failure_reasons: list[dict[str, Any]],
product_catalog_failures: list[dict[str, Any]],
) -> list[str]:
warnings: list[str] = []
offer_selects = _safe_int(counts.get("offer_selects"))
attempts = _safe_int(counts.get("purchase_attempts"))
views = _safe_int(counts.get("views"))
if attempts > offer_selects and offer_selects > 0:
warnings.append(
"purchase_attempts exceed offer_selects; paywall funnel events are inconsistent and need instrumentation review"
)
if attempts > views and views > 0:
warnings.append(
"purchase_attempts exceed paywall views; entry or purchase attempt events are being emitted without matching impressions"
)
if any(str(row.get("entry_point")) == "unknown" and _safe_int(row.get("views")) >= 20 for row in entry_points):
warnings.append("unknown paywall entry_point is still receiving meaningful traffic")
if settings_hotspots:
top = settings_hotspots[0]
if str(top.get("setting_name")) == "unknown" and _safe_int(top.get("changes")) >= 100:
warnings.append("settings_changed is still dominated by unknown setting_name rows in live data")
failure_total = sum(_safe_int(row.get("count")) for row in failure_reasons)
user_cancelled = sum(
_safe_int(row.get("count"))
for row in failure_reasons
if str(row.get("reason")) == "user_cancelled"
)
if failure_total > 0 and _rate(user_cancelled, failure_total) >= 0.75:
warnings.append(
"purchase failures are dominated by user_cancelled; prioritize pricing, plan default, and purchase-sheet value proof before assuming a store outage"
)
catalog_failure_total = sum(_safe_int(row.get("failures")) for row in product_catalog_failures)
if catalog_failure_total > 0:
warnings.append(
"product catalog lookup failures detected; verify App Store Connect and Google Play product IDs, approval state, and cleared-for-sale status"
)
return warnings
def build_markdown(payload: Dict[str, Any]) -> str:
funnel = payload.get("funnel", {})
lines = [
"# Paywall Conversion Report",
"",
f"Generated: {payload.get('generated_at', '')}",
f"Window (days): {payload.get('window_days', 30)}",
"",
"## Funnel",
f"- Views: **{funnel.get('views', 0)}**",
f"- Offer Selects: **{funnel.get('offer_selects', 0)}**",
f"- Purchase Attempts: **{funnel.get('purchase_attempts', 0)}**",
f"- Purchase Successes: **{funnel.get('purchase_successes', 0)}**",
f"- View -> Offer Select: **{funnel.get('view_to_select_rate', 0):.1%}**",
f"- Select -> Purchase Attempt: **{funnel.get('select_to_attempt_rate', 0):.1%}**",
f"- Attempt -> Purchase Success: **{funnel.get('attempt_to_success_rate', 0):.1%}**",
"",
"## Top Failure Reasons",
"| Reason | Count |",
"|--------|-------|",
]
for row in payload.get("top_failure_reasons", []):
lines.append(f"| {row.get('reason', 'unknown')} | {row.get('count', 0)} |")
if not payload.get("top_failure_reasons"):
lines.append("| (none) | 0 |")
lines.extend(
[
"",
"## Failure Breakdown",
"| Platform | Product ID | Reason | Failures | Users |",
"|----------|------------|--------|----------|-------|",
]
)
for row in payload.get("failure_breakdown", []):
lines.append(
f"| {row.get('platform', 'unknown')} | {row.get('product_id', 'unknown')} | "
f"{row.get('reason', 'unknown')} | {row.get('failures', 0)} | {row.get('users', 0)} |"
)
if not payload.get("failure_breakdown"):
lines.append("| (none) | (none) | (none) | 0 | 0 |")
lines.extend(
[
"",
"## Product Funnel",
"| Platform | Product ID | Selects | Attempts | Successes | Select->Attempt | Attempt->Success |",
"|----------|------------|---------|----------|-----------|-----------------|------------------|",
]
)
for row in payload.get("product_funnel", []):
lines.append(
f"| {row.get('platform', 'unknown')} | {row.get('product_id', 'unknown')} | "
f"{row.get('offer_selects', 0)} | {row.get('attempts', 0)} | {row.get('successes', 0)} | "
f"{row.get('select_to_attempt_rate', 0):.1%} | {row.get('attempt_to_success_rate', 0):.1%} |"
)
if not payload.get("product_funnel"):
lines.append("| (none) | (none) | 0 | 0 | 0 | 0.0% | 0.0% |")
lines.extend(
[
"",
"## Product Catalog Failures",
"| Platform | Product ID | Failures | Users |",
"|----------|------------|----------|-------|",
]
)
for row in payload.get("product_catalog_failures", []):
lines.append(
f"| {row.get('platform', 'unknown')} | {row.get('product_id', 'unknown')} | "
f"{row.get('failures', 0)} | {row.get('users', 0)} |"
)
if not payload.get("product_catalog_failures"):
lines.append("| (none) | (none) | 0 | 0 |")
lines.extend(
[
"",
"## Entry Point Funnel",
"| Entry Point | Views | Attempts | Successes | View->Attempt | Attempt->Success |",
"|-------------|-------|----------|-----------|---------------|------------------|",
]
)
for row in payload.get("entry_points", []):
lines.append(
f"| {row.get('entry_point', 'unknown')} | {row.get('views', 0)} | "
f"{row.get('attempts', 0)} | {row.get('successes', 0)} | "
f"{row.get('view_to_attempt_rate', 0):.1%} | {row.get('attempt_to_success_rate', 0):.1%} |"
)
if not payload.get("entry_points"):
lines.append("| (none) | 0 | 0 | 0 | 0.0% | 0.0% |")
lines.extend(
[
"",
"## Leaky Entry Points",
]
)
for row in payload.get("leaky_entry_points", []):
lines.append(
f"- `{row.get('entry_point', 'unknown')}` had **{row.get('views', 0)}** views and "
f"**0** purchase attempts."
)
if not payload.get("leaky_entry_points"):
lines.append("- None")
lines.extend(
[
"",
"## Settings Hotspots",
"| Setting | Changes | Users |",
"|---------|---------|-------|",
]
)
for row in payload.get("settings_hotspots", []):
lines.append(
f"| {row.get('setting_name', 'unknown')} | {row.get('changes', 0)} | {row.get('users', 0)} |"
)
if not payload.get("settings_hotspots"):
lines.append("| (none) | 0 | 0 |")
lines.extend(
[
"",
"## Data Quality Warnings",
]
)
for warning in payload.get("data_quality_warnings", []):
lines.append(f"- {warning}")
if not payload.get("data_quality_warnings"):
lines.append("- None")
if payload.get("query_errors"):
lines.extend(
[
"",
"## Query Diagnostics",
f"- Query errors: **{len(payload.get('query_errors', []))}**",
f"- Last error: `{payload['query_errors'][-1]}`",
]
)
return "\n".join(lines) + "\n"
def run(repo_root: Path, days: int = 30) -> Dict[str, Any]:
load_repo_dotenv(repo_root)
output_dir = repo_root / "marketing" / "data"
output_dir.mkdir(parents=True, exist_ok=True)
json_path = output_dir / "paywall_conversion_report.json"
md_path = output_dir / "paywall_conversion_report.md"
generated_at = dt.datetime.now(dt.timezone.utc).replace(microsecond=0).isoformat()
api_key = (
os.getenv("POSTHOG_PERSONAL_API_KEY", "").strip()
or os.getenv("POSTHOG_API_KEY", "").strip()
or os.getenv("posthog_api_key", "").strip()
)
project_id = os.getenv("POSTHOG_PROJECT_ID", "").strip()
errors: list[str] = []
payload: Dict[str, Any] = {
"generated_at": generated_at,
"window_days": int(days),
"status": "ok",
"reason": "",
"funnel": {
"views": 0,
"offer_selects": 0,
"purchase_attempts": 0,
"purchase_successes": 0,
"view_to_select_rate": 0.0,
"select_to_attempt_rate": 0.0,
"attempt_to_success_rate": 0.0,
},
"top_failure_reasons": [],
"failure_breakdown": [],
"product_funnel": [],
"product_catalog_failures": [],
"product_catalog_failures_play_store": [],
"entry_points": [],
"leaky_entry_points": [],
"settings_hotspots": [],
"data_quality_warnings": [],
"query_errors": [],
}
if not api_key or not project_id:
payload["status"] = "skipped"
payload["reason"] = "missing POSTHOG_PERSONAL_API_KEY/POSTHOG_API_KEY or POSTHOG_PROJECT_ID"
md = build_markdown(payload)
json_path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
md_path.write_text(md, encoding="utf-8")
return payload
counts = _funnel_counts(api_key, project_id, days, errors)
failures = _failure_reasons(api_key, project_id, days, errors)
failure_breakdown = _failure_breakdown(api_key, project_id, days, errors)
product_funnel = _product_funnel(api_key, project_id, days, errors)
product_catalog_failures = _product_catalog_failures(api_key, project_id, days, errors)
product_catalog_failures_play_store = _product_catalog_failures(
api_key, project_id, days, errors, play_store_only=True
)
entry_points = _entry_point_funnel(api_key, project_id, days, errors)
settings_hotspots = _settings_hotspots(api_key, project_id, days, errors)
payload["funnel"] = {
**counts,
"view_to_select_rate": _rate(counts["offer_selects"], counts["views"]),
"select_to_attempt_rate": _rate(counts["purchase_attempts"], counts["offer_selects"]),
"attempt_to_success_rate": _rate(counts["purchase_successes"], counts["purchase_attempts"]),
}
payload["top_failure_reasons"] = failures
payload["failure_breakdown"] = failure_breakdown
payload["product_funnel"] = product_funnel
payload["product_catalog_failures"] = product_catalog_failures
payload["product_catalog_failures_play_store"] = product_catalog_failures_play_store
payload["entry_points"] = entry_points
payload["leaky_entry_points"] = _leaky_entry_points(entry_points)
payload["settings_hotspots"] = settings_hotspots
payload["data_quality_warnings"] = _data_quality_warnings(
payload["funnel"],
entry_points,
settings_hotspots,
failures,
product_catalog_failures_play_store or product_catalog_failures,
)
payload["query_errors"] = errors
if errors:
payload["status"] = "degraded"
payload["reason"] = "one or more PostHog queries failed"
md = build_markdown(payload)
json_path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
md_path.write_text(md, encoding="utf-8")
return payload
def main() -> int:
parser = argparse.ArgumentParser(description="Generate paywall conversion report")
parser.add_argument("--repo-root", default=".", help="Repository root")
parser.add_argument("--days", type=int, default=30, help="Lookback window")
args = parser.parse_args()
result = run(Path(args.repo_root).resolve(), days=args.days)
print(json.dumps(result, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())