Hero
Back to all guides

How to add analytics to Python

Add server-side analytics to your Python application with OpenPanel's Python SDK.

OpenPanel Team

12/15/2025

Beginner
7 min

How to add analytics to Python

This guide walks you through adding server-side analytics to any Python application. You'll install the OpenPanel SDK, configure it with your credentials, track custom events, and identify users.

Server-side tracking gives you complete control over what data you collect and ensures events are never blocked by browser extensions or ad blockers. The Python SDK works with Django, Flask, FastAPI, and any other Python framework or script.

Prerequisites

  • A Python project
  • An OpenPanel account
  • Your Client ID and Client Secret from the dashboard

Install the SDK

Start by installing the OpenPanel package from PyPI.

pip install openpanel

If you're using Poetry, you can run poetry add openpanel instead.

Initialize OpenPanel

Create a shared module for your OpenPanel instance. This approach lets you import the same configured instance throughout your application.

# lib/op.py
import os
from openpanel import OpenPanel

op = OpenPanel(
    client_id=os.getenv("OPENPANEL_CLIENT_ID"),
    client_secret=os.getenv("OPENPANEL_CLIENT_SECRET")
)

Server-side tracking requires both a client ID and client secret for authentication. Add these to your environment variables.

# .env
OPENPANEL_CLIENT_ID=your-client-id
OPENPANEL_CLIENT_SECRET=your-client-secret

You can also pass global properties during initialization. These properties are included with every event automatically.

op = OpenPanel(
    client_id=os.getenv("OPENPANEL_CLIENT_ID"),
    client_secret=os.getenv("OPENPANEL_CLIENT_SECRET"),
    global_properties={
        "app_version": "1.0.0",
        "environment": os.getenv("ENVIRONMENT", "production")
    }
)

Track events

Use the track method to record events. The first argument is the event name, and the second is an optional dictionary of properties.

from lib.op import op

# Track a simple event
op.track("button_clicked")

# Track with properties
op.track("purchase_completed", {
    "product_id": "123",
    "price": 99.99,
    "currency": "USD"
})

When tracking events in request handlers, you'll typically pull data from the request and track it alongside your business logic. Here's an example in a Django view.

from lib.op import op

def signup_view(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        name = request.POST.get('name')
        
        user = create_user(email, name)
        
        op.track("user_signed_up", {
            "email": email,
            "source": "website"
        })
        
        return JsonResponse({"success": True})

The same pattern works in Flask and FastAPI. Import your OpenPanel instance and call track wherever you need to record an event.

You can also track events from background tasks. This is useful for monitoring async jobs, email delivery, and scheduled tasks.

from celery import shared_task
from lib.op import op

@shared_task
def send_email_task(user_id, email_type):
    try:
        send_email(user_id, email_type)
        op.track("email_sent", {
            "user_id": user_id,
            "email_type": email_type
        })
    except Exception as e:
        op.track("email_failed", {
            "user_id": user_id,
            "error": str(e)
        })

Identify users

The identify method associates a user profile with their ID. Call this after authentication to link subsequent events to that user.

from lib.op import op

def login_view(request):
    user = authenticate_user(request)
    
    op.identify(user.id, {
        "firstName": user.first_name,
        "lastName": user.last_name,
        "email": user.email,
        "tier": user.plan
    })
    
    op.track("user_logged_in", {"method": "email"})
    
    return JsonResponse({"success": True})

To track an event for a specific user without calling identify first, pass the profile_id parameter to the track method.

op.track("purchase_completed", {
    "product_id": "123",
    "amount": 99.99
}, profile_id="user_456")

You can increment numeric properties on user profiles. This is useful for counters like login count or total purchases.

op.increment({
    "profile_id": "user_456",
    "property": "login_count",
    "value": 1
})

Verify your setup

Run your Python application and trigger a few events. Open your OpenPanel dashboard and navigate to the real-time view. You should see your events appearing within seconds.

If events aren't showing up, check that your client ID and client secret are correct. Server-side tracking won't work without the client secret. Review your application logs for any error messages from the SDK.

Next steps

The Python SDK reference covers additional configuration options like event filtering and disabling tracking. If you're also tracking client-side events, you might want to read about cookieless analytics to understand how OpenPanel handles privacy without cookies.

Loved by builders everywhere

From indie hackers to global teams, OpenPanel helps people understand their users effortlessly.

  • Steven Tey
    Steven Tey
    @steventey

    Open-source Mixpanel alternative just dropped → http://git.new/openpanel

    It combines the power of Mixpanel + the ease of use of @PlausibleHQ into a fully open-source product.

    Built by @CarlLindesvard and it’s already tracking 750K+ events 🤩

  • Pontus Abrahamsson - oss/acc
    Pontus Abrahamsson - oss/acc
    @pontusab

    Thanks, OpenPanel is a beast, love it!

  • Piotr Kulpinski
    Piotr Kulpinski
    @piotrkulpinski

    The Overview tab in OpenPanel is great. It has everything I need from my analytics: the stats, the graph, traffic sources, locations, devices, etc.

    The UI is beautiful ✨ Clean, modern look, very pleasing to the eye.

  • greg hodson 🍜
    greg hodson 🍜
    @h0dson

    i second this, openpanel is killing it

  • Jacob 🍀 Build in Public
    Jacob 🍀 Build in Public
    @javayhuwx

    🤯 wow, it's amazing! Just integrate @OpenPanelDev into http://indiehackers.site last night, and now I can see visitors coming from all round the world.

    OpenPanel has a more beautiful UI and much more powerful features when compared to Umami.

    #buildinpublic #indiehackers

  • Lee
    Lee
    @DutchEngIishman

    Day two of marketing.

    I like this upward trend..

    P.S. website went live on Sunday

    P.P.S. Openpanel by @CarlLindesvard is awesome.

Ready to understand your users better?
Start tracking in minutes

Join thousands of companies using OpenPanel. Free 30-day trial, no credit card required. Self-host for free or use our cloud.

Get started now