Python
updated 2 years ago
In this article:
- Installation
- Configuration
- Identifying users
- Identifying as an Account
- Update/add user attributes
- Tracking user events and actions
- Add user to an account
- Change a user's role in an account
- Remove a user from an account
The Engage Python SDK allows you to collect user attributes and events from your app. You can then use this to create user segments for analytics, broadcast messages, and automation messages in Engage.
Don't Have an account? Create an Engage account.
Installation
pip install engagesdk
Configuration
Use your public and private keys to initialize the SDK. Your keys can be found in the settings section of your Engage dashboard (Settings -> Accounts).
from engagesdk import Engage
engage = Engage(api_key = '', secret_key = '')
Identifying users
To track events and attributes on Engage, you only need a unique identity that represents the user on your platform. You can provide the unique identification and other attributes to Engage to link an appropriate profile to these tracked attributes and events.
engage.users.identify({
'id': 'u13345',
'email': '[email protected]',
'created_at': '2020-05-30T09:30:10Z'
})
id
represents the unique identifier for the user on your platform. It is the only required parameter. You can send any other attribute you want e.g. age
, plan
. Here are the standard ones we use internally on the user profile:
first_name
last_name
email
is_account
number
(with international dialing code without the +)created_at
(represents when the user registered on your platform. If not added, Engage sets it to the current timestamp.)device_platform
device_token
Identifying as an Account
If you want to identify a user as an account, you can add is_account
property and set it to true
:
engage.users.identify({
'id': 'u13345',
'is_account': 'true',
'email': '[email protected]',
'created_at': '2020-05-30T09:30:10Z'
})
You can convert between Customer and Account user types with the methods below.
This converts a Customer to an Account:
engage.users.convert_to_account(user_id)
This converts an Account to a Customer:
engage.users.convert_to_customer(user_id)
Update/add user attributes
The add_attribute
method can be used to add new attributes or to update existing ones.
engage.users.add_attribute(user_id, {
'first_name': 'Dan',
'plan': 'Premium'
})
(You can also use identify
to update or add new attributes.)
Tracking user events and actions
You can track user events and actions in a couple of ways.
Tracking an event with no value:
engage.users.track(user_id, 'paid');
Tracking an event with a value:
engage.users.track(user_id, {
'event': 'New badge',
'value': 'gold',
'timestamp': '2020-05-30T09:30:10Z'
})
event
is the event you want to track. value
is the value of the event. This can be a string, number, or boolean. There is an optional timestamp
parameter. If not included, Engage uses the current timestamp. The timestamp value must be a valid date-time string.
If you need to track more properties with the event, you can do it this way:
engage.users.track(user_id, {
'event': 'Add to cart',
'properties': {
'product': 'T123',
'currency': 'USD',
'amount': 12.99
}
})
Add user to an account
This adds a user to an account if it exists.
engage.users.add_to_account(user_id= "uid", account_id= "aid", role="developer")
Where
user_id
- The uid of the Customer you want to add to the Account.account_id
- The uid of the Account you are adding the Customer to.role
- The role of the Customer in the Account. This is optional.
Change a user's role in an account
This changes a user's role in an account
engage.users.change_account_role(user_id= "uid", account_id= "aid", role="admin")
Remove a user from an account
This removes a user from an account
engage.users.remove_from_account(user_id= "uid", account_id= "aid")
Was this article helpful?