Flushing Events Periodically
Detailed steps to flush events periodically from the RudderStack SDK’s internal database to Rudder Server.
RudderStack supports periodic flushing of events from the Android SDK to RudderStack, irrespective of whether your app is opened or not. This ensures that the events that are triggered just before closing the app are sent from the device - even if the app is not opened by the user for a long time.
To use this feature, follow these steps:
- Open your
app/build.gradle
and add the WorkManager dependency underdependencies
as shown below:
implementation 'androidx.work:work-runtime:2.7.1'
- Use
withFlushPeriodically()
function while passing theRudderConfig
object to thegetInstance()
API, as shown below:
rudderClient = RudderClient.getInstance( this, <WRITE_KEY>, RudderConfig.Builder() .withDataPlaneUrl(<DATA_PLANE_URL>) // enabling periodical flush of events in the DB with a repeat interval of 15 minutes .withFlushPeriodically(15, TimeUnit.MINUTES) .build())
withFlushPeriodically
parameters
The withFlushPeriodically
function takes two parameters:
- The first parameter is the number representing the time interval for the flushing to be repeated.
- The second parameter is the time unit of the repeat interval. It can be Minutes, Hours, or Days. The minimum value of the repeat interval is 15 minutes.
Initializing WorkManager on demand
By default, the WorkManager is initialized on the startup of the application. However, you can initialize it only on demand by following the below steps:
- Add the below
provider
to the application tag of your application’sAndroidManifest.xml
.
<provider android:name="androidx.startup.InitializationProvider" android:authorities="${applicationId}.androidx-startup" android:exported="false" tools:node="merge"> <!-- If you are using androidx.startup to initialize other components --> <meta-data android:name="androidx.work.WorkManagerInitializer" android:value="androidx.startup" tools:node="remove" /> </provider>
- Implement
Configuration.Provider
and define the functiongetWorkManagerConfiguration()
in your application class as shown below:
class MyApplication(): Application(), Configuration.Provider {
override fun getWorkManagerConfiguration() = Configuration.Builder() .setMinimumLoggingLevel(android.util.Log.INFO) .build()}
Contact us
For queries on any of the sections covered in this guide, you can contact us or start a conversation in our Slack community.