-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-posthog.py
More file actions
267 lines (212 loc) · 11.6 KB
/
Copy pathtest-posthog.py
File metadata and controls
267 lines (212 loc) · 11.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
"""
PostHog Analytics Test Utility
Run this in your browser console to test if analytics are working.
"""
# Step 1: Check PostHog Configuration
def check_posthog_config():
"""Check if PostHog is properly configured"""
results = {
'api_key_present': False,
'api_key_value': '',
'posthog_loaded': False,
'posthog_initialized': False,
'test_tracking_works': False,
}
# Check in browser console:
print("""
══════════════════════════════════════════════════════════
📊 PostHog Analytics Configuration Test
══════════════════════════════════════════════════════════
Run these commands in your BROWSER CONSOLE (F12):
// 1. Check if API key is set
console.log('API Key:', process.env.NEXT_PUBLIC_POSTHOG_KEY);
// 2. Check if PostHog is loaded
console.log('PostHog loaded:', !!window.posthog);
console.log('PostHog initialized:', window.posthog?.__loaded);
// 3. Check PostHog object
console.log('PostHog object:', window.posthog);
// 4. Send test event
if (window.posthog) {
window.posthog.capture('test_event', {
test: true,
timestamp: new Date().toISOString(),
source: 'manual_test'
});
console.log('✅ Test event sent!');
} else {
console.log('❌ PostHog not loaded');
}
// 5. Check network requests
// Open Network tab, filter by "posthog" or "capture"
// You should see POST requests to app.posthog.com/capture/
══════════════════════════════════════════════════════════
""")
# Step 2: Setup Instructions
def setup_instructions():
print("""
══════════════════════════════════════════════════════════
🔧 SETUP INSTRUCTIONS
══════════════════════════════════════════════════════════
If you see "undefined" or errors, follow these steps:
1. CREATE .env.local FILE:
────────────────────────────────────────────────────
In your project root, create: .env.local
Add these lines:
NEXT_PUBLIC_POSTHOG_KEY=phc_your_actual_key_here
NEXT_PUBLIC_POSTHOG_HOST=https://app.posthog.com
2. GET POSTHOG API KEY:
────────────────────────────────────────────────────
a) Go to: https://posthog.com/signup
b) Create account (free)
c) Create new project
d) Go to: Settings → Project Settings
e) Copy "Project API Key" (starts with phc_)
f) Paste into .env.local file
3. RESTART YOUR SERVER:
────────────────────────────────────────────────────
Stop your dev server (Ctrl+C)
Run: npm run dev
Wait for it to fully start
4. TEST IN BROWSER:
────────────────────────────────────────────────────
a) Open: http://localhost:3000
b) Open DevTools (F12)
c) Go to Network tab
d) Filter by: "posthog"
e) Perform a search in your app
f) You should see POST requests to:
https://app.posthog.com/capture/
Status: 200 OK
5. CHECK POSTHOG DASHBOARD:
────────────────────────────────────────────────────
a) Go to: https://app.posthog.com
b) Click: Activity → Events
c) Enable: Live mode (toggle in top right)
d) Perform search in your app
e) Event should appear within 1-2 seconds
══════════════════════════════════════════════════════════
""")
# Step 3: How to View Data in PostHog
def view_data_instructions():
print("""
══════════════════════════════════════════════════════════
📊 HOW TO SEE USER SEARCHES IN POSTHOG
══════════════════════════════════════════════════════════
METHOD 1: View Live Events
────────────────────────────────────────────────────────
1. Login: https://app.posthog.com
2. Click: Activity → Events (left sidebar)
3. Toggle: "Live" mode (top right)
4. Click on any "research_completed" event
5. Look at "Properties" section:
✅ query: "what user searched"
✅ outputLength: 2500 (characters)
✅ outputCharacters: 2500
✅ duration: 3200 (milliseconds)
✅ model: "gemini-flash"
✅ success: true
✅ resultsCount: 5
METHOD 2: Create Insight for Search Queries
────────────────────────────────────────────────────────
1. Click: "Insights" (left sidebar)
2. Click: "New Insight"
3. Select: Event → "research_completed"
4. Change view to: "Table"
5. Add columns:
- Click "+ Add column"
- Select: Properties → query
- Select: Properties → outputLength
- Select: Properties → duration
- Select: Properties → model
6. Click: "Save" → Name it "User Searches"
Now you have a table showing:
- What each user searched
- How long results were
- How long it took
- Which model was used
METHOD 3: Create Dashboard
────────────────────────────────────────────────────────
1. Click: "Dashboards" (left sidebar)
2. Click: "New Dashboard"
3. Name it: "User Activity Monitor"
4. Click: "Add insight"
5. Add your saved insights from Method 2
6. Arrange tiles as you like
METHOD 4: Export Data
────────────────────────────────────────────────────────
1. Go to any Insight
2. Click: "..." (three dots)
3. Select: "Export" → CSV or JSON
4. Download and analyze in Excel/Python
══════════════════════════════════════════════════════════
""")
# Step 4: Sample Queries
def sample_queries():
print("""
══════════════════════════════════════════════════════════
🔍 SAMPLE POSTHOG QUERIES
══════════════════════════════════════════════════════════
QUERY 1: See all searches today
────────────────────────────────────────────────────────
Event: research_initiated
Filter: timestamp > -1d
Group by: properties.query
QUERY 2: Average result length
────────────────────────────────────────────────────────
Event: research_completed
Property: outputLength
Aggregation: Average
Time period: Last 7 days
QUERY 3: Most popular searches
────────────────────────────────────────────────────────
Event: research_initiated
Group by: properties.query
Sort by: count (descending)
Limit: 10
QUERY 4: Search duration by model
────────────────────────────────────────────────────────
Event: research_completed
Property: duration
Aggregation: Average
Breakdown by: properties.model
QUERY 5: Output length distribution
────────────────────────────────────────────────────────
Event: research_completed
Property: outputLength
Visualization: Histogram
Buckets: 0-1000, 1000-5000, 5000-10000, 10000+
══════════════════════════════════════════════════════════
""")
# Main execution
if __name__ == "__main__":
print("""
╔══════════════════════════════════════════════════════════╗
║ ║
║ PostHog Analytics Testing Guide ║
║ ║
╚══════════════════════════════════════════════════════════╝
""")
check_posthog_config()
setup_instructions()
view_data_instructions()
sample_queries()
print("""
══════════════════════════════════════════════════════════
✅ CHECKLIST
══════════════════════════════════════════════════════════
□ Created .env.local file
□ Added NEXT_PUBLIC_POSTHOG_KEY
□ Added NEXT_PUBLIC_POSTHOG_HOST
□ Restarted dev server
□ Tested in browser (F12 → Network tab)
□ Saw POST requests to posthog.com
□ Checked PostHog dashboard
□ Saw events in Activity → Events
□ Created insights for searches
□ Created dashboard
══════════════════════════════════════════════════════════
Need help? Check:
- POSTHOG_SETUP_GUIDE.md
- ANALYTICS_TRACKING.md
══════════════════════════════════════════════════════════
""")