-
Notifications
You must be signed in to change notification settings - Fork 0
Posthog analytics #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
82b19ce
57aefbb
da0f24c
b61961d
3851a40
7a5571b
953ff9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from posthog import Posthog | ||
| from .config import settings | ||
|
|
||
| # Initialize PostHog client | ||
| ph_client = Posthog( | ||
| settings.POSTHOG_API_KEY, | ||
| host=settings.POSTHOG_HOST, | ||
| # debug=True # Helpful for debugging delivery issues | ||
| ) | ||
|
|
||
| # Gracefully disable if no API key is provided | ||
| if not settings.POSTHOG_API_KEY: | ||
| print("⚠️ [POSTHOG] Disabled: No API Key found in settings") | ||
| ph_client.disabled = True | ||
| else: | ||
| # print(f"✅ [POSTHOG] Initialized with Key: {settings.POSTHOG_API_KEY[:8]}***") | ||
| print(f"✅ [POSTHOG] Initialized") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from ..schemas.resource import ResourceOut, ResourceUpdate | ||
| from ..routers.auth import get_current_user | ||
| from ..services.storage import storage_service | ||
| from ..analytics import ph_client | ||
| from arq import create_pool | ||
| from ..config import settings | ||
| from arq.connections import RedisSettings | ||
|
|
@@ -94,6 +95,13 @@ async def upload_resource( | |
| detail="Failed to upload file to storage" | ||
| ) | ||
|
|
||
| ph_client.capture("resource_upload_started", distinct_id=str(current_user.id), properties={ | ||
| "filename": file.filename, | ||
| "content_type": file.content_type, | ||
| "size_mb": total_size / (1024 * 1024), | ||
| "type": type | ||
| }) | ||
|
|
||
|
Comment on lines
+98
to
+104
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Show the relevant portion of the file with line numbers
sed -n '70,140p' backend/app/routers/resources.py | nl -ba -w3 -s': '
# Also find where the upload/try/cleanup block is defined in this file
rg -n "try:|except:|finally:|cleanup|rollback|delete|remove|capture\\(" backend/app/routers/resources.py
# Show the exact ph_client usage context (a bit wider)
rg -n "ph_client\\.capture" -n backend/app/routers/resources.pyRepository: shubhamxdd/pyq-appl Length of output: 106 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "=== resources.py (lines ~1-220) with line numbers) ==="
# Use cat -n instead of nl (nl is missing in the sandbox)
sed -n '1,220p' backend/app/routers/resources.py | cat -n -w3 -s': '
echo "=== Search for upload/try/cleanup/rollback and capture usage ==="
rg -n "ph_client\\.capture|try:|except:|finally:|cleanup|rollback|delete|remove|filesystem|storage|upload" backend/app/routers/resources.pyRepository: shubhamxdd/pyq-appl Length of output: 202 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "=== resources.py: lines 60-140 (with line numbers) ==="
sed -n '60,140p' backend/app/routers/resources.py | cat -n
echo "=== Exact ph_client.capture call contexts ==="
rg -n "ph_client\.capture" backend/app/routers/resources.py
echo "=== Surrounding try/except/finally around capture (context) ==="
# Print a window around the capture line(s)
CAPTURE_LINE="$(rg -n "ph_client\.capture" backend/app/routers/resources.py | head -n1 | cut -d: -f1 || true)"
if [ -n "${CAPTURE_LINE}" ]; then
START=$((CAPTURE_LINE-30))
END=$((CAPTURE_LINE+30))
echo "CAPTURE_LINE=${CAPTURE_LINE}; showing ${START}-${END}"
sed -n "${START},${END}p" backend/app/routers/resources.py | cat -n
fiRepository: shubhamxdd/pyq-appl Length of output: 6809 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "=== resources.py: lines 90-170 (capture + surrounding try/except) ==="
sed -n '90,170p' backend/app/routers/resources.py | cat -n
echo "=== resources.py: find storage cleanup/delete/remove calls in this area ==="
rg -n "delete|remove|cleanup|rollback|finally" backend/app/routers/resources.py | head -n 80Repository: shubhamxdd/pyq-appl Length of output: 4139 Move
🤖 Prompt for AI Agents |
||
| try: | ||
| # Create DB record (not committed yet) | ||
| new_resource = Resource( | ||
|
|
@@ -187,6 +195,8 @@ async def delete_resource( | |
| await db.delete(resource) | ||
| await db.commit() | ||
|
|
||
| ph_client.capture("resource_deleted", distinct_id=str(current_user.id), properties={"resource_id": str(resource_id)}) | ||
|
|
||
| return {"message": "Resource deleted successfully"} | ||
|
|
||
| @router.patch("/{resource_id}", response_model=ResourceOut) | ||
|
|
@@ -246,6 +256,8 @@ async def retry_extraction( | |
| # Only commit if enqueue was successful | ||
| await db.commit() | ||
| await db.refresh(resource) | ||
|
|
||
| ph_client.capture("resource_retry_extraction", distinct_id=str(current_user.id), properties={"resource_id": str(resource_id)}) | ||
| except Exception as e: | ||
| await db.rollback() | ||
| raise HTTPException( | ||
|
|
@@ -278,5 +290,6 @@ async def stop_processing( | |
| resource.status = "failed" | ||
| await db.commit() | ||
| await db.refresh(resource) | ||
| ph_client.capture("resource_stop_processing", distinct_id=str(current_user.id), properties={"resource_id": str(resource_id)}) | ||
|
|
||
| return resource | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,3 +19,4 @@ python-dotenv | |
| pypdfium2 | ||
| pillow | ||
| email-validator | ||
| posthog | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| VITE_API_URL=http://127.0.0.1:8001/api | ||
| VITE_POSTHOG_KEY= | ||
| VITE_POSTHOG_HOST=https://us.i.posthog.com |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: shubhamxdd/pyq-appl
Length of output: 106
🏁 Script executed:
Repository: shubhamxdd/pyq-appl
Length of output: 2374
🏁 Script executed:
Repository: shubhamxdd/pyq-appl
Length of output: 1449
🏁 Script executed:
Repository: shubhamxdd/pyq-appl
Length of output: 1449
🌐 Web query:
PostHog Python Posthog debug parameter what does debug=True do logs payloads💡 Result:
In the PostHog Python SDK, setting
debug=Trueenables verbose logging, which helps troubleshoot issues with event capturing, feature flags, or survey rendering [1]. When enabled, the SDK changes the logging level of theposthoglogger toDEBUG[2]. This causes the library to output internal operational logs to the standard logging output [1][2]. Specifically, enablingdebug=Trueperforms the following actions: - It configures the logger by callinglogging.basicConfig()[2]. - It sets the log level for theposthoglogger tologging.DEBUG(instead of the defaultlogging.WARNING) [2]. - This allows you to see detailed information about the SDK's internal processes [1], which can include information regarding the processing and sending of payloads, helping you verify if events or configurations are being handled as expected [1]. You can enable this mode during client initialization or by setting the attribute directly on the client instance:python # During initialization from posthog import Posthog posthog = Posthog('<ph_project_token>', debug=True) # Or by setting the attribute posthog.debug = TrueIf you are not seeing expected data appear in your PostHog instance, this mode is the recommended way to inspect the SDK's activity and identify why events might not be reaching the server [1].Citations:
Disable PostHog debug mode by default.
debug=Trueinbackend/app/analytics.pyturns on the PostHog SDK’s verbose logging (sets theposthoglogger toDEBUG), which can cause sensitive event/payload processing details to end up in server logs. DefaultdebugtoFalseand gate it behind an explicit settings/env flag (the currentbackend/app/config.pyhasPOSTHOG_API_KEY/POSTHOG_HOSTonly, with no debug toggle).🤖 Prompt for AI Agents