-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabandon_rate_report.py
More file actions
executable file
·286 lines (270 loc) · 9.11 KB
/
Copy pathabandon_rate_report.py
File metadata and controls
executable file
·286 lines (270 loc) · 9.11 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
#!/usr/bin/env python3
import os
import json
import sys
from pathlib import Path
# Add scripts directory to path to import store_downloads_snapshot
sys.path.append(str(Path(__file__).parent.resolve()))
try:
from store_downloads_snapshot import LIVE_EVENTS_PREDICATE, query_rows, query_scalar
except ImportError:
print("Error: Could not import PostHog query helpers from store_downloads_snapshot.py")
sys.exit(1)
def run():
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()
if not key or not project_id:
print("Error: Missing PostHog credentials.")
return
errors = []
# 1. Abandon Rate (timer_started vs timer_completed) - Live audience only
started = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'timer_started'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
completed = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'timer_completed'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
unique_users = query_scalar(
f"""
SELECT count(DISTINCT person_id)
FROM events
WHERE event = 'timer_started'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
abandon_rate = 0.0
if started > 0:
abandon_rate = ((started - completed) / started) * 100
# 1b. Real abandon reasons from the timer_abandoned event
abandon_reasons = query_rows(
f"""
SELECT
coalesce(properties.abandon_reason, 'unknown') as reason,
coalesce(properties.abandon_source, 'unknown') as source,
count() as events,
count(DISTINCT person_id) as users
FROM events
WHERE event = 'timer_abandoned'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
GROUP BY reason, source
ORDER BY events DESC
LIMIT 20
""",
key,
project_id,
errors,
)
total_abandoned_events = sum(row[2] for row in abandon_reasons) if abandon_reasons else 0
# 1c. Onboarding funnel (first_open -> first_timer_configured -> first_timer_completed)
first_open = query_scalar(
f"""
SELECT count(DISTINCT person_id) FROM events
WHERE event = 'first_open'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
first_configured = query_scalar(
f"""
SELECT count(DISTINCT person_id) FROM events
WHERE event = 'first_timer_configured'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
first_completed = query_scalar(
f"""
SELECT count(DISTINCT person_id) FROM events
WHERE event = 'first_timer_completed'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
# 2. Most Used Parts (Screen Views) - Live audience only
screens = query_rows(f"""
SELECT properties.$screen_name as screen, count() as count
FROM events
WHERE event = '$screenview'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
GROUP BY screen
ORDER BY count DESC
""", key, project_id, errors)
# 3. Top Events - Live audience only
top_events = query_rows(f"""
SELECT event, count() as count
FROM events
WHERE timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
AND event NOT IN ('$feature_flag_called', '$groupidentify', '$identify', '$screenview', '$pageview')
GROUP BY event
ORDER BY count DESC
LIMIT 10
""", key, project_id, errors)
# 4. Monetization funnel (live audience only)
paywall_viewed = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_viewed'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
canonical_purchase_attempts = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_attempt'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
canonical_purchase_success = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_success'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
purchase_metrics_source = "canonical_events"
paywall_purchase_attempts = canonical_purchase_attempts
paywall_purchase_success = canonical_purchase_success
if canonical_purchase_attempts == 0 and canonical_purchase_success == 0:
purchase_metrics_source = "paywall_purchase_result_fallback"
paywall_purchase_attempts = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_result'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
paywall_purchase_success = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_purchase_result'
AND (
lower(coalesce(properties.result, '')) IN ('success', 'restored', 'already_unlocked')
OR lower(toString(properties.success)) = 'true'
)
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
paywall_restore_events = query_scalar(
f"""
SELECT count()
FROM events
WHERE event = 'paywall_restore_result'
AND timestamp > now() - interval 30 day
AND {LIVE_EVENTS_PREDICATE}
""",
key,
project_id,
errors,
)
# 5. Build segmentation sanity check (all events, last 30d)
build_audience_breakdown = query_rows(
"""
SELECT lower(coalesce(properties.build_audience, '(missing)')) AS build_audience, count() AS events
FROM events
WHERE timestamp > now() - interval 30 day
GROUP BY build_audience
ORDER BY events DESC
""",
key,
project_id,
errors,
)
report = {
"abandon_metrics": {
"timer_started_30d": started,
"timer_completed_30d": completed,
"unique_started_users_30d": unique_users,
"inferred_abandon_rate_percent": round(abandon_rate, 2),
"timer_abandoned_events_30d": total_abandoned_events,
"abandon_reasons": [
{"reason": row[0], "source": row[1], "events": row[2], "users": row[3]}
for row in abandon_reasons
],
},
"onboarding_funnel_30d": {
"first_open_users": first_open,
"first_timer_configured_users": first_configured,
"first_timer_completed_users": first_completed,
"open_to_configured_pct": round((first_configured / first_open) * 100, 2) if first_open > 0 else 0.0,
"configured_to_completed_pct": round((first_completed / first_configured) * 100, 2) if first_configured > 0 else 0.0,
"open_to_completed_pct": round((first_completed / first_open) * 100, 2) if first_open > 0 else 0.0,
},
"monetization_metrics": {
"paywall_viewed_30d": paywall_viewed,
"paywall_purchase_attempts_30d": paywall_purchase_attempts,
"paywall_purchase_success_30d": paywall_purchase_success,
"paywall_restore_events_30d": paywall_restore_events,
"paywall_purchase_metrics_source": purchase_metrics_source,
"paywall_view_to_purchase_rate_percent": round((paywall_purchase_success / paywall_viewed) * 100, 2) if paywall_viewed > 0 else 0.0,
},
"build_audience_breakdown_30d": [{"build_audience": row[0], "events": row[1]} for row in build_audience_breakdown],
"most_used_screens": [{"name": row[0], "count": row[1]} for row in screens if row[0]],
"top_feature_events": [{"event": row[0], "count": row[1]} for row in top_events],
"errors": errors
}
print(json.dumps(report, indent=2))
if __name__ == "__main__":
run()