From 793d736d1018f3f1ea80d9104584ffdfd2951c75 Mon Sep 17 00:00:00 2001 From: edwinyjlim Date: Tue, 20 Jan 2026 17:47:58 -0500 Subject: [PATCH] wizard-ci: flask/flask3-social-media --- .../.claude/skills/flask/SKILL.md | 51 + .../skills/flask/references/EXAMPLE.md | 1178 +++++++++++++++++ .../references/basic-integration-1.0-begin.md | 43 + .../references/basic-integration-1.1-edit.md | 37 + .../basic-integration-1.2-revise.md | 22 + .../basic-integration-1.3-conclude.md | 36 + .../.claude/skills/flask/references/flask.md | 75 ++ .../skills/flask/references/identify-users.md | 202 +++ .../flask/flask3-social-media/app/__init__.py | 40 +- .../flask3-social-media/app/api/tokens.py | 12 +- .../flask3-social-media/app/api/users.py | 12 +- .../flask3-social-media/app/auth/routes.py | 42 +- .../flask3-social-media/app/main/routes.py | 66 + apps/flask/flask3-social-media/config.py | 4 + .../posthog-setup-report.md | 67 + .../flask3-social-media/requirements.txt | 1 + 16 files changed, 1883 insertions(+), 5 deletions(-) create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/SKILL.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/EXAMPLE.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.0-begin.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.1-edit.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.2-revise.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.3-conclude.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/flask.md create mode 100644 apps/flask/flask3-social-media/.claude/skills/flask/references/identify-users.md create mode 100644 apps/flask/flask3-social-media/posthog-setup-report.md diff --git a/apps/flask/flask3-social-media/.claude/skills/flask/SKILL.md b/apps/flask/flask3-social-media/.claude/skills/flask/SKILL.md new file mode 100644 index 00000000..d3d30958 --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/SKILL.md @@ -0,0 +1,51 @@ +--- +name: 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. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here** +2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit +3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise +4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion + +## Reference files + +- `EXAMPLE.md` - Flask example project code +- `flask.md` - Flask - docs +- `identify-users.md` - Identify users - docs +- `basic-integration-1.0-begin.md` - PostHog setup - begin +- `basic-integration-1.1-edit.md` - PostHog setup - edit +- `basic-integration-1.2-revise.md` - PostHog setup - revise +- `basic-integration-1.3-conclude.md` - PostHog setup - conclusion + +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 + +- Remember that source code is available in the venv/site-packages directory +- posthog is the Python SDK package name + +## 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/flask/flask3-social-media/.claude/skills/flask/references/EXAMPLE.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/EXAMPLE.md new file mode 100644 index 00000000..f14c79ce --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/EXAMPLE.md @@ -0,0 +1,1178 @@ +# PostHog Flask Example Project + +Repository: https://github.com/PostHog/examples +Path: basics/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 and exception capture +- Group analytics + +## 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 +Exceptions are captured for monitoring: +```python +posthog.capture_exception(exception) +``` + +### Group Analytics +Company-level analytics tracking: +```python +posthog.group_identify('company', 'acme-corp', { + 'name': 'Acme Corporation', + 'plan': 'enterprise' +}) + +with new_context(): + identify_context(current_user.email) + capture('feature_used', properties={'feature_name': 'analytics'}, + groups={'company': 'acme-corp'}) +``` + +## 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 +``` + +## Key Differences from Django Version + +| Aspect | Django | Flask | +|--------|--------|-------| +| Project Structure | Single app in project | Application factory + blueprints | +| Database | SQLite via Django ORM | SQLite via Flask-SQLAlchemy | +| User Model | Built-in `auth.User` model | Custom SQLAlchemy `User` model | +| User Registration | Django admin / `createsuperuser` | `/signup` route with form | +| Authentication | Django auth system | Flask-Login | +| Session Management | Django sessions | Flask sessions (cookie-based) | +| Configuration | settings.py | Config classes with app factory | +| URL Routing | urls.py patterns | Blueprint route decorators | +| PostHog Init | AppConfig.ready() | Application factory | +| Error Capture | PostHog middleware auto-captures | Requires global `@app.errorhandler(Exception)` | + +--- + +## .env.example + +```example +POSTHOG_API_KEY= +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, jsonify +from flask_login import current_user +from posthog import identify_context, new_context + +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_API_KEY"] + 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) + + # Global error handler for PostHog exception capture + # Flask's built-in error handlers bypass PostHog's default autocapture, + # so we need to manually capture exceptions here + @app.errorhandler(Exception) + def handle_exception(e): + # Capture the exception in PostHog and get the event UUID + # This UUID can be shown to users for bug reports + # Use context to attribute exception to the current user + with new_context(): + if current_user.is_authenticated: + identify_context(current_user.email) + event_id = posthog.capture_exception(e) + + # For API routes, return JSON error response + if hasattr(e, "code"): + status_code = e.code + else: + status_code = 500 + + return ( + jsonify( + { + "error": str(e), + "error_id": event_id, + "message": "An error occurred. Reference ID: " + + (event_id or "unknown"), + } + ), + status_code, + ) + + # 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("/trigger-error", methods=["POST"]) +@login_required +def trigger_error(): + """Demonstrate error tracking.""" + error_type = request.form.get("error_type", "generic") + + try: + if error_type == "value": + raise ValueError("Invalid value provided by user") + elif error_type == "key": + data = {} + _ = data["nonexistent_key"] + else: + raise Exception("A generic error occurred") + except Exception as e: + # PostHog: Capture exception and track error event with user context + with new_context(): + identify_context(current_user.email) + posthog.capture_exception(e) + capture( + "error_triggered", + properties={"error_type": error_type, "error_message": str(e)}, + ) + + return ( + jsonify( + { + "success": False, + "error": "An error occurred", + "message": "Error has been captured by PostHog", + } + ), + 400, + ) + + return jsonify({"success": True}) + + +@api_bp.route("/group-analytics", methods=["GET", "POST"]) +@login_required +def group_analytics(): + """Demonstrate group analytics.""" + # PostHog: Group identify + posthog.group_identify( + "company", + "acme-corp", + {"name": "Acme Corporation", "plan": "enterprise", "employee_count": 500}, + ) + + # Capture event with group association + with new_context(): + identify_context(current_user.email) + capture( + "feature_used", + properties={"feature_name": "group_analytics"}, + groups={"company": "acme-corp"}, + ) + + return jsonify({"success": True, "message": "Group analytics event captured"}) + +``` + +--- + +## 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_API_KEY = os.environ.get("POSTHOG_API_KEY", "") + 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 = "core.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/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/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.0-begin.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.0-begin.md new file mode 100644 index 00000000..953ead2d --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.0-begin.md @@ -0,0 +1,43 @@ +--- +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. + +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. + +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: event name, event description, and the file path we want to place the event in. 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 + + +--- + +**Upon completion, continue with:** [basic-integration-1.1-edit.md](basic-integration-1.1-edit.md) \ No newline at end of file diff --git a/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.1-edit.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.1-edit.md new file mode 100644 index 00000000..44c1a4e9 --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.1-edit.md @@ -0,0 +1,37 @@ +--- +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. + +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:** [basic-integration-1.2-revise.md](basic-integration-1.2-revise.md) \ No newline at end of file diff --git a/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.2-revise.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.2-revise.md new file mode 100644 index 00000000..26f3a60e --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.2-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. + +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. + +## 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:** [basic-integration-1.3-conclude.md](basic-integration-1.3-conclude.md) \ No newline at end of file diff --git a/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.3-conclude.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.3-conclude.md new file mode 100644 index 00000000..552118fb --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/basic-integration-1.3-conclude.md @@ -0,0 +1,36 @@ +--- +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" based on the events created here. 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. + +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, along with a list of links for the dashboard and insights created. 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] + +### 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. + + + +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/flask/flask3-social-media/.claude/skills/flask/references/flask.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/flask.md new file mode 100644 index 00000000..aab32585 --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/flask.md @@ -0,0 +1,75 @@ +# 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, 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. + +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 +package main +from flask import Flask, render_template, request, redirect, session, url_for +from posthog import Posthog +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 API key and instance address in [your project settings](https://us.posthog.com/project/settings). + +## 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 +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/flask/flask3-social-media/.claude/skills/flask/references/identify-users.md b/apps/flask/flask3-social-media/.claude/skills/flask/references/identify-users.md new file mode 100644 index 00000000..ce165453 --- /dev/null +++ b/apps/flask/flask3-social-media/.claude/skills/flask/references/identify-users.md @@ -0,0 +1,202 @@ +# 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#capturing-events.md) or [mobile app](/docs/libraries/ios#capturing-events.md), 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#capturing-anonymous-events.md). + +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 +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 user is already authenticated on the mobile app. In this case, call [`posthog.alias()`](/docs/libraries/js/features#alias.md) with the distinct ID from the web. This associates the two distinct IDs as a single person. +- The user is unauthenticated. In this case, call [`posthog.identify()`](/docs/libraries/js/features#identifying-users.md) with the distinct ID from the web. Events will be associated with this distinct ID. + +As long as you associate the distinct IDs with `posthog.identify()` or `posthog.alias()`, you can track events generated across platforms. + +## Further reading + +- [Identifying users docs](/docs/product-analytics/identify.md) +- [How person processing works](/docs/how-posthog-works/ingestion-pipeline#2-person-processing.md) +- [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/flask/flask3-social-media/app/__init__.py b/apps/flask/flask3-social-media/app/__init__.py index f15c103d..be9da1d7 100644 --- a/apps/flask/flask3-social-media/app/__init__.py +++ b/apps/flask/flask3-social-media/app/__init__.py @@ -1,13 +1,15 @@ import logging from logging.handlers import SMTPHandler, RotatingFileHandler import os -from flask import Flask, request, current_app +from flask import Flask, request, current_app, jsonify from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate -from flask_login import LoginManager +from flask_login import LoginManager, current_user from flask_mail import Mail from flask_moment import Moment from flask_babel import Babel, lazy_gettext as _l +import posthog +from posthog import identify_context, new_context try: from elasticsearch import Elasticsearch except ImportError: @@ -54,6 +56,40 @@ def create_app(config_class=Config): app.redis = None app.task_queue = None + # Initialize PostHog + if not app.config.get('POSTHOG_DISABLED') and app.config.get('POSTHOG_API_KEY'): + posthog.api_key = app.config['POSTHOG_API_KEY'] + posthog.host = app.config['POSTHOG_HOST'] + posthog.debug = app.debug + + # Global error handler for PostHog exception capture + # Flask's built-in error handlers bypass PostHog's default autocapture, + # so we need to manually capture exceptions here + @app.errorhandler(Exception) + def handle_exception(e): + # Capture the exception in PostHog and get the event UUID + event_id = None + if not app.config.get('POSTHOG_DISABLED') and app.config.get('POSTHOG_API_KEY'): + with new_context(): + if current_user.is_authenticated: + identify_context(str(current_user.id)) + event_id = posthog.capture_exception(e) + + # For API routes, return JSON error response + if hasattr(e, 'code'): + status_code = e.code + else: + status_code = 500 + + return ( + jsonify({ + 'error': str(e), + 'error_id': event_id, + 'message': 'An error occurred. Reference ID: ' + (event_id or 'unknown'), + }), + status_code, + ) + from app.errors import bp as errors_bp app.register_blueprint(errors_bp) diff --git a/apps/flask/flask3-social-media/app/api/tokens.py b/apps/flask/flask3-social-media/app/api/tokens.py index 29a82a40..a7b1e642 100644 --- a/apps/flask/flask3-social-media/app/api/tokens.py +++ b/apps/flask/flask3-social-media/app/api/tokens.py @@ -1,3 +1,5 @@ +from flask import current_app +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 +8,16 @@ @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() + + # PostHog: Capture API token generated event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + capture('api_token_generated') + return {'token': token} diff --git a/apps/flask/flask3-social-media/app/api/users.py b/apps/flask/flask3-social-media/app/api/users.py index 8a0b59e5..bc94863e 100644 --- a/apps/flask/flask3-social-media/app/api/users.py +++ b/apps/flask/flask3-social-media/app/api/users.py @@ -1,5 +1,6 @@ import sqlalchemy as sa -from flask import request, url_for, abort +from flask import request, url_for, abort, current_app +from posthog import capture, identify_context, new_context, tag from app import db from app.models import User from app.api import bp @@ -57,6 +58,15 @@ def create_user(): user.from_dict(data, new_user=True) db.session.add(user) db.session.commit() + + # PostHog: Identify new user and capture API user created event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + tag('email', user.email) + tag('username', 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/flask/flask3-social-media/app/auth/routes.py b/apps/flask/flask3-social-media/app/auth/routes.py index 79360ff7..09280c59 100644 --- a/apps/flask/flask3-social-media/app/auth/routes.py +++ b/apps/flask/flask3-social-media/app/auth/routes.py @@ -1,8 +1,9 @@ -from flask import render_template, redirect, url_for, flash, request +from flask import render_template, redirect, url_for, flash, request, current_app from urllib.parse import urlsplit 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,15 @@ def login(): flash(_('Invalid username or password')) return redirect(url_for('auth.login')) login_user(user, remember=form.remember_me.data) + + # PostHog: Identify user and capture login event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + tag('email', user.email) + 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 +42,13 @@ def login(): @bp.route('/logout') def logout(): + # PostHog: Capture logout event before session ends + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + if current_user.is_authenticated: + with new_context(): + identify_context(str(current_user.id)) + capture('user_logged_out') + logout_user() return redirect(url_for('main.index')) @@ -46,6 +63,15 @@ def register(): user.set_password(form.password.data) db.session.add(user) db.session.commit() + + # PostHog: Identify new user and capture signup event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + tag('email', user.email) + 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 +88,13 @@ def reset_password_request(): sa.select(User).where(User.email == form.email.data)) if user: send_password_reset_email(user) + + # PostHog: Capture password reset request event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + capture('password_reset_requested') + flash( _('Check your email for the instructions to reset your password')) return redirect(url_for('auth.login')) @@ -80,6 +113,13 @@ def reset_password(token): if form.validate_on_submit(): user.set_password(form.password.data) db.session.commit() + + # PostHog: Capture password reset completed event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(user.id)) + 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/flask/flask3-social-media/app/main/routes.py b/apps/flask/flask3-social-media/app/main/routes.py index 103bbc7b..07909f0f 100644 --- a/apps/flask/flask3-social-media/app/main/routes.py +++ b/apps/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,16 @@ def index(): language=language) db.session.add(post) db.session.commit() + + # PostHog: Capture post created event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + 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 +113,13 @@ def edit_profile(): current_user.username = form.username.data current_user.about_me = form.about_me.data db.session.commit() + + # PostHog: Capture profile updated event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('profile_updated') + flash(_('Your changes have been saved.')) return redirect(url_for('main.edit_profile')) elif request.method == 'GET': @@ -126,6 +144,16 @@ def follow(username): return redirect(url_for('main.user', username=username)) current_user.follow(user) db.session.commit() + + # PostHog: Capture user followed event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('user_followed', properties={ + 'followed_user_id': str(user.id), + 'followed_username': user.username + }) + flash(_('You are following %(username)s!', username=username)) return redirect(url_for('main.user', username=username)) else: @@ -147,6 +175,16 @@ def unfollow(username): return redirect(url_for('main.user', username=username)) current_user.unfollow(user) db.session.commit() + + # PostHog: Capture user unfollowed event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('user_unfollowed', properties={ + 'unfollowed_user_id': str(user.id), + 'unfollowed_username': user.username + }) + flash(_('You are not following %(username)s.', username=username)) return redirect(url_for('main.user', username=username)) else: @@ -170,6 +208,17 @@ def search(): page = request.args.get('page', 1, type=int) posts, total = Post.search(g.search_form.q.data, page, current_app.config['POSTS_PER_PAGE']) + + # PostHog: Capture search performed event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('search_performed', properties={ + 'query_length': len(g.search_form.q.data), + 'results_count': total, + 'page': page + }) + next_url = url_for('main.search', q=g.search_form.q.data, page=page + 1) \ 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) \ @@ -190,6 +239,16 @@ def send_message(recipient): user.add_notification('unread_message_count', user.unread_message_count()) db.session.commit() + + # PostHog: Capture message sent event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('message_sent', properties={ + 'recipient_id': str(user.id), + '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 +283,13 @@ def export_posts(): else: current_user.launch_task('export_posts', _('Exporting posts...')) db.session.commit() + + # PostHog: Capture posts exported event + if not current_app.config.get('POSTHOG_DISABLED') and current_app.config.get('POSTHOG_API_KEY'): + with new_context(): + identify_context(str(current_user.id)) + capture('posts_exported') + return redirect(url_for('main.user', username=current_user.username)) diff --git a/apps/flask/flask3-social-media/config.py b/apps/flask/flask3-social-media/config.py index 7e50c9ee..b9829730 100644 --- a/apps/flask/flask3-social-media/config.py +++ b/apps/flask/flask3-social-media/config.py @@ -23,3 +23,7 @@ class Config: ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL') REDIS_URL = os.environ.get('REDIS_URL') or 'redis://' POSTS_PER_PAGE = 25 + # PostHog configuration + POSTHOG_API_KEY = os.environ.get('POSTHOG_API_KEY') + POSTHOG_HOST = os.environ.get('POSTHOG_HOST', 'https://us.i.posthog.com') + POSTHOG_DISABLED = os.environ.get('POSTHOG_DISABLED', 'False').lower() == 'true' diff --git a/apps/flask/flask3-social-media/posthog-setup-report.md b/apps/flask/flask3-social-media/posthog-setup-report.md new file mode 100644 index 00000000..36401904 --- /dev/null +++ b/apps/flask/flask3-social-media/posthog-setup-report.md @@ -0,0 +1,67 @@ +# PostHog post-wizard report + +The wizard has completed a deep integration of PostHog analytics into your Flask Microblog application. The integration includes comprehensive server-side event tracking for user authentication, social interactions, content creation, and API activity. PostHog has been initialized in the application factory with automatic exception capture via a global error handler. + +## Summary of Changes + +### Files Modified + +| File | Changes | +|------|---------| +| `config.py` | Added PostHog configuration variables (API key, host, disabled flag) | +| `app/__init__.py` | Added PostHog initialization and global exception handler | +| `app/auth/routes.py` | Added authentication event tracking | +| `app/main/routes.py` | Added user engagement event tracking | +| `app/api/users.py` | Added API user creation event tracking | +| `app/api/tokens.py` | Added API token generation event tracking | +| `requirements.txt` | Added `posthog>=3.0.0` dependency | +| `.env` | Created with PostHog API key and host configuration | + +### Events Implemented + +| Event Name | Description | File Path | +|------------|-------------|-----------| +| `user_signed_up` | Fired when a new user successfully registers an account | `app/auth/routes.py` | +| `user_logged_in` | Fired when a user successfully logs in | `app/auth/routes.py` | +| `user_logged_out` | Fired when a user logs out | `app/auth/routes.py` | +| `password_reset_requested` | Fired when a user requests a password reset email | `app/auth/routes.py` | +| `password_reset_completed` | Fired when a user successfully resets their password | `app/auth/routes.py` | +| `post_created` | Fired when a user creates a new post/microblog entry | `app/main/routes.py` | +| `user_followed` | Fired when a user follows another user | `app/main/routes.py` | +| `user_unfollowed` | Fired when a user unfollows another user | `app/main/routes.py` | +| `message_sent` | Fired when a user sends a private message to another user | `app/main/routes.py` | +| `profile_updated` | Fired when a user updates their profile information | `app/main/routes.py` | +| `search_performed` | Fired when a user performs a search query | `app/main/routes.py` | +| `posts_exported` | Fired when a user initiates a post export task | `app/main/routes.py` | +| `api_user_created` | Fired when a new user is created via API | `app/api/users.py` | +| `api_token_generated` | Fired when a user generates an API authentication token | `app/api/tokens.py` | + +## 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: + +### Dashboard +- [Analytics basics](https://us.posthog.com/project/2/dashboard/1094501) - Core analytics dashboard for tracking user engagement and authentication + +### Insights +- [User Authentication Activity](https://us.posthog.com/project/2/insights/hnJjhUHO) - Tracks sign ups, logins, and logouts over time +- [User Engagement Activity](https://us.posthog.com/project/2/insights/YlC57M40) - Tracks posts created, follows, unfollows, and messages sent +- [Signup to First Post Funnel](https://us.posthog.com/project/2/insights/tTtwASHI) - Conversion funnel from user signup to creating their first post +- [Search & Profile Activity](https://us.posthog.com/project/2/insights/dh2Ypjtp) - Tracks search and profile-related activity +- [API Activity](https://us.posthog.com/project/2/insights/nYgcb5Mf) - Tracks API-related events: user creation and token generation + +## Agent skill + +We've left an agent skill folder in your project at `.claude/skills/flask/`. 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. + +## Environment Variables + +The following environment variables have been configured in `.env`: + +``` +POSTHOG_API_KEY=sTMFPsFhdP1Ssg +POSTHOG_HOST=https://us.i.posthog.com +POSTHOG_DISABLED=False +``` + +To disable PostHog tracking (e.g., in development), set `POSTHOG_DISABLED=True`. diff --git a/apps/flask/flask3-social-media/requirements.txt b/apps/flask/flask3-social-media/requirements.txt index 0d986eff..7d0e82ba 100644 --- a/apps/flask/flask3-social-media/requirements.txt +++ b/apps/flask/flask3-social-media/requirements.txt @@ -12,6 +12,7 @@ Flask-SQLAlchemy Flask-WTF gunicorn langdetect +posthog>=3.0.0 PyJWT python-dotenv redis