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.
- Newly tracked events are hidden by default. You will need to manually display them from the dashboard settings > "Custom data" tab.

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 NSString that can't be longer than 200 bytes (optional).
  • A custom data object can be attached. See the section "Event Data", right under this one.

Here's are some examples:

  • Swift
  • Objective-C
BatchUser.trackEvent("ad_seen")
BatchUser.trackEvent("tab_clicked", withLabel: "activity")

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

Event data

Starting with Batch SDK 1.13.0, you can attach custom data to events using BatchEventData. You will then use this event when calling BatchUser.trackEvent(). It is very similar to BatchUserDataEditor.

It supports setting the following data:

  • Tags: a collection of string values
    • They can't be longer than 64 characters, empty or null.
    • The SDK will automatically lowercase them, so two same strings with different casing does not count as two different tags.
  • Attributes: key/value pairs of data
    • Attributes associate values to keys.
    • Keys should be made of letters, numbers or underscores ([a-z0-9_]) and can't be longer than 30 characters.
      They will be lowercased, so trying to use the same key with different casing will overwrite the previously set value.
    • Values can be of the following types:
      • Strings, not longer than 64 characters, can't be empty or null.
      • Booleans
      • Floats/Doubles
      • NSInteger
      • Dates 1.15.1
      • NSURLs 1.18, not longer than 2048 characters and must follow the format scheme://[authority][path][?query][#fragment].

Events support at most:

1.15.1 10 tags and 15 attributes.

1.13.0 10 tags and 10 attributes.

Any attempt to add an invalid tag or attribute will fail. Setting a value for an existing key will overwrite it.

Example:

  • Swift
  • Objective-C
let data = BatchEventData()
data.add(tag: "sports")
data.add(tag: "squash")
data.add(tag: "daily_digest")

data.put(true, forKey: "premium")
data.put("123456", forKey: "id")
BatchUser.trackEvent("read_article", withLabel: nil, data: data)

Note: Event data was available before Batch SDK 1.13.0, and worked with a plain NSDictionary object. This method has been deprecated: BatchEventData must now be used to describe your custom data: see the Batch 1.13 migration guide for more info.

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.

Here's an example:

  • Swift
  • Objective-C
BatchUser.trackTransaction(withAmount: 20.5)

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 CoreLocation's standard CLLocation object, which you usually get from the CoreLocation itself. You can also instanciate one manually from a latitude/longitude.

Here's an example:

  • Swift
  • Objective-C
// let location: CLLocation = [...]
BatchUser.trackLocation(location)

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.

Background events

1.18.0 Events can be sent while the application is in the background by asking UIApplication to begin a background task.
Once the event has been sent to the server, Batch will emit a BatchEventTrackerFinishedNotification NSNotification.

Please note that this notification might be sent multiple times: you may want to dynamically add the observer and remove it once your event has been tracked.

Here is a sample implementation:

@objc
class BackgroundEventSender : NSObject {
    var eventBackgroundEventTaskID: UIBackgroundTaskIdentifier?
    
    override init() {
        super.init()
        NotificationCenter.default.addObserver(self, selector: #selector(finishBackgroundTask), name: Notification.Name.BatchEventTrackerFinished, object: nil)
    }
    
    func trackBackgroundEvent() {
        guard eventBackgroundEventTaskID == nil else {
            // This sample code doesn't support tracking multiple events at once
            return
        }
        
        eventBackgroundEventTaskID = UIApplication.shared.beginBackgroundTask(withName: "batch_background_event", expirationHandler: self.finishBackgroundTask)
        
        BatchUser.trackEvent("SAMPLE_BACKGROUND_EVENT")
    }
    
    @objc
    func finishBackgroundTask() {
        guard let task = eventBackgroundEventTaskID else {
            return
        }
        self.eventBackgroundEventTaskID = nil
        UIApplication.shared.endBackgroundTask(task)
    }
}