Amplitude Data supports tracking analytics events from Python apps (Python 3.6 or higher). The generated tracking library is packaged as a python package.
pip install amplitude-analytics
npm install -g @amplitude/ampli
Pull the Ampli Wrapper into your project
ampli pull [--path ./ampli]
from .ampli import * ampli.load(LoadOptions( client=LoadClientOptions(AMPLITUDE_API_KEY)))
Identify users and set user properties
ampli.identify("user_id", Identify(userProp="A trait associated with this user"))
Track events with strongly typed methods and classes
ampli.song_played('user_id', SongPlayed(song_id="song-1"))ampli.track('user-id', new SongFavorited(song_id="song-2"));
Flush events before application exit
ampli.flush();
Verify implementation status with CLI
ampli status [--update]
If you haven't already, install the core Amplitude SDK dependencies.
pip install amplitude-analytics
You can install the Ampli CLI from Homebrew or NPM.
npm install -g @amplitude/ampli
brew tap amplitude/amplibrew install ampli
Run the Ampli CLI pull
command to log in to Amplitude Data and download the strongly typed Ampli Wrapper for your tracking plan. Ampli CLI commands are usually run from the project root directory.
ampli pull
Initialize Ampli in your code. The load()
method requires a configuration options parameter:
from .ampli import * ampli.load(LoadOptions( client=LoadClientOptions(AMPLITUDE_API_KEY)))
Arg of load() | Description |
---|---|
options |
Required. An instance of LoadOptions. Specifies configuration options for the Ampli Wrapper. |
Arg of LoadOptions | Description |
---|---|
disabled |
Optional. Defaults to False. Specifies whether the Ampli Wrapper does any work. When true, all calls to the Ampli Wrapper are no-ops. Useful in local or development environments. |
client |
Optional. Defaults to None. A instance of LoadClientOptions specifies configuration options for the Amplitude core SDK client. |
Arg of LoadClientOptions | Description |
---|---|
instance |
Required if apiKey isn't set. Specifies an Amplitude instance. By default Ampli creates an instance for you. |
api_key |
Required if instance isn't set. Specifies an API Key. This option overrides the default, which is the API Key configured in your tracking plan. |
configuration |
Optional. Defaults to None. Specifies the Amplitude configuration. This option overrides the default configuration. |
Call identify()
to identify a user in your app and associate all future events with their identity, or to set their properties.
Just as Ampli creates types for events and their properties, it creates types for user properties.
The identify()
function accepts a string user_id
, an Identify event instance, and an optional EventOptions
.
For example your tracking plan contains a user property called role
. The property's type is a string.
ampli.identify("user_id", Identify(role="admin"))
The options argument allows you to pass Amplitude fields for this call, such as device_id
.
ampli.identify("user_id", Identify(role="admin"), EventOptions(device_id="device_id"))
Call group_identify()
to identify a group in your app and set/update group properties.
Just as Ampli creates types for events and their properties, it creates types for group properties.
The group_identify()
function accepts a string group_type
, a string group_name
, an Group event instance, and an optional EventOptions
.
For example your tracking plan contains a group sport:football
has a property called total_member
. The property's type is a int.
ampli.group_identify("sport", "football", Group(total_member=23))
Call set_group()
to associate a user with their group (for example, their department or company). The set_group()
function accept user_id
group_type
, group_name
and an optional EventOptions.
ampli.set_group("user_id", "sport", "football")
group_name
can be one group name string or multiple group names list.
ampli.set_group("user_id", "sport", ["football", "basketball"])
To track an event, call the event's corresponding function. Every event in your tracking plan gets its own function in the Ampli Wrapper. The call is structured like this:
ampli.event_name("user_id", EventName(...), EventOptions(...))
The optional EventOptions
argument allows you to pass Amplitude fields, like device_id
.
For example, in the following code snippet, your tracking plan contains an event called songPlayed
. The event is defined with two required properties: songId
and songFavorited.
The property type for songId
is string, and songFavorited
is a boolean.
ampli.song_played('user_id', SongPlayed( song_id = 'songId', # str, song_favorited = True, # bool))
Ampli also generates a class for each event.
my_event = SongPlayed( song_id = 'songId', # str, song_favorited = True, # bool)
Send event objects using the generic track method.
ampli.track('user_id', SongPlayed( song_id = 'songId', # str, song_favorited = True, # bool), EventOptions(device_id="device_id"))
The Ampli wrapper queues events and sends them on an interval based on the configuration.
Call flush()
to immediately send any pending events.
The flush()
method returns a promise that can be used to ensure all pending events have been sent before continuing.
This can be useful to call prior to application exit.
Ampli flushes events in the buffer automatically when flushQueueSize
or flushInterval
are reached.
Ampli sends events automatically without calling flush()
, but using flush()
is useful if you need to send events before the application exits.
ampli.flush();
Plugins allow you to extend the Amplitude behavior, for example, modifying event properties (enrichment type) or sending to third-party APIs (destination type).
First you need to define your plugin. Destination Plugin example:
from amplitude import DestinationPlugin, PluginType, BaseEvent, IdentifyEventfrom analytics import Client as SegmentClient class SegmentPlugin(DestinationPlugin): def __init__(self, write_key): self.plugin_type = PluginType.DESTINATION self.configuration = None self.segment = SegmentClient(write_key) def setup(self, client): self.configuration = client.configuration def execute(self, event: BaseEvent) -> None: if isinstance(event, IdentifyEvent): self.segment.identify(event.user_id, event.user_properties) elif isinstance(event, BaseEvent): self.segment.track(event.user_id, event.event_type, event.event_properties)
Thanks for your feedback!
July 16th, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2025 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.