At Segment, we’re working hard to make our mobile SDKs the best possible collection options for your analytics data. An SDK can make your data more durable, minimize data transfer, and optimize your app’s battery usage. In this article, we’ll take you through what happens under the hood as a piece of data flows through our iOS and Android SDKs from a button handler in your app all the way through to our API.
When you initialize Segment’s iOS and Android library, it will automatically start tracking a few app lifecycle events that are part of our Native Mobile Spec.
Application Installed— User installs your application for the first time.
Application Opened— User opens your application.
Application Updated— User updates to a newer version of your app.
The automatic lifecycle tracking allows you to focus on building your app and thinking less about your analytics tracking plan.
Now that we’ve initialized the library, we’re ready for user interaction.
Let’s take a look at a fictitious mobile app, Bluth’s Banana Stand, and see what happens with our SDKs when the user completes an order.
Listening on the purchase button handler, you’ll fire off a
It’s safe to invoke this call directly from your button handler, as our SDKs automatically dispatch it to a background queue thread.
These queues represent various threads on the mobile device. The .track() event is moved to the background queue so the UI queue can continue to operate for the user.
Inside the Queue
Once dispatched to the background thread, the event is immediately written to the device’s disk to maximize the chances of deliverability. In a world where power can be lost at any time, this method maximizes the durability of your data. (You can read about why we chose QueueFile for reliable request batching on Android).
The Road to Segment’s API
On the way out of the queue and onto Segment’s tracking API, Segment’s SDKs use two different strategies to optimize your app’s bandwidth and power usage:
Batching — Combining multiple events into each outbound request allows us to minimize the amount of times we power up the wireless hardware
Compression — Compressing each request with gzip which allows us to drastically reduce the amount of bandwidth used
Before the request is sent outbound, the event is now batched together with other events before being sent to our servers. Our SDKs minimize battery use with auto-batching, since powering up the radio on every call can waste battery life. Auto-batching allows our SDKs to send events in batches, without powering up the radio on every call.
Visual representation of batched events, with the green circle representing a
We batch our requests to minimize the number of times we access the underlying hardware. This allows us to send fewer requests, spaced further apart, and therefore allow wireless hardware to remain offline for majority of the time.
Next, we’ll compress the outbound request body with gzip, reducing the total size drastically.
In our testing, we sent one thousand
.track() calls without the SDK (directly to our API) and with the SDK. The amount of data on the wire without the SDK was 1.1mb and with the SDK is 63kb — roughly 17x saving in bandwidth. We’re able to accomplish this via intelligent batching and aggressive data “middle-out compression” technologies.
These are the bandwidth impacts realized with our SDKs’ batching and compression.
The battery story is even better—we’re able to reduce the wasted energy by almost 3x from 56% overhead to 20% overhead. The net result of this is low average energy impact on the app and much more efficient battery usage—and happier users!
These are the battery enhancements our SDKs enable. (Xcode).
Finally, the request leaves the mobile device and makes its way to our servers.
By tracing the SDKs functionality from tap to API, we’re able to see how a few simple data transfer strategies can significantly strengthen your mobile analytics stack:
Increase durability — Immediately persist every message to a disk-backed queue, preventing you from losing data in the event of battery or network loss.
Auto-Retries — If the network is spotty or not available, our SDKs will retry transferring the batch until the request is successful. This drastically improves data deliverability.
Reduce Network Usage — Each batch is gzip compressed, decreasing the amount of bytes on the wire by 10x-20x.
Save Battery — Because of data batching and compression, Segment’s SDKs reduce energy overhead by 2-3x which means longer battery life for your app’s users.
These features would not have been possible without the help of our incredibly supportive open source community. With 80+ contributors, 550+ pull requests, 240+ releases, our iOS and Android libraries just kept improving. Thanks to their work, more than 3,000 apps now collect customer data using Segment’s mobile SDKs every day.
We can’t wait to keep improving the mobile analytics ecosystem alongside you. If you have any ideas, we’re all ears! Send a pull request or email us at email@example.com. 😃
Want to learn about and grow your mobile users? Sign up today.
Interested in building infrastructural components for mobile apps and SDKs? We’re hiring.