Log in

Python SDK

Instant insights from any Python app

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.

Getting Started

Installation

To install the June SDK in your Python project, use the PIP package manager:

1
pip install june-analytics-python

Configuration

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:

1
from june import analytics
2
3
analytics.write_key = "YOUR_WRITE_KEY"

Now you can use the analytics instance to track events, identify users, and more.

API Reference

Client

June Python SDK Client Configuration

The June Python SDK allows you to configure several settings according to your needs. The available configuration parameters are:

Initialization Parameters

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:

1
from june import analytics
2
3
analytics.write_key = "YOUR_WRITE_KEY"
4
analytics.host = "YOUR_HOST"
5
analytics.debug = True
6
analytics.max_queue_size = 10000
7
analytics.send = True
8
analytics.on_error = lambda error, items: print(error, items)
9
analytics.gzip = True
10
analytics.max_retries = 5
11
analytics.sync_mode = False
12
analytics.timeout = 10

Tracking Events

To track events, you can use the track method:

1
analytics.track(
2
user_id="USER_ID",
3
event="Signed In",
4
properties={"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

Identifying Users & Setting User Traits

To identify users, you can use the identify method:

1
analytics.identify(
2
user_id="USER_ID",
3
traits={
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

Grouping Users

To group users by organization or company, use the group method:

1
analytics.group(
2
user_id="USER_ID",
3
group_id="GROUP_ID",
4
traits={
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

Serverless environments

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.

Flush events manually

1
analytics.flush()

Flush events automatically

If you set sync_mode to True, the SDK will flush events automatically after every event.

1
analytics.sync_mode = True