Android > Custom data

Events

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.

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

Managing 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("ad_seen");
Batch.User.trackEvent("tab_clicked", "activity");

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

Managing 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'll 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:

Batch.User.trackTransaction(20.5);

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!

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

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:

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        Batch.User.trackLocation(location);
    }
}

Note: 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.