Android > Custom data


Batch allows you to track events that happen in your application. They automatically keep track of their count, the last time it happened and their value.

Please read our guide on custom data before tagging your app.

Tracking events

Events are easy to use, but have some rules:

  • Event names are strings. They should be made of letters, numbers or underscores ([a-z0-9_]) and can't be longer than 30 characters.
  • They can have a label, which is a string with no limitations (optional).
  • A custom data object can be attached. It must be an instance of JSONObject. Though it will be collected by Batch, it cannot be used for targeting purposes at the moment (optional).

Here's an example:

Batch.User.trackEvent("tab_clicked", "activity");

JSONObject data = new JSONObject();
data.put("utm_campaign", "thanksgiving_promo");
Batch.User.trackEvent("ad_seen", null, data);

Please test your implementation using our debug tool before releasing your app on the store.

Tracking transactions

We also have a specialized kind of events: Transactions. They let you track a transaction of a certain amount, without any currency attached: You will need to make sure you only track comparable values.

They also have some rules:

  • Amount should be a double.
  • A custom data object is supported, just like standard events (optional).

Here's an example:


JSONObject data = new JSONObject();
data.put("item", "100gold");
Batch.User.trackTransaction(20.5, data);

Make sure you start Batch before or after tracking your events. Otherwise, they will not be sent!

You can use this data to target users who already have or haven't made any transactions in your app yet from the dashboard. Batch also displays the income generated in your app in Batch analytics.

Tracking user location

Starting with Batch 1.8, you can now natively track a user location. This uses Android's standard Location object, which you usually get from the Location Service or Fused Location API. You can also instanciate one manually from a latitude/longitude.

Here's an example:

public void onLocationChanged(Location location) {
    if (location != null) {

This data will allow you to send geotargeted push notifications from the dashboard or the Campaigns API.

The SDK will throttle location tracking to optimize network and battery usage. You can track one location event every 30 seconds, any attempt at updating the location sooner will be ignored by the SDK.