This document describes how you can manage dispatching data to Google Analytics using the Google Analytics SDK for Android v2.
Overview
In the Google Analytics SDK for Android, collected data like screen views or events are stored locally in a queue before being sent to the Google Analytics servers. The process by which these pieces of data (referred to here as "hits") are sent from the SDK to Google Analytics is known as dispatching.
Dispatching is unique to the mobile collections libraries and is designed to mitigate the challenges of unreliable network access and limited battery life.
There are two types of dispatching:
- Periodic dispatch – automatically dispatches hits at a
recurring interval that you specify either
programmatically or in your
analytics.xml
file. - Manual dispatch – manually dispatch hits to send data when it is convenient for you, for example when there is an existing HTTP connection.
Both types of dispatch occur off the main UI thread in version 2 of the SDK
The remainder of this document will provide a more in-depth look at each type of dispatch and how to implement them in your app.
Periodic Dispatch
As your app collects GA data, that data is added to a queue and periodically dispatched to Google Analytics. Periodic dispatch can occur either when your app is running in the foreground or the background.
The default dispatch period is
30 minutes
.
You can provide your own interval in seconds by
using the ga_dispatchPeriod
parameter in your analytics.xml
file, or by calling setDispatchPeriod(int dispatchPeriodInSeconds)
as in this example:
In your analytics.xml
file:
<integer name="ga_dispatchPeriod">60</integer>
Programmatically:
GAServiceManager.getInstance().setDispatchPeriod(60);
Setting a negative value will disable periodic dispatch, requiring that
you use manual dispatch if you want to send any data
to Google Analytics. On the other hand, setting a value of 0
will
dispatch each hit immediately if a network connection is available.
Once all hits have been dispatched, periodic dispatch will enter a power save mode and be disabled until another send call is made.
If a user loses network access or quits your app while there are still hits waiting to be dispatched, those hits are persisted in local storage. They will be dispatched the next time your app is running and dispatch is called.
Manual Dispatch
Apart from relying on periodic dispatch, there may be times when you want to manually dispatch your hits. For example, you could bundle your dispatches with other HTTP requests made by your application to reduce overhead.
Hits can be manually dispatched using
the GAServiceManager
instance:
GAServiceManager.getInstance().dispatch();