Flask

Last updated:

|Edit this page

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.

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
package main
from flask import Flask, render_template, request, redirect, session, url_for
from posthog import Posthog
posthog = Posthog(
'<ph_project_api_key>',
host='https://us.i.posthog.com'
)
@app.route('/api/dashboard', methods=['POST'])
def api_dashboard():
posthog.capture(
'distinct_id_of_your_user',
'dashboard_api_called'
)
return '', 204

You can find your project API key and instance address in your project settings.

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.

Alternatively, the following tutorials can help you get started:

Questions?

Was this page useful?

Next article

Framer

PostHog makes it easy to get data about traffic and usage of your Framer app. Integrating PostHog into your site enables analytics about user behavior, custom events capture, session recordings, feature flags, and more. This guide walks you through integrating PostHog into your Framer app using the JavaScript Web SDK . Installation Capture custom events To capture custom events , you call posthog.capture() using custom code components in Framer . Go to the Assets tab in the top left of…

Read next article