1pip install june-analytics-python
This documentation provides guidance on how to integrate the June SDK with your Python application. June SDK is a powerful library for tracking user interactions and events, offering similar functionality to the underlying Segment SDK.
To install the June SDK in your Python project, use the PIP package manager:
1pip install june-analytics-python
First, you'll need to import the SDK into your application. Next, instantiate the client using the write key you've received when you created your June account:
1from june import analytics 2 3analytics.write_key = "YOUR_WRITE_KEY"
Now you can use the analytics instance to track events, identify users, and more.
The June Python SDK allows you to configure several settings according to your needs. The available configuration parameters are:
Option | Type | Description | Required |
---|---|---|---|
write_key | String | The write key for your project, obtained from June analytics. | Yes |
host | String | The hostname to which events should be sent. | No |
debug | Boolean | If set to "True", enables debug-level logging. | No |
max_queue_size | Integer | The maximum size of the event queue. If the queue becomes full, new events may be dropped. | No |
send | Boolean | If set to "False", no events will be sent. | No |
on_error | Function | A function to call when an error occurs during event sending. | No |
gzip | Boolean | If set to "True", events will be gzip-compressed before sending. | No |
max_retries | Integer | The maximum number of retries if a request fails. | No |
sync_mode | Boolean | If set to "True", events are sent synchronously. Otherwise, they are sent asynchronously. | No |
timeout | Integer | The timeout for HTTP requests. | No |
Here's an example of how to configure the client:
1from june import analytics 2 3analytics.write_key = "YOUR_WRITE_KEY" 4analytics.host = "YOUR_HOST" 5analytics.debug = True 6analytics.max_queue_size = 10000 7analytics.send = True 8analytics.on_error = lambda error, items: print(error, items) 9analytics.gzip = True 10analytics.max_retries = 5 11analytics.sync_mode = False 12analytics.timeout = 10
To track events, you can use the track
method:
1analytics.track( 2user_id="USER_ID", 3event="Signed In", 4properties={"browser": "chrome"}, 5)
Parameters:
Parameter | Type | Description | Required |
---|---|---|---|
userId | String | The ID for this user in your database. Note: At least one of "userId" or "anonymousId" must be included in any track call. | No |
anonymousId | String | An ID associated with the user when you don’t know who they are (for example, the "anonymousId" generated by analytics.js). Note: You must include at least one of "userId" or "anonymousId" in all track calls. | No |
event | String | The name of the event you’re tracking. It is recommended to use human-readable names like "Song Played" or "Status Updated". | Yes |
properties | Dictionary | A dictionary of properties for the event. For instance, if the event was "Product Added", it might have properties like price or product. | No |
timestamp | DateTime | A DateTime object representing when the track took place. If the track just happened, leave it out and the server’s time will be used. If you’re importing data from the past, make sure to send a timestamp. | No |
context | Dictionary | A dictionary of extra context to attach to the call. Note: "context" differs from "traits" because it is not attributes of the user itself. | No |
To identify users, you can use the identify
method:
1analytics.identify( 2user_id="USER_ID", 3traits={ 4"email": "test@example.com", 5# Optional 6"name": "Joe Bloggs", 7"avatar": "https://avatar.com/0sd9fsd8y97a0sf99asd.png", 8# Add anything else about the user here 9}, 10)
Parameters:
Parameter | Type | Description | Required |
---|---|---|---|
userId | String | The ID for this user in your database. Note: At least one of "userId" or "anonymousId" must be included in any identify call. | No |
anonymousId | String | An ID associated with the user when you don’t know who they are (for example, the "anonymousId" generated by analytics.js). Note: You must include at least one of "userId" or "anonymousId" in all identify calls. | No |
traits | Dictionary | A dictionary of traits you know about the user. Things like: email, name, or friends. | No |
timestamp | DateTime | A DateTime object representing when the identify took place. If the identify just happened, leave it out as Segment uses the server’s time. If you’re importing data from the past, make sure to send a timestamp. | No |
context | Dictionary | A dictionary of extra context to attach to the call. Note: "context" differs from "traits" because it is not attributes of the user itself. | No |
To group users by organization or company, use the group
method:
1analytics.group( 2user_id="USER_ID", 3group_id="GROUP_ID", 4traits={ 5"name": "Acme Inc", 6# Optional 7"avatar": "https://avatar.com/0sd9fsd8y97a0sf99asd.png", 8# Add anything else about the company here 9}, 10)
Parameters:
Parameter | Type | Description | Required |
---|---|---|---|
userId | String | The ID for this user in your database. Note: At least one of "userId" or "anonymousId" must be included in any group call. | No |
anonymousId | String | An ID associated with the user when you don’t know who they are (for example, the "anonymousId" generated by analytics.js). Note: You must include at least one of "userId" or "anonymousId" in all group calls. | No |
groupId | String | The ID of the group. | Yes |
traits | Dictionary | A dictionary of traits you know about the group. For a company, they might be things like name, address, or phone. | No |
timestamp | DateTime | A DateTime object representing when the group took place. If the group just happened, leave it out and we’ll use the server’s time. If you’re importing data from the past, make sure to send a timestamp. | No |
context | Dictionary | A dictionary of extra context to attach to the call. Note: "context" differs from "traits" because it is not attributes of the user itself. | No |
In serverless environemnts like AWS Lambda, your environment may finish the code execution before June SDK is able to send events to June API.
You can use the flush
method to send all queued events to June or you can configure the SDK to flush events automatically.
1analytics.flush()
If you set sync_mode
to True
, the SDK will flush events automatically after every event.
1analytics.sync_mode = True