From bd09c280cd93a2dee05aeda8037ed6bf53527fe6 Mon Sep 17 00:00:00 2001 From: "wizard-ci-bot[bot]" <254716194+wizard-ci-bot[bot]@users.noreply.github.com> Date: Tue, 23 Jun 2026 16:00:48 +0000 Subject: [PATCH] wizard-ci: flask/flask3-social-media --- .../skills/integration-flask/.posthog-wizard | 0 .../.claude/skills/integration-flask/SKILL.md | 62 + .../integration-flask/references/1-begin.md | 56 + .../integration-flask/references/2-edit.md | 36 + .../integration-flask/references/3-revise.md | 22 + .../references/4-conclude.md | 57 + .../integration-flask/references/EXAMPLE.md | 1208 +++++++++++++++++ .../integration-flask/references/flask.md | 117 ++ .../references/identify-users.md | 272 ++++ .../flask/flask3-social-media/.gitignore | 1 + .../flask/flask3-social-media/app/__init__.py | 6 + .../flask3-social-media/app/api/tokens.py | 7 +- .../flask3-social-media/app/api/users.py | 4 + .../flask3-social-media/app/auth/routes.py | 19 + .../flask3-social-media/app/main/routes.py | 38 + .../flask/flask3-social-media/config.py | 2 + .../posthog-setup-report.md | 41 + .../flask3-social-media/requirements.txt | 1 + 18 files changed, 1948 insertions(+), 1 deletion(-) create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/.posthog-wizard create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/SKILL.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/1-begin.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/2-edit.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/3-revise.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/4-conclude.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/EXAMPLE.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/flask.md create mode 100644 apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/identify-users.md create mode 100644 apps/basic-integration/flask/flask3-social-media/posthog-setup-report.md diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/.posthog-wizard b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/.posthog-wizard new file mode 100644 index 000000000..e69de29bb diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/SKILL.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/SKILL.md new file mode 100644 index 000000000..edf7cd3d6 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/SKILL.md @@ -0,0 +1,62 @@ +--- +name: integration-flask +description: PostHog integration for Flask applications +metadata: + author: PostHog + version: dev +--- + +# PostHog integration for Flask + +This skill helps you add PostHog analytics to Flask applications. + +## Workflow + +Follow these steps in order to complete the integration: + +1. `references/1-begin.md` - PostHog Setup - Begin ← **Start here** +2. `references/2-edit.md` - PostHog Setup - Edit +3. `references/3-revise.md` - PostHog Setup - Revise +4. `references/4-conclude.md` - PostHog Setup - Conclusion + +## Reference files + +- `references/EXAMPLE.md` - Flask example project code +- `references/1-begin.md` - Start the event tracking setup process by analyzing the project and creating an event tracking plan +- `references/2-edit.md` - Implement PostHog event tracking in the identified files, following best practices and the example project +- `references/3-revise.md` - Review and fix any errors in the PostHog integration implementation +- `references/4-conclude.md` - Review and fix any errors in the PostHog integration implementation +- `references/flask.md` - Flask - docs +- `references/identify-users.md` - Identify users - docs + +The example project shows the target implementation pattern. Consult the documentation for API details. + +## Key principles + +- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them. +- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code. +- **Match the example**: Your implementation should follow the example project's patterns as closely as possible. + +## Framework guidelines + +- Initialize PostHog globally in create_app() using posthog.api_key and posthog.host (NOT per-request) +- Manually capture exceptions with `posthog.capture_exception(e)` for error tracking since Flask has built-in error handlers +- Blueprint registration happens AFTER PostHog initialization in create_app() +- Remember that source code is available in the venv/site-packages directory +- posthog is the Python SDK package name +- Install dependencies with `pip install posthog` or `pip install -r requirements.txt` and do NOT use unquoted version specifiers like `>=` directly in shell commands +- In CLIs and scripts: MUST call posthog.shutdown() before exit or all events are lost +- Always use the Posthog() class constructor (instance-based API) instead of module-level posthog.api_key config +- Always include enable_exception_autocapture=True in the Posthog() constructor to automatically track exceptions +- NEVER send PII in capture() event properties — no emails, full names, phone numbers, physical addresses, IP addresses, or user-generated content +- PII belongs in identify() person properties, NOT in capture() event properties. Safe event properties are metadata like message_length, form_type, boolean flags. +- Register posthog_client.shutdown with atexit.register() to ensure all events are flushed on exit +- The Python SDK has NO identify() method — use posthog_client.set(distinct_id=user_id, properties={...}) to set person properties, or use identify_context(user_id) within a context + +## Identifying users + +Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation. + +## Error tracking + +Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries. diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/1-begin.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/1-begin.md new file mode 100644 index 000000000..55f0a8326 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/1-begin.md @@ -0,0 +1,56 @@ +--- +title: PostHog Setup - Begin +description: Start the event tracking setup process by analyzing the project and creating an event tracking plan +--- + +We're making an event tracking plan for this project. + +This is the first of several phases — plan the events, implement them, revise and validate changes, then conclude by creating a dashboard and writing a setup report. + +## Task list + +As soon as you've read this description and have a rough sense of the work, make a single **call `TaskCreate` immediately** before reading any reference file or beginning analysis. The user is watching the task pane and shouldn't see it sit empty. + +It's fine if your first list is incomplete or imprecise. Seed it with whatever high-level items you can infer from the overview above, then call `TaskCreate` again (or `TaskUpdate` to refine existing items) every time your understanding sharpens: after a phase reveals work you didn't anticipate, after planning surfaces concrete sub-items, after you hit something new. Use `TaskUpdate` to mark items `in_progress` when you start them and `completed` when you finish. Keeping the list current matters more than getting it right on the first call. + +Keep task titles broad and job-oriented. Describe the purpose or area of work with wording like "Planning event tracking", "Identifying users", "Installing PostHog", "Capturing events", or "Creating dashboards", not the specific files, paths, or symbols involved. Adjust the task names according to the user's project and context. + +Before proceeding, find any existing `posthog.capture()` code. Make note of event name formatting. + +From the project's file list, select between 10 and 15 files that might have interesting business value for event tracking, especially conversion and churn events. Also look for additional files related to login that could be used for identifying users, along with error handling. Read the files. If a file is already well-covered by PostHog events, replace it with another option. Do not spawn subagents. + +Look for opportunities to track client-side events. + +**IMPORTANT: Server-side events are REQUIRED** if the project includes any instrumentable server-side code. If the project has API routes (e.g., `app/api/**/route.ts`) or Server Actions, you MUST include server-side events for critical business operations like: + + - Payment/checkout completion + - Webhook handlers + - Authentication endpoints + +Do not skip server-side events - they capture actions that cannot be tracked client-side. + +Create a new file with a JSON array at the root of the project: .posthog-events.json. It should include one object for each event we want to add with these exact field names: `event_name` (the event name), `event_description` (one sentence), and `file` (the file path the event goes in). The wizard reads this file to surface the plan in the UI. If events already exist, don't duplicate them; supplement them. + +Track actions only, not pageviews. These can be captured automatically. Exceptions can be made for "viewed"-type events that correspond to the top of a conversion funnel. + +As you review files, make an internal note of opportunities to identify users and catch errors. We'll need them for the next step. + +## Status + +Before beginning a phase of the setup, you will send a status message with the exact prefix '[STATUS]', as in: + +[STATUS] Checking project structure. + +Status to report in this phase: + +- Checking project structure +- Verifying PostHog dependencies +- Generating events based on project + +## Abort statuses + +If and only if the instructions have `[ABORT]` states specified, and you clearly match the conditions for an abort, emit the abort message. Do NOT attempt to exit or halt yourself — the wizard's middleware catches `[ABORT]` and terminates the run for you. + +--- + +**Upon completion, continue with:** [2-edit.md](2-edit.md) \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/2-edit.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/2-edit.md new file mode 100644 index 000000000..e5f7ffd16 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/2-edit.md @@ -0,0 +1,36 @@ +--- +title: PostHog Setup - Edit +description: Implement PostHog event tracking in the identified files, following best practices and the example project +--- + +For each of the files and events noted in .posthog-events.json, make edits to capture events using PostHog. Make sure to set up any helper files needed. Carefully examine the included example project code: your implementation should match it as closely as possible. Do not spawn subagents. + +Use environment variables for PostHog keys. Do not hardcode PostHog keys. + +If a file already has existing integration code for other tools or services, don't overwrite or remove that code. Place PostHog code below it. + +For each event, add useful properties, and use your access to the PostHog source code to ensure correctness. You also have access to documentation about creating new events with PostHog. Consider this documentation carefully and follow it closely before adding events. Your integration should be based on documented best practices. Carefully consider how the user project's framework version may impact the correct PostHog integration approach. + +Remember that you can find the source code for any dependency in the node_modules directory. This may be necessary to properly populate property names. There are also example project code files available via the PostHog MCP; use these for reference. + +Where possible, add calls for PostHog's identify() function on the client side upon events like logins and signups. Use the contents of login and signup forms to identify users on submit. If there is server-side code, pass the client-side session and distinct ID to the server-side code to identify the user. On the server side, make sure events have a matching distinct ID where relevant. + +It's essential to do this in both client code and server code, so that user behavior from both domains is easy to correlate. + +You should also add PostHog exception capture error tracking to these files where relevant. + +Remember: Do not alter the fundamental architecture of existing files. Make your additions minimal and targeted. + +Remember the documentation and example project resources you were provided at the beginning. Read them now. + +## Status + +Status to report in this phase: + +- Inserting PostHog capture code +- A status message for each file whose edits you are planning, including a high level summary of changes +- A status message for each file you have edited + +--- + +**Upon completion, continue with:** [3-revise.md](3-revise.md) \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/3-revise.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/3-revise.md new file mode 100644 index 000000000..3b07f5069 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/3-revise.md @@ -0,0 +1,22 @@ +--- +title: PostHog Setup - Revise +description: Review and fix any errors in the PostHog integration implementation +--- + +Check the project for errors. Read the package.json file for any type checking or build scripts that may provide input about what to fix. Remember that you can find the source code for any dependency in the node_modules directory. Do not spawn subagents. + +Ensure that any components created were actually used. + +Once all other tasks are complete, run any linter or prettier-like scripts found in the package.json, but ONLY on the files you have edited or created during this session. Do not run formatting or linting across the entire project's codebase. + +## Status + +Status to report in this phase: + +- Finding and correcting errors +- Report details of any errors you fix +- Linting, building and prettying + +--- + +**Upon completion, continue with:** [4-conclude.md](4-conclude.md) \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/4-conclude.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/4-conclude.md new file mode 100644 index 000000000..d876d4353 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/4-conclude.md @@ -0,0 +1,57 @@ +--- +title: PostHog Setup - Conclusion +description: Review and fix any errors in the PostHog integration implementation +--- + +Use the PostHog MCP to create a new dashboard named "Analytics basics (wizard)" based on the events created here. Keep the `(wizard)` tag with that exact casing so anyone browsing PostHog can see the wizard created this dashboard, and so a quick search for `(wizard)` surfaces every wizard-created artifact in one go. Make sure to use the exact same event names as implemented in the code. Populate it with up to five insights, with special emphasis on things like conversion funnels, churn events, and other business critical insights. + +Once the dashboard exists, emit its URL on its own line in your assistant message using this exact marker: `[DASHBOARD_URL] `. The wizard parses this marker from your visible message and surfaces the link in the success summary. Mentioning the URL only in thinking or in prose without the marker means the link is dropped. + +Search for a file called `.posthog-events.json` and read it for available events. + +Do not spawn subagents. + +Create the file posthog-setup-report.md. It should include a summary of the integration edits, a table with the event names, event descriptions, and files where events were added, a list of links for the dashboard and insights created, and a "Verify before merging" checklist (see below). Follow this format: + + +# PostHog post-wizard report + +The wizard has completed a deep integration of your project. [Detailed summary of changes] + +[table of events/descriptions/files] + +## Next steps + +We've built some insights and a dashboard for you to keep an eye on user behavior, based on the events we just instrumented: + +[links] + +## Verify before merging + +[checklist] + +### Agent skill + +We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. + + + +For the "Verify before merging" checklist, write GitHub-style checkboxes (`- [ ] ...`) covering what the developer (or their coding agent) still needs to do to take this from "wizard finished" to "merged". Include ONLY the items that actually apply to the integration you just performed — judge each against the code you changed in this run, and drop any that don't fit. Phrase each item as a concrete, checkable action. Candidate items, with the condition for including each: + +- Always: "Run a full production build (the wizard only verified the files it touched) and fix any lint or type errors introduced by the generated code." +- Always: "Run the test suite — call sites that were rewritten or instrumented may need updated mocks or fixtures." +- If you added environment variables: "Add the exact PostHog env var names you added to `.env.example` and any monorepo/bootstrap scripts so collaborators know what to set." +- If this integration ships a minified production browser bundle (most SPA/SSR web frameworks — e.g. Next.js, Nuxt, SvelteKit, Astro, Vite-based apps): "Wire source-map upload (`posthog-cli sourcemap` or your bundler's upload step) into CI so production stack traces de-minify." +- If LLM analytics was set up in this run: "Trigger the LLM call path(s) you instrumented and confirm `$ai_generation` events appear in PostHog AI Observability." +- If the app has user auth and an `identify` call was added: "Confirm the returning-visitor path also calls `identify` — a handler that only identifies on fresh login can leave returning sessions on anonymous distinct IDs." + +Do not invent items beyond what applies. If only the two "Always" items apply, the checklist is just those two. + +Upon completion, remove .posthog-events.json. + +## Status + +Status to report in this phase: + +- Configured dashboard: [insert PostHog dashboard URL] +- Created setup report: [insert full local file path] \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/EXAMPLE.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/EXAMPLE.md new file mode 100644 index 000000000..63465a49f --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/EXAMPLE.md @@ -0,0 +1,1208 @@ +# PostHog Flask Example Project + +Repository: https://github.com/PostHog/context-mill +Path: example-apps/flask + +--- + +## README.md + +# PostHog Flask Example + +A Flask application demonstrating PostHog integration for analytics, feature flags, and error tracking. + +## Features + +- User registration and authentication with Flask-Login +- SQLite database persistence with Flask-SQLAlchemy +- User identification and property tracking +- Custom event tracking +- Feature flags with payload support +- Error tracking with manual exception capture + +## Quick Start + +1. Create and activate a virtual environment: + ```bash + python -m venv venv + source venv/bin/activate # On Windows: venv\Scripts\activate + ``` + +2. Install dependencies: + ```bash + pip install -r requirements.txt + ``` + +3. Copy the environment file and configure: + ```bash + cp .env.example .env + # Edit .env with your PostHog project key + ``` + +4. Run the application: + ```bash + python run.py + ``` + +5. Open http://localhost:5001 and either: + - Login with default credentials: `admin@example.com` / `admin` + - Or click "Sign up here" to create a new account + +## PostHog Integration Points + +### User Registration +New users are identified and tracked on signup using the context-based API: +```python +with new_context(): + identify_context(user.email) + tag('email', user.email) + tag('is_staff', user.is_staff) + capture('user_signed_up', properties={'signup_method': 'form'}) +``` + +### User Identification +Users are identified on login with their properties: +```python +with new_context(): + identify_context(user.email) + tag('email', user.email) + tag('is_staff', user.is_staff) + capture('user_logged_in', properties={'login_method': 'password'}) +``` + +### Event Tracking +Custom events are captured throughout the app: +```python +with new_context(): + identify_context(current_user.email) + capture('burrito_considered', properties={'total_considerations': count}) +``` + +### Feature Flags +The dashboard demonstrates feature flag checking: +```python +show_new_feature = posthog.feature_enabled( + 'new-dashboard-feature', + current_user.email, + person_properties={'email': current_user.email, 'is_staff': current_user.is_staff} +) +feature_config = posthog.get_feature_flag_payload('new-dashboard-feature', current_user.email) +``` + +### Error Tracking + +The example demonstrates two approaches to error tracking: + +Manual capture for specific critical operations** (`app/api/routes.py`). + +```python +try: + # Critical operation that might fail + result = process_payment() +except Exception as e: + # Manually capture this specific exception + with new_context(): + identify_context(current_user.email) + event_id = posthog.capture_exception(e) + + return jsonify({ + "error": "Operation failed", + "error_id": event_id, + "message": f"Error captured in PostHog. Reference ID: {event_id}" + }), 500 +``` + +The `/api/test-error` endpoint demonstrates manual exception capture. Use `?capture=true` to capture in PostHog, or `?capture=false` to skip tracking. + +## Project Structure + +``` +basics/flask/ +├── app/ +│ ├── __init__.py # Application factory +│ ├── config.py # Configuration classes +│ ├── extensions.py # Extension instances +│ ├── models.py # User model (SQLAlchemy) +│ ├── main/ +│ │ ├── __init__.py # Main blueprint +│ │ └── routes.py # View functions +│ ├── templates/ # HTML templates +│ └── api/ +│ ├── __init__.py # API blueprint +│ └── routes.py # API endpoints +├── .env.example +├── .gitignore +├── requirements.txt +├── README.md +└── run.py # Entry point +``` + +--- + +## .env.example + +```example +POSTHOG_PROJECT_TOKEN= +POSTHOG_HOST=https://us.i.posthog.com +FLASK_SECRET_KEY=your-secret-key-here +FLASK_DEBUG=True +POSTHOG_DISABLED=False + +``` + +--- + +## app/__init__.py + +```py +"""Flask application factory.""" + +import posthog +from flask import Flask, g, jsonify, render_template, request +from flask_login import current_user +from posthog import identify_context, new_context +from werkzeug.exceptions import HTTPException + +from app.config import config +from app.extensions import db, login_manager + + +def create_app(config_name="default"): + """Application factory.""" + app = Flask(__name__) + app.config.from_object(config[config_name]) + + # Initialize extensions + db.init_app(app) + login_manager.init_app(app) + + # Initialize PostHog + if not app.config["POSTHOG_DISABLED"]: + posthog.api_key = app.config["POSTHOG_PROJECT_TOKEN"] + posthog.host = app.config["POSTHOG_HOST"] + posthog.debug = app.config["DEBUG"] + + # Import models after db is initialized + from app.models import User + + # User loader for Flask-Login + @login_manager.user_loader + def load_user(user_id): + return User.get_by_id(user_id) + + # Simple error handlers - no automatic PostHog capture + # Capture exceptions manually only where it makes sense (e.g., test endpoints) + @app.errorhandler(404) + def page_not_found(e): + if request.path.startswith('/api/'): + return jsonify({"error": "Not found"}), 404 + return render_template('errors/404.html'), 404 + + @app.errorhandler(500) + def internal_server_error(e): + if request.path.startswith('/api/'): + return jsonify({"error": "Internal server error"}), 500 + return render_template('errors/500.html'), 500 + + # Register blueprints + from app.api import api_bp + from app.main import main_bp + + app.register_blueprint(main_bp) + app.register_blueprint(api_bp, url_prefix="/api") + + # Create database tables and seed default admin user + with app.app_context(): + db.create_all() + if not User.get_by_email("admin@example.com"): + User.create_user( + email="admin@example.com", + password="admin", + is_staff=True, + ) + + return app + +``` + +--- + +## app/api/__init__.py + +```py +"""API blueprint registration.""" + +from flask import Blueprint + +api_bp = Blueprint("api", __name__) + +from app.api import routes # noqa: E402, F401 + +``` + +--- + +## app/api/routes.py + +```py +"""API endpoints demonstrating PostHog integration patterns.""" + +import posthog +from flask import jsonify, request, session +from flask_login import current_user, login_required +from posthog import capture, identify_context, new_context + +from app.api import api_bp + + +@api_bp.route("/burrito/consider", methods=["POST"]) +@login_required +def consider_burrito(): + """Track burrito consideration event.""" + # Increment session counter + burrito_count = session.get("burrito_count", 0) + 1 + session["burrito_count"] = burrito_count + + # PostHog: Capture custom event + with new_context(): + identify_context(current_user.email) + capture("burrito_considered", properties={"total_considerations": burrito_count}) + + return jsonify({"success": True, "count": burrito_count}) + + +@api_bp.route("/test-error", methods=["POST"]) +@login_required +def test_error(): + """Test endpoint demonstrating manual exception capture in PostHog. + + Shows how to intentionally capture specific errors in PostHog. + Use this pattern for critical operations where you want error tracking. + + Query params: + - capture: "true" to capture the exception in PostHog, "false" to just raise it + """ + should_capture = request.args.get("capture", "true").lower() == "true" + + try: + # Simulate a critical operation failure + raise Exception("Test exception from critical operation") + except Exception as e: + if should_capture: + # Manually capture this specific exception in PostHog + with new_context(): + identify_context(current_user.email) + event_id = posthog.capture_exception(e) + + return jsonify({ + "error": "Operation failed", + "error_id": event_id, + "message": f"Error captured in PostHog. Reference ID: {event_id}" + }), 500 + else: + # Just return error without PostHog capture + return jsonify({"error": str(e)}), 500 + + + +``` + +--- + +## app/config.py + +```py +"""Flask application configuration.""" + +import os +from dotenv import load_dotenv + +load_dotenv() + + +class Config: + """Base configuration.""" + + SECRET_KEY = os.environ.get("FLASK_SECRET_KEY", "dev-secret-key-change-in-production") + + # Database configuration (SQLite like Django example) + SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL", "sqlite:///db.sqlite3") + SQLALCHEMY_TRACK_MODIFICATIONS = False + + # PostHog configuration + POSTHOG_PROJECT_TOKEN = os.environ.get("POSTHOG_PROJECT_TOKEN", "") + POSTHOG_HOST = os.environ.get("POSTHOG_HOST", "https://us.i.posthog.com") + POSTHOG_DISABLED = os.environ.get("POSTHOG_DISABLED", "False").lower() == "true" + + +class DevelopmentConfig(Config): + """Development configuration.""" + + DEBUG = True + + +class ProductionConfig(Config): + """Production configuration.""" + + DEBUG = False + + +config = { + "development": DevelopmentConfig, + "production": ProductionConfig, + "default": DevelopmentConfig, +} + +``` + +--- + +## app/extensions.py + +```py +"""Flask extensions initialized without binding to app.""" + +from flask_login import LoginManager +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +login_manager = LoginManager() +login_manager.login_view = "main.home" +login_manager.login_message = "Please log in to access this page." + +``` + +--- + +## app/main/__init__.py + +```py +"""Main blueprint registration.""" + +from flask import Blueprint + +main_bp = Blueprint("main", __name__, template_folder="../templates") + +from app.main import routes # noqa: E402, F401 + +``` + +--- + +## app/main/routes.py + +```py +"""Core view functions demonstrating PostHog integration patterns.""" + +import posthog +from flask import flash, redirect, render_template, request, session, url_for +from flask_login import current_user, login_required, login_user, logout_user +from posthog import capture, identify_context, new_context, tag + +from app.main import main_bp +from app.models import User + + +@main_bp.route("/", methods=["GET", "POST"]) +def home(): + """Home/login page.""" + if current_user.is_authenticated: + return redirect(url_for("main.dashboard")) + + if request.method == "POST": + email = request.form.get("email") + password = request.form.get("password") + + user = User.authenticate(email, password) + if user: + login_user(user) + + # PostHog: Identify user and capture login event + with new_context(): + identify_context(user.email) + + # Set person properties (PII goes in tag, not capture) + tag("email", user.email) + tag("is_staff", user.is_staff) + tag("date_joined", user.date_joined.isoformat()) + + capture("user_logged_in", properties={"login_method": "password"}) + + return redirect(url_for("main.dashboard")) + else: + flash("Invalid email or password", "error") + + return render_template("home.html") + + +@main_bp.route("/signup", methods=["GET", "POST"]) +def signup(): + """User registration page.""" + if current_user.is_authenticated: + return redirect(url_for("main.dashboard")) + + if request.method == "POST": + email = request.form.get("email") + password = request.form.get("password") + password_confirm = request.form.get("password_confirm") + + # Validation + if not email or not password: + flash("Email and password are required", "error") + elif password != password_confirm: + flash("Passwords do not match", "error") + elif User.get_by_email(email): + flash("Email already registered", "error") + else: + # Create new user + user = User.create_user( + email=email, + password=password, + is_staff=False, + ) + + # PostHog: Identify new user and capture signup event + with new_context(): + identify_context(user.email) + + tag("email", user.email) + tag("is_staff", user.is_staff) + tag("date_joined", user.date_joined.isoformat()) + + capture("user_signed_up", properties={"signup_method": "form"}) + + # Log the user in + login_user(user) + flash("Account created successfully!", "success") + return redirect(url_for("main.dashboard")) + + return render_template("signup.html") + + +@main_bp.route("/logout") +@login_required +def logout(): + """Logout and capture event.""" + # PostHog: Capture logout event before session ends + with new_context(): + identify_context(current_user.email) + capture("user_logged_out") + + logout_user() + return redirect(url_for("main.home")) + + +@main_bp.route("/dashboard") +@login_required +def dashboard(): + """Dashboard with feature flag demonstration.""" + # PostHog: Capture dashboard view + with new_context(): + identify_context(current_user.email) + capture("dashboard_viewed", properties={"is_staff": current_user.is_staff}) + + # Check feature flag + show_new_feature = posthog.feature_enabled( + "new-dashboard-feature", + current_user.email, + person_properties={ + "email": current_user.email, + "is_staff": current_user.is_staff, + }, + ) + + # Get feature flag payload + feature_config = posthog.get_feature_flag_payload( + "new-dashboard-feature", current_user.email + ) + + return render_template( + "dashboard.html", + show_new_feature=show_new_feature, + feature_config=feature_config, + ) + + +@main_bp.route("/burrito") +@login_required +def burrito(): + """Burrito consideration tracker page.""" + burrito_count = session.get("burrito_count", 0) + return render_template("burrito.html", burrito_count=burrito_count) + + +@main_bp.route("/profile") +@login_required +def profile(): + """User profile page.""" + # PostHog: Capture profile view + with new_context(): + identify_context(current_user.email) + capture("profile_viewed") + + return render_template("profile.html") + +``` + +--- + +## app/models.py + +```py +"""User model with SQLite persistence (similar to Django's auth.User).""" + +from datetime import datetime, timezone + +from flask_login import UserMixin +from werkzeug.security import check_password_hash, generate_password_hash + +from app.extensions import db + + +class User(UserMixin, db.Model): + """User model with SQLite persistence.""" + + __tablename__ = "users" + + id = db.Column(db.Integer, primary_key=True) + email = db.Column(db.String(254), unique=True, nullable=False) + password_hash = db.Column(db.String(256), nullable=False) + is_staff = db.Column(db.Boolean, default=False) + is_active = db.Column(db.Boolean, default=True) + date_joined = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc)) + + def set_password(self, password): + """Hash and set the user's password.""" + self.password_hash = generate_password_hash(password) + + def check_password(self, password): + """Verify the password against the hash.""" + return check_password_hash(self.password_hash, password) + + @classmethod + def create_user(cls, email, password, is_staff=False): + """Create and save a new user.""" + user = cls(email=email, is_staff=is_staff) + # nosemgrep: python.django.security.audit.unvalidated-password.unvalidated-password + user.set_password(password) + db.session.add(user) + db.session.commit() + return user + + @classmethod + def get_by_id(cls, user_id): + """Get user by ID.""" + return cls.query.get(int(user_id)) + + @classmethod + def get_by_email(cls, email): + """Get user by email.""" + return cls.query.filter_by(email=email).first() + + @classmethod + def authenticate(cls, email, password): + """Authenticate user with email and password.""" + user = cls.get_by_email(email) + if user and user.check_password(password): + return user + return None + + def __repr__(self): + return f"" + +``` + +--- + +## app/templates/base.html + +```html + + + + + + {% block title %}PostHog Flask Example{% endblock %} + + + + {% if current_user.is_authenticated %} + + {% endif %} + +
+ {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} +
+ {% for category, message in messages %} +
{{ message }}
+ {% endfor %} +
+ {% endif %} + {% endwith %} + + {% block content %}{% endblock %} +
+ + {% block scripts %}{% endblock %} + + + +``` + +--- + +## app/templates/burrito.html + +```html +{% extends "base.html" %} + +{% block title %}Burrito - PostHog Flask Example{% endblock %} + +{% block content %} +
+

Burrito Consideration Tracker

+

This page demonstrates custom event tracking with PostHog.

+ +
{{ burrito_count }}
+

Times you've considered a burrito

+ +
+ +
+
+ +
+

Code Example

+
+# API endpoint captures the event
+with new_context():
+    identify_context(current_user.email)
+    capture('burrito_considered', properties={
+        'total_considerations': burrito_count
+    })
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + +``` + +--- + +## app/templates/dashboard.html + +```html +{% extends "base.html" %} + +{% block title %}Dashboard - PostHog Flask Example{% endblock %} + +{% block content %} +
+

Dashboard

+

Welcome back, {{ current_user.username }}!

+
+ +
+

Feature Flags

+ + {% if show_new_feature %} +
+ New Feature Enabled! +

You're seeing this because the new-dashboard-feature flag is enabled for you.

+ {% if feature_config %} +

Feature Configuration:

+
{{ feature_config | tojson(indent=2) }}
+ {% endif %} +
+ {% else %} +

The new-dashboard-feature flag is not enabled for your account.

+ {% endif %} + +

Code Example

+
+# Check if feature flag is enabled
+show_new_feature = posthog.feature_enabled(
+    'new-dashboard-feature',
+    user_id,
+    person_properties={
+        'email': current_user.email,
+        'is_staff': current_user.is_staff
+    }
+)
+
+# Get feature flag payload
+feature_config = posthog.get_feature_flag_payload(
+    'new-dashboard-feature',
+    user_id
+)
+
+{% endblock %} + +``` + +--- + +## app/templates/errors/404.html + +```html +{% extends "base.html" %} + +{% block title %}404 - Page Not Found{% endblock %} + +{% block content %} +
+

404

+

Page Not Found

+

+ The page you're looking for doesn't exist or has been moved. +

+ + {% if error_id %} +
+

Error Reference ID:

+ {{ error_id }} +

+ Share this ID with support if you need assistance. +

+
+ {% endif %} + +
+ Go to Home + {% if current_user.is_authenticated %} + Go to Dashboard + {% endif %} +
+
+{% endblock %} + +``` + +--- + +## app/templates/errors/500.html + +```html +{% extends "base.html" %} + +{% block title %}500 - Internal Server Error{% endblock %} + +{% block content %} +
+

500

+

Internal Server Error

+

+ Something went wrong on our end. We've been notified and are looking into it. +

+ + {% if error_id %} +
+

Error Reference ID:

+ {{ error_id }} +

+ Share this ID with support if you need assistance. This error has been logged in PostHog. +

+
+ {% endif %} + + {% if error and config.DEBUG %} +
+

Debug Information:

+ {{ error }} +
+ {% endif %} + +
+ Go to Home + {% if current_user.is_authenticated %} + Go to Dashboard + {% endif %} +
+
+{% endblock %} + +``` + +--- + +## app/templates/home.html + +```html +{% extends "base.html" %} + +{% block title %}Login - PostHog Flask Example{% endblock %} + +{% block content %} +
+

Welcome to PostHog Flask Example

+

This example demonstrates how to integrate PostHog with a Flask application.

+ +
+ + + + + + + +
+ +

+ Don't have an account? Sign up here +

+

+ Tip: Default credentials are admin@example.com/admin +

+
+ +
+

Features Demonstrated

+
    +
  • User registration and identification
  • +
  • Event tracking
  • +
  • Feature flags
  • +
  • Error tracking
  • +
  • Group analytics
  • +
+
+{% endblock %} + +``` + +--- + +## app/templates/profile.html + +```html +{% extends "base.html" %} + +{% block title %}Profile - PostHog Flask Example{% endblock %} + +{% block content %} +
+

Your Profile

+

This page demonstrates error tracking with PostHog.

+ + + + + + + + + + + + + + +
Email{{ current_user.email }}
Date Joined{{ current_user.date_joined.strftime('%Y-%m-%d %H:%M') }}
Staff Status{{ 'Yes' if current_user.is_staff else 'No' }}
+
+ +
+

Error Tracking Demo

+

Click a button to trigger an error and see it captured in PostHog:

+ +
+ + + +
+ + +
+ +
+

Code Example

+
+try:
+    raise ValueError('Invalid value provided')
+except Exception as e:
+    # Capture exception and event with user context
+    with new_context():
+        identify_context(current_user.email)
+        posthog.capture_exception(e)
+        capture('error_triggered', properties={
+            'error_type': 'value',
+            'error_message': str(e)
+        })
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + +``` + +--- + +## app/templates/signup.html + +```html +{% extends "base.html" %} + +{% block title %}Sign Up - PostHog Flask Example{% endblock %} + +{% block content %} +
+

Create an Account

+

Sign up to explore the PostHog Flask integration example.

+ +
+ + + + + + + + + + +
+ +

+ Already have an account? Login here +

+
+ +
+

PostHog Integration

+

When you sign up, the following PostHog events are captured:

+
    +
  • identify_context() - Associates your email with the context
  • +
  • tag() - Sets person properties (email, etc.)
  • +
  • user_signed_up event - Tracks the signup action
  • +
+ +

Code Example

+
+# After creating the user
+with new_context():
+    identify_context(user.email)
+
+    tag('email', user.email)
+    tag('is_staff', user.is_staff)
+    tag('date_joined', user.date_joined.isoformat())
+
+    capture('user_signed_up', properties={'signup_method': 'form'})
+
+{% endblock %} + +``` + +--- + +## requirements.txt + +```txt +Flask>=3.1.0 +Flask-Login>=0.6.3 +Flask-SQLAlchemy>=3.1.0 +python-dotenv>=1.0.0 +posthog>=3.0.0 +Werkzeug>=3.0.0 + +``` + +--- + +## run.py + +```py +"""Development server entry point.""" + +from app import create_app + +app = create_app() + +if __name__ == "__main__": + app.run(port=5001) + +``` + +--- + diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/flask.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/flask.md new file mode 100644 index 000000000..86c3984b3 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/flask.md @@ -0,0 +1,117 @@ +# Flask - Docs + +PostHog makes it easy to get data about traffic and usage of your Flask app. Integrating PostHog enables analytics, custom events capture, feature flags, error tracking, and more. + +This guide walks you through integrating PostHog into your Flask app using the [Python SDK](/docs/libraries/python.md). + +## Installation + +To start, run `pip install posthog` to install PostHog’s Python SDK. + +> **Note:** Version `7.x` of the PostHog Python SDK requires Python 3.10 or higher. + +Then, initialize PostHog where you'd like to use it. For example, here's how to capture an event in a simple route: + +app.py + +PostHog AI + +```python +from flask import Flask +from posthog import Posthog +app = Flask(__name__) +posthog = Posthog( + '', + host='https://us.i.posthog.com', +) +@app.route('/api/dashboard', methods=['POST']) +def api_dashboard(): + posthog.capture( + 'dashboard_api_called', + distinct_id='distinct_id_of_your_user', + ) + return '', 204 +``` + +You can find your project token and instance address in [your project settings](https://app.posthog.com/project/settings). + +## Identifying users + +> **Identifying users is required.** Backend events need a `distinct_id` that matches the ID your frontend uses when calling `posthog.identify()`. Without this, backend events are orphaned — they can't be linked to frontend event captures, [session replays](/docs/session-replay.md), [LLM traces](/docs/ai-engineering.md), or [error tracking](/docs/error-tracking.md). +> +> See our guide on [identifying users](/docs/getting-started/identify-users.md) for how to set this up. + +## Request contexts + +Use [contexts](/docs/libraries/python.md#contexts) to share identity, session IDs, and tags across multiple captures during a request. + +If you're using [PostHog JS](/docs/libraries/js.md) on the frontend, configure [`tracing_headers`](/docs/libraries/js/config.md#tracing-headers) for your Flask backend hostname so browser requests include the session and distinct ID headers. + +Then read the incoming headers in your Flask request handler. Tracing headers are client-controlled analytics context, not authentication or authorization, so prefer your authenticated user ID when one is available: + +Python + +PostHog AI + +```python +from flask import request, session +from posthog import identify_context, set_context_session, tag +@app.route('/api/dashboard', methods=['POST']) +def api_dashboard(): + with posthog.new_context(fresh=True): + distinct_id = session.get('user_id') or request.headers.get('X-POSTHOG-DISTINCT-ID') + if distinct_id: + identify_context(str(distinct_id)) + session_id = request.headers.get('X-POSTHOG-SESSION-ID') + if session_id: + set_context_session(session_id) + tag('$current_url', request.url) + tag('$request_method', request.method) + tag('$request_path', request.path) + posthog.capture('dashboard_api_called') + return '', 204 +``` + +Events captured without a context or explicit `distinct_id` are sent as [anonymous events](/docs/data/anonymous-vs-identified-events.md) with an auto-generated `distinct_id`. See the [Python SDK docs](/docs/libraries/python.md#person-profiles-and-properties) for more details. + +## Error tracking + +Flask has built-in error handlers. This means PostHog’s default exception autocapture won’t work and we need to manually capture errors instead using `capture_exception()`: + +Python + +PostHog AI + +```python +from flask import Flask, jsonify +from posthog import Posthog +app = Flask(__name__) +posthog = Posthog('', host='https://us.i.posthog.com') +@app.errorhandler(Exception) +def handle_exception(e): + # Capture methods, including capture_exception, return the UUID of the captured event, + # which you can use to find specific errors users encountered + event_id = posthog.capture_exception(e) + # You can show the event ID to your user, and ask them to include it in bug reports + response = jsonify({'message': str(e), 'error_id': event_id}) + response.status_code = 500 + return response +``` + +## Next steps + +For any technical questions for how to integrate specific PostHog features into Flask (such as analytics, feature flags, A/B testing, etc.), have a look at our [Python SDK docs](/docs/libraries/python.md). + +Alternatively, the following tutorials can help you get started: + +- [How to set up analytics in Python and Flask](/tutorials/python-analytics.md) +- [How to set up feature flags in Python and Flask](/tutorials/python-feature-flags.md) +- [How to set up A/B tests in Python and Flask](/tutorials/python-ab-testing.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/identify-users.md b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/identify-users.md new file mode 100644 index 000000000..1417e03a8 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/.claude/skills/integration-flask/references/identify-users.md @@ -0,0 +1,272 @@ +# Identify users - Docs + +Linking events to specific users enables you to build a full picture of how they're using your product across different sessions, devices, and platforms. + +This is straightforward to do when [capturing backend events](/docs/product-analytics/capture-events?tab=Node.js.md), as you associate events to a specific user using a `distinct_id`, which is a required argument. + +However, in the frontend of a [web](/docs/libraries/js/features.md#capturing-events) or [mobile app](/docs/libraries/ios.md#capturing-events), a `distinct_id` is not a required argument — PostHog's SDKs will generate an anonymous `distinct_id` for you automatically and you can capture events anonymously, provided you use the appropriate [configuration](/docs/libraries/js/features.md#capturing-anonymous-events). + +To link events to specific users, call `identify`: + +PostHog AI + +### Web + +```javascript +posthog.identify( + 'distinct_id', // Replace 'distinct_id' with your user's unique identifier + { email: 'max@hedgehogmail.com', name: 'Max Hedgehog' } // optional: set additional person properties +); +``` + +### Android + +```kotlin +PostHog.identify( + distinctId = distinctID, // Replace 'distinctID' with your user's unique identifier + // optional: set additional person properties + userProperties = mapOf( + "name" to "Max Hedgehog", + "email" to "max@hedgehogmail.com" + ) +) +``` + +### iOS + +```swift +PostHogSDK.shared.identify("distinct_id", // Replace "distinct_id" with your user's unique identifier + userProperties: ["name": "Max Hedgehog", "email": "max@hedgehogmail.com"]) // optional: set additional person properties +``` + +### React Native + +```jsx +posthog.identify('distinct_id', { // Replace "distinct_id" with your user's unique identifier + email: 'max@hedgehogmail.com', // optional: set additional person properties + name: 'Max Hedgehog' +}) +``` + +### Dart + +```dart +await Posthog().identify( + userId: 'distinct_id', // Replace "distinct_id" with your user's unique identifier + userProperties: { + 'email': 'max@hedgehogmail.com', // optional: set additional person properties + 'name': 'Max Hedgehog', + }, +); +``` + +Events captured after calling `identify` are identified events and this creates a person profile if one doesn't exist already. + +Due to the cost of processing them, anonymous events can be up to 4x cheaper than identified events, so it's recommended you only capture identified events when needed. + +## How identify works + +When a user starts browsing your website or app, PostHog automatically assigns them an **anonymous ID**, which is stored locally. + +Provided you've [configured persistence](/docs/libraries/js/persistence.md) to use cookies or `localStorage`, this enables us to track anonymous users – even across different sessions. + +By calling `identify` with a `distinct_id` of your choice (usually the user's ID in your database, or their email), you link the anonymous ID and distinct ID together. + +Thus, all past and future events made with that anonymous ID are now associated with the distinct ID. + +This enables you to do things like associate events with a user from before they log in for the first time, or associate their events across different devices or platforms. + +Using identify in the backend + +Although you can call `identify` using our backend SDKs, it is used most in frontends. This is because there is no concept of anonymous sessions in the backend SDKs, so calling `identify` only updates person profiles. + +## Best practices when using `identify` + +### 1\. Call `identify` as soon as you're able to + +In your frontend, you should call `identify` as soon as you're able to. + +Typically, this is every time your **app loads** for the first time, and directly after your **users log in**. + +This ensures that events sent during your users' sessions are correctly associated with them. + +You only need to call `identify` once per session, and you should avoid calling it multiple times unnecessarily. + +If you call `identify` multiple times with the same data without reloading the page in between, PostHog will ignore the subsequent calls. + +### 2\. Use unique strings for distinct IDs + +If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. Two common ways this can happen are: + +- Your logic for generating IDs does not generate sufficiently strong IDs and you can end up with a clash where 2 users have the same ID. +- There's a bug, typo, or mistake in your code leading to most or all users being identified with generic IDs like `null`, `true`, or `distinctId`. + +PostHog also has built-in protections to stop the most common distinct ID mistakes. + +### 3\. Reset after logout + +If a user logs out on your frontend, you should call `reset()` to unlink any future events made on that device with that user. + +This is important if your users are sharing a computer, as otherwise all of those users are grouped together into a single user due to shared cookies between sessions. + +**We strongly recommend you call `reset` on logout even if you don't expect users to share a computer.** + +You can do that like so: + +PostHog AI + +### Web + +```javascript +posthog.reset() +``` + +### iOS + +```swift +PostHogSDK.shared.reset() +``` + +### Android + +```kotlin +PostHog.reset() +``` + +### React Native + +```jsx +posthog.reset() +``` + +### Dart + +```dart +await Posthog().reset(); +``` + +If you *also* want to reset the `device_id` so that the device will be considered a new device in future events, you can pass `true` as an argument: + +Web + +PostHog AI + +```javascript +posthog.reset(true) +``` + +### 4\. Person profiles and properties + +You'll notice that one of the parameters in the `identify` method is a `properties` object. + +This enables you to set [person properties](/docs/product-analytics/person-properties.md). + +Whenever possible, we recommend passing in all person properties you have available each time you call identify, as this ensures their person profile on PostHog is up to date. + +Person properties can also be set being adding a `$set` property to a event `capture` call. + +See our [person properties docs](/docs/product-analytics/person-properties.md) for more details on how to work with them and best practices. + +### 5\. Use deep links between platforms + +We recommend you call `identify` [as soon as you're able](#1-call-identify-as-soon-as-youre-able), typically when a user signs up or logs in. + +This doesn't work if one or both platforms are unauthenticated. Some examples of such cases are: + +- Onboarding and signup flows before authentication. +- Unauthenticated web pages redirecting to authenticated mobile apps. +- Authenticated web apps prompting an app download. + +In these cases, you can use a [deep link](https://developer.android.com/training/app-links/deep-linking) on Android and [universal links](https://developer.apple.com/documentation/xcode/supporting-universal-links-in-your-app) on iOS to identify users. + +1. Use `posthog.get_distinct_id()` to get the current distinct ID. Even if you cannot call identify because the user is unauthenticated, this will return an anonymous distinct ID generated by PostHog. +2. Add the distinct ID to the deep link as query parameters, along with other properties like UTM parameters. +3. When the user is redirected to the app, parse the deep link and handle the following cases: + +- The mobile app is already authenticated. In this case, call [`posthog.alias()`](/docs/libraries/js/features.md#alias) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The mobile app is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features.md#identifying-users) with the distinct ID from the web so pre-login mobile events stay connected to the web session. When the user later logs in on mobile, call `identify()` again with your canonical user ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +Here's an example implementation for handling deep links from web to mobile: + +PostHog AI + +### iOS + +```swift +import PostHog +class DeepLinkIdentityManager { + static let shared = DeepLinkIdentityManager() + // MARK: - Deep Link Received + func handleDeepLink(_ url: URL, isAuthenticatedOnMobile: Bool) { + guard let webDistinctId = URLComponents(url: url, resolvingAgainstBaseURL: true)? + .queryItems?.first(where: { $0.name == "ph_distinct_id" })?.value else { + return + } + if isAuthenticatedOnMobile { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHogSDK.shared.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHogSDK.shared.identify(webDistinctId) + } + } + // MARK: - Login/Signup + func handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHogSDK.shared.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + func handleLogout() { + PostHogSDK.shared.reset() + } +} +``` + +### Android + +```kotlin +import android.net.Uri +import com.posthog.PostHog +object DeepLinkIdentityManager { + // Deep Link Received + fun handleDeepLink(uri: Uri, isAuthenticatedOnMobile: Boolean) { + val webDistinctId = uri.getQueryParameter("ph_distinct_id") ?: return + if (isAuthenticatedOnMobile) { + // The mobile app already knows the current user. + // Alias the incoming web distinct ID to that user. + PostHog.alias(webDistinctId) + } else { + // Reuse the web distinct ID until login on mobile. + PostHog.identify(webDistinctId) + } + } + // Login/Signup + fun handleLogin(canonicalUserId: String) { + // Switch from the web distinct ID (or a mobile anon ID) + // to your canonical user ID. + PostHog.identify(canonicalUserId) + // Set user properties, track signup event, etc. + } + fun handleLogout() { + PostHog.reset() + } +} +``` + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline.md#2-person-processing) +- [An introductory guide to identifying users in PostHog](/tutorials/identifying-users-guide.md) + +### Community questions + +Ask a question + +### Was this page useful? + +HelpfulCould be better \ No newline at end of file diff --git a/apps/basic-integration/flask/flask3-social-media/.gitignore b/apps/basic-integration/flask/flask3-social-media/.gitignore index 1e4f83607..764b603e7 100644 --- a/apps/basic-integration/flask/flask3-social-media/.gitignore +++ b/apps/basic-integration/flask/flask3-social-media/.gitignore @@ -38,3 +38,4 @@ nosetests.xml venv app.db microblog.log* +.env diff --git a/apps/basic-integration/flask/flask3-social-media/app/__init__.py b/apps/basic-integration/flask/flask3-social-media/app/__init__.py index f15c103dd..d3760cd21 100644 --- a/apps/basic-integration/flask/flask3-social-media/app/__init__.py +++ b/apps/basic-integration/flask/flask3-social-media/app/__init__.py @@ -1,6 +1,8 @@ +import atexit import logging from logging.handlers import SMTPHandler, RotatingFileHandler import os +import posthog from flask import Flask, request, current_app from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate @@ -39,6 +41,10 @@ def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) + posthog.api_key = app.config.get('POSTHOG_PROJECT_TOKEN', '') + posthog.host = app.config.get('POSTHOG_HOST', 'https://us.i.posthog.com') + atexit.register(posthog.shutdown) + db.init_app(app) migrate.init_app(app, db) login.init_app(app) diff --git a/apps/basic-integration/flask/flask3-social-media/app/api/tokens.py b/apps/basic-integration/flask/flask3-social-media/app/api/tokens.py index 29a82a40f..8af933803 100644 --- a/apps/basic-integration/flask/flask3-social-media/app/api/tokens.py +++ b/apps/basic-integration/flask/flask3-social-media/app/api/tokens.py @@ -1,3 +1,4 @@ +from posthog import capture, identify_context, new_context from app import db from app.api import bp from app.api.auth import basic_auth, token_auth @@ -6,8 +7,12 @@ @bp.route('/tokens', methods=['POST']) @basic_auth.login_required def get_token(): - token = basic_auth.current_user().get_token() + user = basic_auth.current_user() + token = user.get_token() db.session.commit() + with new_context(): + identify_context(user.username) + capture('api_token_issued') return {'token': token} diff --git a/apps/basic-integration/flask/flask3-social-media/app/api/users.py b/apps/basic-integration/flask/flask3-social-media/app/api/users.py index 8a0b59e5c..9afd06be7 100644 --- a/apps/basic-integration/flask/flask3-social-media/app/api/users.py +++ b/apps/basic-integration/flask/flask3-social-media/app/api/users.py @@ -1,5 +1,6 @@ import sqlalchemy as sa from flask import request, url_for, abort +from posthog import capture, identify_context, new_context from app import db from app.models import User from app.api import bp @@ -57,6 +58,9 @@ def create_user(): user.from_dict(data, new_user=True) db.session.add(user) db.session.commit() + with new_context(): + identify_context(user.username) + capture('api_user_created', properties={'signup_method': 'api'}) return user.to_dict(), 201, {'Location': url_for('api.get_user', id=user.id)} diff --git a/apps/basic-integration/flask/flask3-social-media/app/auth/routes.py b/apps/basic-integration/flask/flask3-social-media/app/auth/routes.py index 79360ff7b..0befc82c1 100644 --- a/apps/basic-integration/flask/flask3-social-media/app/auth/routes.py +++ b/apps/basic-integration/flask/flask3-social-media/app/auth/routes.py @@ -3,6 +3,7 @@ from flask_login import login_user, logout_user, current_user from flask_babel import _ import sqlalchemy as sa +from posthog import capture, identify_context, new_context, tag from app import db from app.auth import bp from app.auth.forms import LoginForm, RegistrationForm, \ @@ -23,6 +24,10 @@ def login(): flash(_('Invalid username or password')) return redirect(url_for('auth.login')) login_user(user, remember=form.remember_me.data) + with new_context(): + identify_context(user.username) + tag('username', user.username) + capture('user_logged_in', properties={'login_method': 'password'}) next_page = request.args.get('next') if not next_page or urlsplit(next_page).netloc != '': next_page = url_for('main.index') @@ -32,6 +37,10 @@ def login(): @bp.route('/logout') def logout(): + if current_user.is_authenticated: + with new_context(): + identify_context(current_user.username) + capture('user_logged_out') logout_user() return redirect(url_for('main.index')) @@ -46,6 +55,10 @@ def register(): user.set_password(form.password.data) db.session.add(user) db.session.commit() + with new_context(): + identify_context(user.username) + tag('username', user.username) + capture('user_signed_up', properties={'signup_method': 'form'}) flash(_('Congratulations, you are now a registered user!')) return redirect(url_for('auth.login')) return render_template('auth/register.html', title=_('Register'), @@ -62,6 +75,9 @@ def reset_password_request(): sa.select(User).where(User.email == form.email.data)) if user: send_password_reset_email(user) + with new_context(): + identify_context(user.username) + capture('password_reset_requested') flash( _('Check your email for the instructions to reset your password')) return redirect(url_for('auth.login')) @@ -80,6 +96,9 @@ def reset_password(token): if form.validate_on_submit(): user.set_password(form.password.data) db.session.commit() + with new_context(): + identify_context(user.username) + capture('password_reset_completed') flash(_('Your password has been reset.')) return redirect(url_for('auth.login')) return render_template('auth/reset_password.html', form=form) diff --git a/apps/basic-integration/flask/flask3-social-media/app/main/routes.py b/apps/basic-integration/flask/flask3-social-media/app/main/routes.py index 103bbc7bc..11b64208c 100644 --- a/apps/basic-integration/flask/flask3-social-media/app/main/routes.py +++ b/apps/basic-integration/flask/flask3-social-media/app/main/routes.py @@ -5,6 +5,7 @@ from flask_babel import _, get_locale import sqlalchemy as sa from langdetect import detect, LangDetectException +from posthog import capture, identify_context, new_context from app import db from app.main.forms import EditProfileForm, EmptyForm, PostForm, SearchForm, \ MessageForm @@ -36,6 +37,12 @@ def index(): language=language) db.session.add(post) db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('post_created', properties={ + 'post_length': len(form.post.data), + 'language': language or 'unknown', + }) flash(_('Your post is now live!')) return redirect(url_for('main.index')) page = request.args.get('page', 1, type=int) @@ -102,6 +109,11 @@ def edit_profile(): current_user.username = form.username.data current_user.about_me = form.about_me.data db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('profile_updated', properties={ + 'has_about_me': bool(current_user.about_me), + }) flash(_('Your changes have been saved.')) return redirect(url_for('main.edit_profile')) elif request.method == 'GET': @@ -126,6 +138,9 @@ def follow(username): return redirect(url_for('main.user', username=username)) current_user.follow(user) db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('user_followed') flash(_('You are following %(username)s!', username=username)) return redirect(url_for('main.user', username=username)) else: @@ -147,6 +162,9 @@ def unfollow(username): return redirect(url_for('main.user', username=username)) current_user.unfollow(user) db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('user_unfollowed') flash(_('You are not following %(username)s.', username=username)) return redirect(url_for('main.user', username=username)) else: @@ -157,6 +175,12 @@ def unfollow(username): @login_required def translate_text(): data = request.get_json() + with new_context(): + identify_context(current_user.username) + capture('post_translated', properties={ + 'source_language': data.get('source_language', 'unknown'), + 'dest_language': data.get('dest_language', 'unknown'), + }) return {'text': translate(data['text'], data['source_language'], data['dest_language'])} @@ -174,6 +198,12 @@ def search(): if total > page * current_app.config['POSTS_PER_PAGE'] else None prev_url = url_for('main.search', q=g.search_form.q.data, page=page - 1) \ if page > 1 else None + with new_context(): + identify_context(current_user.username) + capture('post_searched', properties={ + 'results_count': total, + 'page': page, + }) return render_template('search.html', title=_('Search'), posts=posts, next_url=next_url, prev_url=prev_url) @@ -190,6 +220,11 @@ def send_message(recipient): user.add_notification('unread_message_count', user.unread_message_count()) db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('message_sent', properties={ + 'message_length': len(form.message.data), + }) flash(_('Your message has been sent.')) return redirect(url_for('main.user', username=recipient)) return render_template('send_message.html', title=_('Send Message'), @@ -224,6 +259,9 @@ def export_posts(): else: current_user.launch_task('export_posts', _('Exporting posts...')) db.session.commit() + with new_context(): + identify_context(current_user.username) + capture('posts_export_started') return redirect(url_for('main.user', username=current_user.username)) diff --git a/apps/basic-integration/flask/flask3-social-media/config.py b/apps/basic-integration/flask/flask3-social-media/config.py index 7e50c9ee7..356d5c0a7 100644 --- a/apps/basic-integration/flask/flask3-social-media/config.py +++ b/apps/basic-integration/flask/flask3-social-media/config.py @@ -23,3 +23,5 @@ class Config: ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL') REDIS_URL = os.environ.get('REDIS_URL') or 'redis://' POSTS_PER_PAGE = 25 + POSTHOG_PROJECT_TOKEN = os.environ.get('POSTHOG_PROJECT_TOKEN') + POSTHOG_HOST = os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com') diff --git a/apps/basic-integration/flask/flask3-social-media/posthog-setup-report.md b/apps/basic-integration/flask/flask3-social-media/posthog-setup-report.md new file mode 100644 index 000000000..93e637178 --- /dev/null +++ b/apps/basic-integration/flask/flask3-social-media/posthog-setup-report.md @@ -0,0 +1,41 @@ + +# PostHog post-wizard report + +The wizard has completed a deep integration of PostHog analytics into this Flask social media (microblog) application. PostHog is now initialized globally in `create_app()`, and 15 events covering key user actions — authentication, social interactions, content creation, messaging, and API usage — are captured across 5 files. + +| Event Name | Description | File | +|---|---|---| +| `user_signed_up` | A new user completed registration via the signup form. | `app/auth/routes.py` | +| `user_logged_in` | A user successfully authenticated and logged in. | `app/auth/routes.py` | +| `user_logged_out` | A user logged out of their session. | `app/auth/routes.py` | +| `password_reset_requested` | A user submitted a request to reset their password. | `app/auth/routes.py` | +| `password_reset_completed` | A user successfully reset their password via the reset token link. | `app/auth/routes.py` | +| `post_created` | A user published a new post to their feed. | `app/main/routes.py` | +| `user_followed` | A user followed another user on the platform. | `app/main/routes.py` | +| `user_unfollowed` | A user unfollowed another user on the platform. | `app/main/routes.py` | +| `profile_updated` | A user saved changes to their profile information. | `app/main/routes.py` | +| `message_sent` | A user sent a private message to another user. | `app/main/routes.py` | +| `post_searched` | A user performed a search query across posts. | `app/main/routes.py` | +| `post_translated` | A user requested a translation of a post. | `app/main/routes.py` | +| `posts_export_started` | A user triggered a background task to export all their posts. | `app/main/routes.py` | +| `api_user_created` | A new user account was created via the REST API. | `app/api/users.py` | +| `api_token_issued` | An API authentication token was issued to a user. | `app/api/tokens.py` | + +## Next steps + +We've built some insights and a dashboard to keep an eye on user behavior: + +- [Analytics basics (wizard) dashboard](https://us.i.posthog.com/project/2/dashboard/1720023) + +## Verify before merging + +- [ ] Run a full production build (the wizard only verified the files it touched) and fix any lint or type errors introduced by the generated code. +- [ ] Run the test suite — call sites that were rewritten or instrumented may need updated mocks or fixtures. +- [ ] Add `POSTHOG_PROJECT_TOKEN` and `POSTHOG_HOST` to `.env.example` and any monorepo/bootstrap scripts so collaborators know what to set. +- [ ] Confirm the returning-visitor path also calls `identify` — the current implementation identifies on login and signup, but sessions that are restored from cookies (i.e., `remember_me=True`) skip the `identify` call on subsequent visits. + +### Agent skill + +We've left an agent skill folder in your project. You can use this context for further agent development when using Claude Code. This will help ensure the model provides the most up-to-date approaches for integrating PostHog. + + diff --git a/apps/basic-integration/flask/flask3-social-media/requirements.txt b/apps/basic-integration/flask/flask3-social-media/requirements.txt index 0d986effd..7cf0ed960 100644 --- a/apps/basic-integration/flask/flask3-social-media/requirements.txt +++ b/apps/basic-integration/flask/flask3-social-media/requirements.txt @@ -1,4 +1,5 @@ alembic +posthog Babel email-validator Flask>=3.0