# Analytics for the Android SDK

Track user engagement and app performance with Airship analytics, including custom events, screen tracking, and associated identifiers.

Analytics allows you to track user engagement and app performance through custom events, screen tracking, and associated identifiers.

For information about controlling what data Airship collects, see [Privacy Manager](https://www.airship.com/docs/developer/sdk-integration/android/data-collection/privacy-manager/).

> **Note:** Analytics events are batched and uploaded asynchronously in the background to minimize battery impact. The database size is fixed, so events are safely stored even when offline. Events may not upload immediately and may wait until the next app initialization if the app is closed before the upload completes.


## Custom Events

Track user activities and key conversions with [Custom Events](https://www.airship.com/docs/reference/glossary/#custom_event). They require enabling analytics for your app.


#### Kotlin


```kotlin
customEvent("event_name") {
    addProperty("my_custom_property", "some custom value")
    addProperty("is_neat", true)
    addProperty("any_json", jsonMapOf("foo" to "bar"))
}.track()
```



#### Java


```java
CustomEvent.newBuilder("event_name")
        .setEventValue(123.12)
        .addProperty("my_custom_property", "some custom value")
        .addProperty("is_neat", true)
        .addProperty("any_json", JsonMap.newBuilder()
                .put("foo", "bar")
                .build())
        .build()
        .track();
```




### Templates

Custom Event Templates are a wrapper for Custom Events and are available for Android, [iOS](https://www.airship.com/docs/developer/sdk-integration/apple/analytics/#templates), and [Web](https://www.airship.com/docs/developer/sdk-integration/web/analytics-and-reporting/#templates). See also [CustomEvent](https://www.airship.com/docs/reference/libraries/android-kotlin/latest/urbanairship-core/com.urbanairship.analytics/-custom-event/index.html)
 in the Android SDK library.

#### Account

Use this template to create Custom Events for account-related events. The template is written with account registration as the example.


#### Kotlin



Track a registered account event:

```kotlin
customEvent(AccountEventTemplate.Type.REGISTERED) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = AccountEventTemplate.Type.REGISTERED,
    properties = AccountEventTemplate.Properties(
        category = "premium"
    )
) {
    setEventValue(9.99)
    setTransactionId("12345")
}.track()
```




#### Media

Use this template to create Custom Events for media-related events, including consuming, browsing, starring, and sharing content.


#### Kotlin



Track a consumed content event:

```kotlin
customEvent(MediaEventTemplate.Type.Consumed) { }.track()
```


With an optional value:

```kotlin
customEvent(MediaEventTemplate.Type.Consumed) {
    setEventValue(1.99)
}.track()
```


With optional properties:

```kotlin
customEvent(
    type = MediaEventTemplate.Type.Consumed,
    properties = MediaEventTemplate.Properties(
        id = "12345",
        category = "entertainment",
        type = "video",
        eventDescription = "Watching latest entertainment news.",
        author = "UA Enterprises",
        isFeature = true,
        publishedDate = "August 25, 2016"
    )
) {
    setEventValue(2.99)
}.track()
```





#### Kotlin



Track a starred content event:

```kotlin
customEvent(MediaEventTemplate.Type.Starred) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = MediaEventTemplate.Type.Starred,
    properties = MediaEventTemplate.Properties(
        id = "12345",
        category = "entertainment",
        type = "video",
        eventDescription = "Watching latest entertainment news.",
        author = "UA Enterprises",
        isFeature = true,
        publishedDate = "August 25, 2016"
    )
) {
    setEventValue(2.99)
}.track()
```






#### Kotlin



Track a browsed content event:

```kotlin
customEvent(MediaEventTemplate.Type.Browsed) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = MediaEventTemplate.Type.Browsed,
    properties = MediaEventTemplate.Properties(
        id = "12345",
        category = "entertainment",
        type = "video",
        author = "UA Enterprises",
        isFeature = true,
        publishedDate = "August 25, 2016"
    )
) { }.track()
```





#### Kotlin



Track a shared content event:

```kotlin
customEvent(MediaEventTemplate.Type.Shared()) { }.track()
```


With a source and medium:

```kotlin
customEvent(
    MediaEventTemplate.Type.Shared(source = "facebook", medium = "social")
) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = MediaEventTemplate.Type.Shared(source = "facebook", medium = "social"),
    properties = MediaEventTemplate.Properties(
        id = "12345",
        category = "entertainment",
        type = "video",
        eventDescription = "Watching latest entertainment news.",
        author = "UA Enterprises",
        isFeature = true,
        publishedDate = "August 24, 2016"
    )
) { }.track()
```




#### Retail

Use this template to create Custom Events for retail-related events, including browsing a product, adding an item to a cart, purchasing an item, starring a product, and sharing a product.


#### Kotlin



Track a purchased event:

```kotlin
customEvent(RetailEventTemplate.Type.Purchased) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = RetailEventTemplate.Type.Purchased,
    properties = RetailEventTemplate.Properties(
        id = "12345",
        category = "mens shoes",
        eventDescription = "Low top",
        brand = "SpecialBrand",
        isNewItem = true
    )
) {
    setEventValue(99.99)
    setTransactionId("13579")
}.track()
```






#### Kotlin



Track a browsed event:

```kotlin
customEvent(RetailEventTemplate.Type.Browsed) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = RetailEventTemplate.Type.Browsed,
    properties = RetailEventTemplate.Properties(
        id = "12345",
        category = "mens shoes",
        eventDescription = "Low top",
        brand = "SpecialBrand",
        isNewItem = true
    )
) {
    setEventValue(99.99)
    setTransactionId("13579")
}.track()
```






#### Kotlin



Track an added-to-cart event:

```kotlin
customEvent(RetailEventTemplate.Type.AddedToCart) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = RetailEventTemplate.Type.AddedToCart,
    properties = RetailEventTemplate.Properties(
        id = "12345",
        category = "mens shoes",
        eventDescription = "Low top",
        brand = "SpecialBrand",
        isNewItem = true
    )
) {
    setEventValue(99.99)
    setTransactionId("13579")
}.track()
```





#### Kotlin



Track a starred product event:

```kotlin
customEvent(RetailEventTemplate.Type.Starred) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = RetailEventTemplate.Type.Starred,
    properties = RetailEventTemplate.Properties(
        id = "12345",
        category = "mens shoes",
        eventDescription = "Low top",
        brand = "SpecialBrand",
        isNewItem = true
    )
) {
    setEventValue(99.99)
    setTransactionId("13579")
}.track()
```





#### Kotlin



Track a shared product event:

```kotlin
customEvent(RetailEventTemplate.Type.Shared()) { }.track()
```


With a source and medium:

```kotlin
customEvent(
    RetailEventTemplate.Type.Shared(source = "facebook", medium = "social")
) { }.track()
```


With optional properties:

```kotlin
customEvent(
    type = RetailEventTemplate.Type.Shared(source = "facebook", medium = "social"),
    properties = RetailEventTemplate.Properties(
        id = "12345",
        category = "mens shoes",
        eventDescription = "Low top",
        brand = "SpecialBrand",
        isNewItem = true
    )
) {
    setEventValue(99.99)
    setTransactionId("13579")
}.track()
```




## Associated Identifiers

Associated identifiers (also called custom identifiers) associate an external identifier with a [Channel ID](https://www.airship.com/docs/reference/glossary/#channel_id). They are visible in [Real-Time Data Streaming](https://www.airship.com/docs/reference/glossary/#rtds). We recommend adding any IDs that you may want to be visible in your event stream. You can assign up to 20 associated identifiers to a device. Unlike other identifiers (e.g., tags), you cannot use associated identifiers to target your users.


#### Kotlin


```kotlin
Airship.analytics.editAssociatedIdentifiers {
    addIdentifier("key", "value")
}
```



#### Java


```java
Airship.getAnalytics()
    .editAssociatedIdentifiers()
    .addIdentifier("key", "value")
    .apply();
```




## Screen Tracking

The Airship SDK gives you the ability to track which screens a user views within the application, how long a user stayed on each screen, and also includes the user's previous screen. These events then come through [Real-Time Data Streaming](https://www.airship.com/docs/reference/glossary/#rtds), allowing you to see the path a user took through the application, or trigger actions based on a user visiting a particular area of the application.


#### Kotlin


```kotlin
Airship.analytics.trackScreen("MainScreen")
```



#### Java


```java
Airship.getAnalytics().trackScreen("MainScreen");
```



