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 Go to your project settings and copy your web snippet. It looks like this: With the snippet copied, go to your Framer project settings by clicking the gear in…

Read next article