Amplitude Python SDK

1.2.3 · active · verified Thu Apr 09

The official Amplitude backend Python SDK provides server-side instrumentation for tracking analytics events. Currently at version 1.2.3, it offers functionalities to send events, manage user properties, and track revenue, with regular patch and minor releases to enhance stability and features.

Warnings

Install

Imports

Quickstart

Initializes the Amplitude SDK with an API key (preferably from environment variables), tracks a custom event, and sets user properties. It emphasizes the importance of calling `flush()` to ensure events are sent before the application terminates, and shows how to configure for EU data residency.

import os
from amplitude import Amplitude, BaseEvent, Identify
import time

AMPLITUDE_API_KEY = os.environ.get("AMPLITUDE_API_KEY", "YOUR_AMPLITUDE_API_KEY")

# Initialize the SDK
# For EU data residency, add: server_zone='EU' to Amplitude(API_KEY, ...)
amplitude = Amplitude(AMPLITUDE_API_KEY)

# Example: Configure for EU data residency (uncomment if applicable)
# amplitude.configuration.server_zone = 'EU'

# Create and track a basic event
print("Tracking a 'User Logged In' event...")
event = BaseEvent(
    event_type="User Logged In",
    user_id="user_123",
    device_id="device_abc",
    event_properties={
        "login_method": "email",
        "timestamp": int(time.time())
    }
)
amplitude.track(event)

# Identify a user and set/update user properties
print("Setting user properties for 'user_123'...")
identify = Identify()
identify.set("plan", "premium")
identify.set_once("initial_referrer", "google") # Set only once
identify.add("login_count", 1) # Increment a property
amplitude.identify("user_123", identify)

# Important: Flush events before application exit to ensure delivery
print("Flushing events...")
amplitude.flush()
print("Events sent and flushed. Check your Amplitude project.")

view raw JSON →