Skip to main content

Umami Analytics Client for Python

Project description

Umami Analytics Client for Python

Client for privacy-preserving, open source Umami analytics platform based on httpx and pydantic.

umami-analytics is intended for adding custom data to your Umami instance (self-hosted or SaaS). Many umami events can supplied directly from HTML via their data-* attributes. However, some cannot. For example, if you have an event that is triggered in your app but doesn't have a clear HTML action you can add custom events. These will appear at the bottom of your Umami analtytics page for a website.

One example is a purchase-course event that happens deep inside the Python code rather than in HTML at Talk Python Training. This is what our events section looks like for a typical weekend day (US Pacific Time):

Focused on what you need, not what is offered

The Umami API is extensive and much of that is intended for their frontend code to be able to function. You probably don't want or need that. umami-analytics only covers the subset that most developers will need for common SaaS actions such as adding custom events. That said, PRs are weclome.

Core Features

  • Add a custom event to your Umami analytics dashboard.
  • 💰 Track revenue with dedicated revenue event support.
  • 📄 Add a page view to your Umami analytics dashboard.
  • 🌐 List all websites with details that you have registered at Umami.
  • 📊 Get website statistics including page views, visitors, bounce rate, and more.
  • 👥 Get active users count for real-time monitoring.
  • 💓 Heartbeat check to verify Umami server connectivity.
  • 🔀 Both sync and async programming models.
  • ⚒️ Structured data with Pydantic models for API responses.
  • 👩‍💻 Login / authenticate for either a self-hosted or SaaS hosted instance of Umami.
  • 🥇Set a default website for a simplified API going forward.
  • 🔧 Enable/disable tracking for development and testing environments.

Development and Testing Support

🔧 Disable tracking in development: Use umami.disable() to disable all event and page view tracking without changing your code. Perfect for development and testing environments where you don't want to pollute your analytics with test data.

import umami

# Configure as usual
umami.set_url_base("https://umami.hostedbyyouorthem.com")
umami.set_website_id('cc726914-8e68-4d1a-4be0-af4ca8933456')
umami.set_hostname('somedomain.com')

# Disable tracking for development/testing
umami.disable()

# These calls will return immediately without sending data to Umami
umami.new_event('test-event')  # No HTTP request made
umami.new_page_view('Test Page', '/test')  # No HTTP request made

# Re-enable when needed (default state is enabled)
umami.enable()

When tracking is disabled:

  • No HTTP requests are made to your Umami server
  • API calls still validate parameters (helps catch configuration issues)
  • All other functions work normally (login, websites, stats, etc.)
  • Functions return appropriate values for compatibility

See the usage example below for the Python API around these features.

Async or sync API? You choose

🔀 Async is supported but not required for your Python code. For functions that access the network, there is a func() and func_async() variant that works with Python's async and await.

Installation

Just pip install umami-analytics

Usage

import umami

umami.set_url_base("https://umami.hostedbyyouorthem.com")

# Auth is NOT required to send events, but is for other features.
login = umami.login(username, password)

# Skip the need to pass the target website in subsequent calls.
umami.set_website_id('cc726914-8e68-4d1a-4be0-af4ca8933456')
umami.set_hostname('somedomain.com')

# Optional: Disable tracking for development/testing
# umami.disable()  # Uncomment to disable tracking

# List your websites
websites = umami.websites()

# Create a new event in the events section of the dashboards.
event_resp = umami.new_event(
    website_id='a7cd-5d1a-2b33', # Only send if overriding default above
    event_name='Umami-Test',
    title='Umami-Test', # Defaults to event_name if omitted.
    hostname='somedomain.com', # Only send if overriding default above.
    url='/users/actions',
    custom_data={'client': 'umami-tester-v1'},
    referrer='https://some_url')

# Create a new page view in the pages section of the dashboards.
page_view_resp = umami.new_page_view(
    website_id='a7cd-5d1a-2b33', # Only send if overriding default above
    page_title='Umami-Test', # Defaults to event_name if omitted.
    hostname='somedomain.com', # Only send if overriding default above.
    url='/users/actions',
    referrer='https://some_url')

# Track revenue for a transaction
revenue_resp = umami.new_revenue_event(
    revenue=19.99,  # Monetary amount (required)
    currency='USD',  # ISO 4217 currency code, defaults to 'USD'
    event_name='checkout-cart',  # Defaults to 'revenue' if omitted
    url='/checkout',
    custom_data={'product': 'widget', 'quantity': 2})

# Get website statistics for a date range
from datetime import datetime, timedelta

end_date = datetime.now()
start_date = end_date - timedelta(days=7)  # Last 7 days

stats = umami.website_stats(
    start_at=start_date,
    end_at=end_date,
    website_id='a7cd-5d1a-2b33'  # Only send if overriding default above
)
print(f"Page views: {stats.pageviews}")
print(f"Unique visitors: {stats.visitors}")
print(f"Bounce rate: {stats.bounces}")

# Get current active users count
active_count = umami.active_users(
    website_id='a7cd-5d1a-2b33'  # Only send if overriding default above
)
print(f"Currently active users: {active_count}")

# Check if Umami server is accessible
server_ok = umami.heartbeat()
print(f"Umami server is {'accessible' if server_ok else 'not accessible'}")

# Call after logging in to make sure the auth token is still valid.
umami.verify_token()

This code listing is very-very high fidelity psuedo code. If you want an actually executable example, see the example client in the repo.

Want to contribute?

See the API documentation for the remaining endpoints to be added. PRs are welcome. But please open an issue first to see if the proposed feature fits with the direction of this library.

Enjoy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

umami_analytics-0.4.23.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

umami_analytics-0.4.23-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file umami_analytics-0.4.23.tar.gz.

File metadata

  • Download URL: umami_analytics-0.4.23.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for umami_analytics-0.4.23.tar.gz
Algorithm Hash digest
SHA256 e49c4cbc5d7ccaebbb4aa74fdbda6df3cb24ca960425ac7c5e3679f5f737d75a
MD5 75cfdfd649d911e7a1bc5ef125b32253
BLAKE2b-256 6f58d2b0a07a6f07f7b122bccfaf632dce0427bfaa1078c8e4dcc3fc30fafba1

See more details on using hashes here.

File details

Details for the file umami_analytics-0.4.23-py3-none-any.whl.

File metadata

  • Download URL: umami_analytics-0.4.23-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for umami_analytics-0.4.23-py3-none-any.whl
Algorithm Hash digest
SHA256 8413bcd4083f757b8d528b95813a952e0d1a1b2e19ec450994f21d134f27fcf9
MD5 afac2287aebcf9f1fbc4647d4cd31f7f
BLAKE2b-256 e9aa1f77d6c77ae0d0186a5dc06c2a253f333e29c775c0b3984aa30c808ea004

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page