Ampli for Python SDK


See examples of Ampli implementations for the following languages on GitHub:

Amplitude Data supports tracking analytics events from Python apps (Python 3.6 or higher). The generated tracking library is packaged as a python package.

  1. Install the Amplitude SDK

    1pip install amplitude-analytics
  2. Install the Ampli CLI

    1npm install -g @amplitude/ampli
  3. Pull the Ampli Wrapper into your project

    1ampli pull [--path ./ampli]
  4. Initialize the Ampli Wrapper

    1from .ampli import *
    5 client=LoadClientOptions(AMPLITUDE_API_KEY)
  5. Identify users and set user properties

    1ampli.identify("user_id", Identify(userProp="A trait associated with this user"))
  6. Track events with strongly typed methods and classes

    1ampli.song_played('user_id', SongPlayed(song_id="song-1"))
    2ampli.track('user-id', new SongFavorited(song_id="song-2"));
  7. Flush events before application exit

  8. Verify implementation status with CLI

    1ampli status [--update]

Install the Amplitude SDK

If you haven't already, install the core Amplitude SDK dependencies.

1pip install amplitude-analytics

Install Ampli

You can install the Ampli CLI from Homebrew or NPM.

1npm install -g @amplitude/ampli

1brew tap amplitude/ampli
2brew install ampli

Pull the Ampi wrapper into your project

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.

1ampli pull

Initialize Ampli

Initialize Ampli in your code. The load() method requires a configuration options parameter:

1from .ampli import *
4 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.

1ampli.identify("user_id", Identify(role="admin"))

The options argument allows you to pass Amplitude fields for this call, such as device_id.

1ampli.identify("user_id", Identify(role="admin"), EventOptions(device_id="device_id"))

Group identify

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.

1ampli.group_identify("sport", "football", Group(total_member=23))

Set group

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.

1ampli.set_group("user_id", "sport", "football")

group_name can be one group name string or multiple group names list.

1ampli.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:

1ampli.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.

1ampli.song_played('user_id', SongPlayed(
2 song_id = 'songId', # str,
3 song_favorited = True, # bool

Ampli also generates a class for each event.

1my_event = SongPlayed(
2 song_id = 'songId', # str,
3 song_favorited = True, # bool

Send event objects using the generic track method.

1ampli.track('user_id', SongPlayed(
2 song_id = 'songId', # str,
3 song_favorited = True, # bool
4), 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.



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:

1from amplitude import DestinationPlugin, PluginType, BaseEvent, IdentifyEvent
2from analytics import Client as SegmentClient
5class SegmentPlugin(DestinationPlugin):
7 def __init__(self, write_key):
8 self.plugin_type = PluginType.DESTINATION
9 self.configuration = None
10 self.segment = SegmentClient(write_key)
12 def setup(self, client):
13 self.configuration = client.configuration
15 def execute(self, event: BaseEvent) -> None:
16 if isinstance(event, IdentifyEvent):
17 self.segment.identify(event.user_id, event.user_properties)
18 elif isinstance(event, BaseEvent):
19 self.segment.track(event.user_id, event.event_type, event.event_properties)

Add your plugin after init Ampli:



The pull command downloads the Ampli Wrapper code to your project. Run the pull command from the project root.

1ampli pull

You will be prompted to log in to your workspace and select a source.

1ampli pull
2Ampli project is not initialized. No existing `ampli.json` configuration found.
3? Create a new Ampli project here? Yes
4? Organization: Amplitude
5? Workspace: My Workspace
6? Source: My Source


Verify that events are implemented in your code with the status command:

1ampli status [--update]

The output displays status and indicates what events are missing.

1ampli status
2Verifying event tracking implementation in source code
3Song Played (1 location)
4Song Stopped Called when a user stops playing a song.
5Events Tracked: 1 missed, 2 total
Was this page helpful?

Thanks for your feedback!

July 16th, 2024

Need help? Contact Support


Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.