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

2025-12-15

Updated on 2026-02-07

Beginner
7 min

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.

What our customers say

  • After paying a lot to PostHog for years, OpenPanel gives us the same, in many ways better, analytics while keeping full ownership of our data. It's truly self-hosted but surprisingly low maintenance. Setup and ongoing upkeep are straightforward, so we spend less time managing tooling and more time acting on insights. The support is fantastic. We honestly don't want to run any business without OpenPanel anymore.
    ETEngineering teamSelf-hosted, switched from PostHog
  • After testing several product analytics tools for Strackr, we chose OpenPanel and we are very satisfied with the product. We have been using it since the beta, and there are constant updates. Profiles and conversion events are our favourite features.
    JHJulien HanyFounder, Strackr
  • Before OpenPanel I was using Plausible, and it was ok. But OpenPanel is like 10 leagues ahead. Better UX and UI, many more features, and incredible support from the founder. Bonus point: one-click install on Coolify. I won't switch to anything else.
    Thomas SanlisFounder, Uneed
  • The Overview tab has everything I need from my analytics: the stats, the graph, traffic sources, locations, devices. The UI is beautiful. Clean, modern look, very pleasing to the eye.
    Piotr KulpinskiFounder, Kulpinski
  • It combines the power of Mixpanel with the ease of use of Plausible into a fully open-source product.
    Steven TeyFounder, Dub
  • Integrated OpenPanel last night, and now I can see visitors coming from all around the world. More beautiful UI and much more powerful than Umami.
    JacobIndie hacker

Start understanding your users today

One script on your site, or one SDK call in your app, and the first events show up within a minute. 30-day free trial, no credit card.

Cloud from $2.50 a month, or self-host for free.