-
Notifications
You must be signed in to change notification settings - Fork 0
fix: bugs in example apps #101
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,29 @@ | ||
| """PostHog client initialization using the documented Posthog() constructor. | ||
| Creates a shared client instance that routes and middleware import from. | ||
| @see https://posthog.com/docs/libraries/python | ||
| """ | ||
|
|
||
| from posthog import Posthog | ||
|
|
||
| from app.config import get_settings | ||
|
|
||
| settings = get_settings() | ||
|
|
||
| # Initialize PostHog using the documented constructor pattern. | ||
| # This creates a client instance that all routes and middleware share. | ||
| # @see https://posthog.com/docs/libraries/python | ||
| if not settings.posthog_disabled: | ||
| posthog = Posthog( | ||
| settings.posthog_project_token, | ||
| host=settings.posthog_host, | ||
| debug=settings.debug, | ||
| enable_exception_autocapture=True, | ||
| ) | ||
| else: | ||
| # Create a disabled client so imports don't break | ||
| posthog = Posthog( | ||
| "disabled", | ||
| host=settings.posthog_host, | ||
| disabled=True, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,11 @@ | |
| from pathlib import Path | ||
| from typing import Annotated | ||
|
|
||
| import posthog | ||
| from fastapi import APIRouter, Cookie, Depends, Form, Request | ||
| from fastapi.responses import HTMLResponse, RedirectResponse | ||
| from fastapi.templating import Jinja2Templates | ||
| from posthog import capture | ||
|
|
||
| from app.posthog_client import posthog | ||
|
|
||
| from app.dependencies import ( | ||
| CurrentUser, | ||
|
|
@@ -47,13 +47,13 @@ async def login( | |
|
|
||
| if user: | ||
| is_new_user = user.record_login(db) | ||
| posthog.identify(user.email, {"email": user.email, "is_staff": user.is_staff}) | ||
| posthog.capture( | ||
| user.email, | ||
| "user_logged_in", | ||
| distinct_id=str(user.id), | ||
| properties={ | ||
| "username": user.email, | ||
| "is_new_user": is_new_user, | ||
| "$set": {"email": user.email, "is_staff": user.is_staff}, | ||
| "$set_once": {"first_login_date": user.date_joined.isoformat()}, | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -112,13 +112,13 @@ async def signup( | |
| # Create new user | ||
| user = User.create_user(db, email=email, password=password, is_staff=False) | ||
|
|
||
| posthog.identify(user.email, {"email": user.email, "is_staff": user.is_staff}) | ||
| posthog.capture( | ||
| user.email, | ||
| "user_signed_up", | ||
| distinct_id=str(user.id), | ||
|
Collaborator
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. We should be setting context no?
Collaborator
Author
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. @gewenyu99 could you clarify what you mean by context here?
Collaborator
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.
using this pattern instead of explicitly passing in distinct ID. Basically like if you imagine a route controller: func handle_request(req, res):
{distinct_id, session_id} = parse(req.headers)
Identify_context(...):
handle() # actual logicI believe this is the preferred logic. |
||
| properties={ | ||
| "username": user.email, | ||
| "signup_method": "form", | ||
| "$set": {"email": user.email, "is_staff": user.is_staff}, | ||
| "$set_once": {"first_login_date": user.date_joined.isoformat()}, | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -136,7 +136,7 @@ async def signup( | |
| @router.get("/logout") | ||
| async def logout(current_user: RequiredUser): | ||
| """Logout and capture event.""" | ||
| capture("user_logged_out") | ||
| posthog.capture("user_logged_out", distinct_id=str(current_user.id)) | ||
|
|
||
| response = RedirectResponse(url="/", status_code=302) | ||
| response.delete_cookie(key="session_token") | ||
|
|
@@ -149,12 +149,12 @@ async def dashboard( | |
| current_user: RequiredUser, | ||
| ): | ||
| """Dashboard with feature flag demonstration.""" | ||
| capture("dashboard_viewed", properties={"is_staff": current_user.is_staff}) | ||
| posthog.capture("dashboard_viewed", distinct_id=str(current_user.id), properties={"is_staff": current_user.is_staff}) | ||
|
|
||
| # Check feature flag | ||
| show_new_feature = posthog.feature_enabled( | ||
| "new-dashboard-feature", | ||
| current_user.email, | ||
| str(current_user.id), | ||
| person_properties={ | ||
| "email": current_user.email, | ||
| "is_staff": current_user.is_staff, | ||
|
|
@@ -163,7 +163,7 @@ async def dashboard( | |
|
|
||
| # Get feature flag payload | ||
| feature_config = posthog.get_feature_flag_payload( | ||
| "new-dashboard-feature", current_user.email | ||
| "new-dashboard-feature", str(current_user.id) | ||
| ) | ||
|
|
||
| return templates.TemplateResponse( | ||
|
|
@@ -194,7 +194,7 @@ async def burrito( | |
| @router.get("/profile", response_class=HTMLResponse) | ||
| async def profile(request: Request, current_user: RequiredUser): | ||
| """User profile page.""" | ||
| capture("profile_viewed") | ||
| posthog.capture("profile_viewed", distinct_id=str(current_user.id)) | ||
|
|
||
| return templates.TemplateResponse( | ||
| request, "profile.html", {"current_user": current_user} | ||
|
|
@@ -212,10 +212,10 @@ async def update_profile( | |
| fields_changed = current_user.update_profile(db, name=name) | ||
|
|
||
| if fields_changed: | ||
| capture( | ||
| posthog.capture( | ||
| "profile_updated", | ||
| distinct_id=str(current_user.id), | ||
| properties={ | ||
| "username": current_user.email, | ||
| "fields_changed": fields_changed, | ||
| }, | ||
| ) | ||
|
|
||
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.
Is this new? Used to no be like this yah?
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.
the old code was actually off. it should use the shared
posthog_client.pyusing thePosthog()constructor as documentedhttps://posthog.com/docs/libraries/python#installation