Track events

Events represent things people do in your app, website, etc, so that you can track your audience’s activity and metrics. Use events to segment your audience, trigger campaigns, and capture usage metrics in your app.

 Our mobile SDKs are in alpha and subject to change

While we’re very excited about our mobile SDKs, they’re still works in progress! If you want to try them out, contact product@customer.io

This page is part of an introductory series to help you get started with the essential features of our SDK. The highlighted step(s) below are covered on this page. Before you continue, make sure you've implemented previous features—i.e. you can't identify people before you initialize the SDK!

graph LR getting-started(Install SDK) -->B(Initialize SDK) B --> identify(identify people) identify -.-> track-events(Send events) identify -.-> push(Receive push) identify -.-> rich-push(Receive Rich Push) track-events --> testing-error-handling(handle errors) push --> testing-error-handling rich-push --> testing-error-handling click getting-started href "/docs/sdk/ios/getting-started" click B href "/docs/sdk/ios/getting-started/#initialize-the-sdk" click identify href "/docs/sdk/ios/identify" click track-events href "/docs/sdk/ios/track-events/" click push href "/docs/sdk/ios/push" click rich-push href "/docs/sdk/ios/rich-push" click testing-error-handling href "/docs/sdk/ios/testing-error-handling" style track-events fill:#B5FFEF,stroke:#007069

Track a custom event

After you identify a person, you can use the track method to send events representing their activities to Customer.io. When you send events, you can include event data—information about the person or the event that they performed.

In Customer.io, you can use events to trigger campaigns and broadcasts. Those campaigns might send someone a push notification or manipulate information associated with the person in your workspace.

Events include the following:

  • name: the name of the event. Most event-based searches in Customer.io hinge on the name, so make sure that you provide an event name that will make sense to other members of your team.
  • data (Optional): The body of the event as an Encodable object. If not specified, data defaults to an empty object—{}.
  • jsonEncoder (Optional): A custom JSONEncoder for the body parameter, overriding our defaults.
  • onComplete: Asynchronous callback with the result of the SDK’s attempt to identify the person who performed the event.
import CioTracking

CustomerIO.shared.track(name: "logged_in", data: ["ip": "127.0.0.1"]){ [weak self] result in
    // It's recommended to use `[weak self]` in the callback. 
    guard let self = self else { return }

    switch result {
    case .success: 
      // Successfully tracked an event in your workspace!
      break 
    case .failure(let customerIOError):
      // An error occurred. You should parse the `customerIOError` to learn more.
      break 
    }
}

// The `data` parameter can be optionally skipped
CustomerIO.shared.track(name: "played_game")
// `data` accepts any `Encodable` object that generates a hash, this could be:
// 1. A dictionary:
let data = ["product": "socks", "price": "23.45"]
CustomerIO.shared.track(name: "purchase", data: data)

// 2. A custom `Encodable` type:
struct Purchase: Encodable {
  let product: String
  let price: Double
}
CustomerIO.shared.track(name: "purchase", data: Purchase(product: "socks", price: 23.45))
Copied to clipboard!