Python SDK

The Python SDK lets you send events to Amplitude.

Install the SDK

Install amplitude-analytics with pip:

1pip install amplitude-analytics

Initialize the SDK

Initialize the SDK before any events are instrumented. The API key for your Amplitude project is required. You can also pass a config object in this call. You can use the SDK client instance across requests after it's initialized.

1from amplitude import Amplitude
2 
3client = Amplitude(AMPLITUDE_API_KEY)

Configure the SDK

Name Description Default Value
api_key Required. String. The API key of the Amplitude project. Events sent by the client instance are in this project. Set when you initialize the client instance. None
flush_queue_size Integer. Events wait in the buffer and are sent in a batch. The buffer is flushed when the number of events reaches flush_queue_size. 200
flush_interval_millis Integer. Events wait in the buffer and are sent in a batch. The buffer is flushed every flush_interval_millis milliseconds. 10 seconds
flush_max_retries Integer. The number of times the client retries an event when the request returns an error. 12
logger Logger. The logger instance used by Amplitude client. python built-in logging: logging.getLogger(name).
min_id_length Integer. The minimum length of user_id and device_id. 5
callback Function. Client level callback function. Takes three parameters:
1. event: a Event instance
2. code: a integer of HTTP response code
3. message: a string message.
None
server_zone String. The server zone of the projects. Supports EU and US. For EU data residency, Change to EU. US
server_url String. The API endpoint URL that events are sent to. Automatically selected by server_zone and use_batch. If this field is set with a string value instead of None, then server_zone and use_batch are ignored and the string value is used. https://api2.amplitude.com/2/httpapi
use_batch Boolean. Whether to use batch API. By default, the SDK will use the default serverUrl. False
storage_provider StorageProvider. Used to create storage instance to hold events in the storage buffer. Events in storage buffer are waiting to be sent. InMemoryStorageProvider
opt_out Boolean. Opt out option. If set to True, client doesn't process and send events. False
1def callback_func(event, code, message=None):
2 # callback function that takes three input parameters
3 # event: the event that triggered this callback
4 # code: status code of request response
5 # message: a optional string message for more detailed information
6 
7client.configuration.api_key = "new api key"
8client.configuration.flush_max_retries = 5
9client.configuration.logger = logging.getLogger(__name__)
10client.configuration.min_id_length = 7
11client.configuration.callback = callback_func
12client.configuration.server_zone = "EU"
13client.configuration.use_batch = True
14client.configuration.server_url = "proxy url that forwarding the requests"
15client.configuration.opt_out = False

Configure batching behavior

To support high-performance environments, the SDK sends events in batches. Every event logged by track method is queued in memory. Events are flushed in batches in background. You can customize batch behavior with flush_queue_size and flush_interval_millis. By default, the SDK is in regular mode with serverUrl to https://api2.amplitude.com/2/httpapi. For customers who want to send large batches of data at a time, switch to batch mode by setting use_batch to trueto set setServerUrl to batch event upload API https://api2.amplitude.com/batch. Both the regular mode and the batch mode use the same flush queue size and flush intervals.

1from amplitude import Amplitude
2 
3client = Amplitude(AMPLITUDE_API_KEY)
4 
5# Events queued in memory will flush when number of events exceed upload threshold
6# Default value is 200
7client.configuration.flush_queue_size = 100
8# Events queue will flush every certain milliseconds based on setting
9# Default value is 10 milliseconds
10client.configuration.flush_interval_millis = 20000 # 20 seconds

Track an event

Events represent how users interact with your application. For example, “Button Clicked” may be an action you want to note.

1from amplitude import Amplitude, BaseEvent
2 
3client = Amplitude(AMPLITUDE_API_KEY)
4 
5# Track a basic event
6# One of user_id and device_id is required
7event = BaseEvent(event_type="Button Clicked", user_id="User Id")
8client.track(event)
9 
10# Track events with optional properties
11client.track(
12 BaseEvent(
13 event_type="type of event",
14 user_id="USER_ID",
15 device_id="DEVICE_ID",
16 event_properties={
17 "source": "notification"
18 }
19))

User properties

User properties help you understand your users at the time they performed some action within your app such as their device details, their preferences, or language.

Identify is for setting the user properties of a particular user without sending any event. The SDK supports the operations set, set_once, unset, add, append, prepend, pre_insert, post_insert, and remove on individual user properties. Declare the operations via a provided Identify interface. You can chain multiple operations together in a single Identify object. The Identify object is then passed to the Amplitude client to send to the server.

Note

If the Identify call is sent after the event, the results of operations are visible immediately in the dashboard user’s profile area, but it won't appear in chart result until another event is sent after the Identify call. The identify call only affects events going forward. More details here.

Set a user property

The Identify object provides controls over setting user properties. An Identify object must first be instantiated, then Identify methods can be called on it, and finally the client makes a call with the Identify object.

1from amplitude import Identify, EventOptions
2 
3 
4identify_obj=Identify()
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.set

This method sets the value of a user property. For example, you can set a role property of a user.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.set("location", "LAX")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.set_once

This method increments a user property by some numerical value. If the user property doesn't have a value set yet, it will be initialized to 0 before being incremented. For example, you can track a user's travel count.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.add("travel-count", 1)
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Arrays in user properties

You can use arrays as user properties. You can directly set arrays or use prepend, append, pre_insert and post_insert to generate an array.

Identify.prepend

This method prepends a value or values to a user property array. If the user property doesn't have a value set yet, it will be initialized to an empty list before the new values are prepended.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.prepend("visited-locations", "LAX")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.append

This method appends a value or values to a user property array. If the user property doesn't have a value set yet, it will be initialized to an empty list before the new values are appended.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.append("visited-locations", "SFO")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.pre_insert

This method pre-inserts a value or values to a user property if it doesn't exist in the user property yet. Pre-insert means inserting the values at the beginning of a given list. If the user property doesn't have a value set yet, it will be initialized to an empty list before the new values are pre-inserted. If the user property has an existing value, it will be no operation.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.pre_insert("unique-locations", "LAX")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.post_insert

This method post-inserts a value or values to a user property if it doesn't exist in the user property yet. Post-insert means inserting the values at the end of a given list. If the user property doesn't have a value set yet, it will be initialized to an empty list before the new values are post-inserted. If the user property has an existing value, it will be no operation.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.post_insert("unique-locations", "SFO")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

Identify.remove

This method removes a value or values to a user property if it exists in the user property. Remove means remove the existing value from the given list. If the item doesn't exist in the user property, it will be no operation.

1from amplitude import Identify, EventOptions
2 
3identify_obj=Identify()
4identify_obj.remove("unique-locations", "JFK")
5client.identify(identify_obj, EventOptions(user_id="USER_ID"))

User groups

Amplitude supports assigning users to groups and performing queries such as Count by Distinct on those groups. An example would be if you want to group your users based on what organization they're in by using an 'orgId'. You can designate Joe to be in 'orgId' '10' while Sue is in 'orgId' '15'. When performing a query in the Event Segmentation chart, you can then select "..performed by" 'orgId' to query the number of different organizations that have performed a specific event. As long as at least one member of that group has performed the specific event, that group is included in the count.

When setting groups, you need to define a group_type and group_name. In the previous example, 'orgId' is the group_type and each of the values '10' and '15' are a group_name. Another example of a group_type could be 'sport' with group_name values like 'tennis' and 'baseball'. You can use set_group() to designate which groups a user belongs to. Note: This also sets the 'group_type:group_name' as a user property. This overwrites any existing group_name value set for that user's group_type, as well as the corresponding user property value. group_type is a string and group_name can be either a string or an array of strings to indicate a user being in multiple groups (for example, if Joe is in 'orgId' '10' and '16', then the group_name would be '[10, 16]'). Here is what your code might look like.

1# set group with single group name
2client.set_group(group_type="org_id", group_name="15",
3 event_options=EventOptions(user_id="USER_ID"))
4 
5# set group with multiple group names
6client.set_group(group_type="org_id", group_name=["15", "21"],
7 event_options=EventOptions(user_id="USER_ID"))

Event level groups are set by groups attribute of events

1# set groups when initial a event instance
2event = BaseEvent("event_type", "user_id", groups={"org_id": ["15", "21"]})
3 
4# set groups for an existing instance
5event["groups"] = {"sport": "soccer"}
6 
7client.track(event)

Group properties

Use the Group Identify API to set or update the properties of particular groups. However, these updates will only affect events going forward.

The group_identify() method accepts a group type and group name string parameter, as well as an Identify object that's applied to the group.

1identify_obj=Identify()
2identify_obj.set("locale", "en-us")
3client.group_identify(group_type="org-id", group_name="15", identify_obj=identify_obj)

Track revenue

The preferred method of tracking revenue for a user is to use revenue() in conjunction with the provided Revenue interface. Revenue instances store each revenue transaction and allow you to define several special revenue properties, such as revenue_type and product_id, that are used in Amplitude's Event Segmentation and Revenue LTV charts. These Revenue instance objects are then passed into revenue to send as revenue events to Amplitude. This allows Amplitude to automatically display data relevant to revenue in the platform. You can use this to track both in-app and non-in-app purchases.

To track revenue from a user, call revenue each time a user generates revenue. For example, 3 units of a product were purchased at $3.99.

1from amplitude import Revenue
2 
3revenue_obj = Revenue(price=3.99,
4 quantity=3,
5 product_id="com.company.productId")
6client.revenue(revenue_obj, EventOptions(user_id="USER_ID"))

The revenue interface

Name Type Description Default
product_id (optional) string An identifier for the product. Amplitude recommends something like the Google Play Store product ID. null
quantity (required) int The quantity of products purchased. revenue = quantity * price 1
price (required) Double The price of the products purchased, and this can be negative. revenue = quantity * price null
revenue_type (optional, required for revenue verification) String The revenue type (for example, tax, refund, income). null
receipt (optional) String The receipt identifier of the revenue. null
receipt_sig (optional, required for revenue verification) String The receipt signature of the revenue. null
properties (optional) JSONObject An object of event properties to include in the revenue event. null

Flush

The flush method triggers the client to send buffered events.

1client.flush()

Add

The add method adds a plugin to Amplitude client instance. Plugins can help processing and sending events. Learn more about plugins..

1client.add(plugin_obj)

Remove

The remove method removes the given plugin from the client instance if exists.

1client.remove(plugin_obj)

Shutdown

Use the shutdown method to close the instance. A closed instance doesn't accept new events and tries to flush events left in the buffer. Then the client instance shuts down running threads.
In version v1.1.1 and higher, the shutdown method is automatically registered to be called when the main thread exits.

1client.shutdown()

Plugins

Plugins allow you to extend Amplitude SDK's behavior by, for example, modifying event properties (enrichment type) or sending to third-party APIs (destination type). A plugin is an object with methods setup() and execute().

Plugin.setup

This method contains logic for preparing the plugin for use and has client instance as a parameter. The expected return value is None. A typical use for this method, is to copy configuration from client.configuration or instantiate plugin dependencies. This method is called when you register the plugin to the client via client.add().

Plugin.execute

This method contains the logic for processing events and has event instance as parameter. If used as enrichment type plugin, the expected return value is the modified/enriched event. If used as a destination type plugin, the expected return value is a map with keys: event (BaseEvent), code (number), and message (string). This method is called for each event that's instrumented using the client interface, including Identify, GroupIdentify and Revenue events.

Plugin examples

Enrichment type plugin

Here's an example of a plugin that modifies each event that's instrumented by adding an increment integer to event_id property of an event.

1from threading import Lock
2from amplitude import Amplitude, EventPlugin, PluginType
3 
4class AddEventIdPlugin(EventPlugin):
5 
6 def __init__(self, start=0):
7 super().__init__(PluginType.ENRICHMENT)
8 self.current_id = start
9 self.configuration = None
10 self.lock = Lock()
11 
12 def setup(self, client):
13 self.configuration = client.configuration
14 
15 def execute(self, event):
16 with self.lock:
17 event.event_id = self.current_id
18 self.current_id += 1
19 return event
20 
21client = Amplitude(AMPLITUDE_API_KEY)
22client.add(AddInsertIdPlugin())

Destination type plugin

1from amplitude import Amplitude, EventPlugin, DestinationPlugin, PluginType
2import requests
3 
4class MyDestinationPlugin(DestinationPlugin):
5 
6 def __init__(self):
7 super().__init__()
8 # other init operations
9 self.url = "api endpoint url"
10 self.configuration = None
11 
12 def setup(self, client):
13 # setup plugin using client instance
14 # triggered by client.add() method
15 super().setup(client)
16 self.configuration = client.configuration
17 
18 def execute(self, event):
19 # process event using plugins in this destination plugin instance
20 event = self.timeline.process(event)
21 # send event to customized destination
22 payload = '{"key":"secret", "event": ' + str(event) + '}'
23 requests.post(self.url, data=payload)
24 self.configuration.logger.info("Event sent")
25 
26 
27client = Amplitude(AMPLITUDE_API_KEY)
28client.add(MyDestinationPlugin())
Was this page helpful?

Thanks for your feedback!

July 23rd, 2024

Need help? Contact Support

Visit Amplitude.com

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.