CARVIEW |
Navigation Menu
-
Notifications
You must be signed in to change notification settings - Fork 94
How to build your own SDK
π₯ Aptabase is a powerful analytics platform designed for mobile and desktop applications. Building a client-side SDK to interact with Aptabase is straightforward and can significantly enhance your app's data collection capabilities. In this guide, we'll walk you through the steps to create your own SDK.
To send events to Aptabase, you need to make a POST request to the {host}/api/v0/events
endpoint with a list of events as the body (max batch size is 25). The events should be sent asynchronously and can be stored in an in-memory queue to handle connectivity issues.
π Here's an example of the event payload:
[
{
"timestamp": "2024-04-19T08:44:07.465Z",
"sessionId": "171351624706652714",
"eventName": "home_viewed",
"systemProps": {
"locale": "en-US",
"osName": "macOS",
"osVersion": "14.2.2",
"deviceModel": "MacBookPro15,1",
"isDebug": false,
"appVersion": "1.0.0",
"sdkVersion": "aptabase-sdk@0.4.0"
},
"props": {
"period": "24h",
"apps_count": 3
}
}
]
Tip
-
systemProps
: These properties are usually automatically detected by the SDK and include information about the system, locale, OS, device model, and app version. -
props
: These are defined and input by the SDK consumer to capture event-specific data.
βοΈ The host can be determined from the App-Key. It will be either EU or US. Here's an example:
def get_host_from_app_key(app_key):
return 'https://eu.aptabase.com' if 'EU' in app_key else 'https://us.aptabase.com'
π The session ID must be in a specific format (epochInSeconds + 8 random numbers):
def new_session_id():
epoch_seconds = int(datetime.datetime.utcnow().timestamp())
random_number = random.randint(0, 99999999)
return f"{epoch_seconds}{random_number}"
π² All API calls should be done asynchronously to avoid blocking the UI thread.
const response = await fetch(opts.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'App-Key': opts.appKey,
},
credentials: 'omit',
body: JSON.stringify({
timestamp: new Date().toISOString(),
sessionId: opts.sessionId,
eventName: opts.eventName,
systemProps: {
locale: opts.locale,
isDebug: opts.isDebug,
appVersion: opts.appVersion,
sdkVersion: opts.sdkVersion,
},
props: opts.props,
}),
});
Tip
- Make sure to include the
App-Key
http header. - Let the application continue running even if a tracking request fails (log errors without raising them).
π« You can use an in-memory queue to store events and try to send them periodically, ensuring event delivery even if the device is offline.
curl -X POST https://eu.aptabase.com/api/v0/events \
-H "Content-Type: application/json" \
-H "App-Key: A-EU-00000000" \
-d '[
{
"timestamp": "2024-04-19T08:44:07.465Z",
"sessionId": "171351624706652714",
"eventName": "home_viewed",
"systemProps": {
"locale": "en-US",
"osName": "macOS",
"osVersion": "14.2.2",
"deviceModel": "MacBookPro15,1",
"isDebug": false,
"appVersion": "1.0.0",
"sdkVersion": "aptabase-sdk@0.4.0"
},
"props": {
"period": "24h",
"apps_count": 3
}
}
]'