Registry / communication / analytics-python

analytics-python

JSON →
library1.4.post1pypypi✓ verified 49d ago

analytics-python is Segment's official Python client library, designed for integrating analytics into any Python application. It simplifies data collection and routing to various analytics services via a single API. The library uses an internal queue for non-blocking calls, batching messages, and asynchronously flushing data to Segment's servers. Currently, this specific package (version 1.4.post1) is in maintenance mode, receiving only critical updates, with Segment recommending `segment-analytics-python` (version 2.x) for new feature support and active development.

communicationdata
pip install analytics-python
Install & Compatibility
Where this runs
tested against v1.4.post1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.687s · 22.4MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.3s · import 0.617s · 23MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

analytics
import segment.analytics as analytics

This quickstart demonstrates how to initialize the Segment client with a write key and send common analytics events like `identify`, `track`, `page`, `group`, and `alias`. It includes a crucial `flush()` call to ensure all events are sent, especially in applications that might terminate quickly.

import os import segment.analytics as analytics # Configure with your Write Key from Segment # It's recommended to load this from an environment variable or secure config analytics.write_key = os.environ.get('SEGMENT_WRITE_KEY', 'YOUR_SEGMENT_WRITE_KEY') # For testing, you might want to run in synchronous mode and debug # analytics.debug = True # analytics.sync_mode = True # Identify a user analytics.identify('user-123', { 'name': 'John Doe', 'email': 'john.doe@example.com', 'plan': 'premium' }) # Track an event analytics.track('user-123', 'Signed Up', { 'method': 'Email', 'plan': 'premium' }) # Track a page view analytics.page('user-123', 'Home Page', { 'title': 'Welcome to our site' }) # Group a user with a company/account analytics.group('user-123', 'group-456', { 'name': 'Acme Corp', 'industry': 'Technology' }) # Alias users analytics.alias('anonymous-id', 'user-123') # Flush any buffered events before exiting (important in short-lived scripts) analytics.flush()
Debug
Known issues
breakingThe `analytics-python` library (version 1.x) is officially in maintenance mode. This means it will send data as intended but will not receive new feature support, only critical maintenance updates. Segment recommends migrating to `segment-analytics-python` (version 2.x) for active development and new features.
fix
Consider upgrading to `segment-analytics-python` by `pip install segment-analytics-python` and updating import paths and potentially API calls according to its documentation.
affects: < 2.0.0
gotchaWhen performing historical data imports using the `timestamp` argument, some destinations like Google Analytics do not accept historical timestamped data, which can lead to data discrepancies or failures for specific integrations.
fix
Verify destination compatibility with historical data before importing. For Google Analytics, historical data cannot be imported via this method.
affects: All versions
gotchaIn server-side applications, it is crucial to call `analytics.flush()` before the application exits or shuts down. This ensures that all buffered events are sent to Segment, preventing data loss, especially if not running in synchronous mode.
fix
Implement explicit `analytics.flush()` calls in application shutdown hooks or error handlers. If `sync_mode = True` is used, messages are sent immediately, but this can impact performance.
affects: All versions
gotchaIf your application needs to send data to multiple Segment sources (each with a different `write_key`), you must initialize a new `Client` instance for each `write_key` to manage them independently.
fix
Instead of setting `analytics.write_key` globally, create `analytics.Client(write_key='YOUR_KEY_1')` and `analytics.Client(write_key='YOUR_KEY_2')` instances and use them separately.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'analytics'
This error occurs when the 'analytics-python' package is not installed or not found in the Python environment.
fix
Install the package using pip: 'pip install analytics-python'.
ImportError: cannot import name 'analytics'
This error occurs when attempting to import a module or function that does not exist or is not accessible in the 'analytics-python' package.
fix
Ensure that you are importing the correct module or function from 'analytics-python'. Refer to the official documentation for the correct import statements.
AttributeError: 'analytics' object has no attribute 'track'
This error occurs when the 'analytics' object does not have the 'track' method, possibly due to incorrect initialization or usage.
fix
Verify that the 'analytics' object is properly initialized and that the 'track' method is being called correctly. Refer to the official documentation for proper usage.
TypeError: track() missing 1 required positional argument: 'event'
This error occurs when the 'track' method is called without the required 'event' argument.
fix
Ensure that you provide the 'event' argument when calling the 'track' method: 'analytics.track(user_id, event, properties)'.
ValueError: Invalid write key
This error occurs when an invalid or incorrect write key is provided during the initialization of the 'analytics' client.
fix
Verify that the correct write key is being used during initialization: 'analytics.write_key = 'YOUR_WRITE_KEY''. Ensure that the write key is valid and corresponds to your Segment account.
Upgrade
Version history
1.4.post1latest on PyPI
Audit
Dependencies
requestsrequiredHTTP client for making requests to the Segment API.
python-dateutilrequiredDate and time utilities.
sixrequiredPython 2 and 3 compatibility utilities (often a dependency of other packages, may not be explicitly listed by users).
monotonicoptionalMonotonic time for reliable timing (conditional for older Python versions).
PyJWToptionalJSON Web Token implementation (used for authentication if crypto features are enabled).
Agent activity
90 hits · last 30 days
node
8
claudebot
4
ahrefsbot
3
amazonbot
1
bytedance
1
googlebot
1
Resources