# Analytics for the Apple 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/apple/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.


#### Swift


```swift
var event = CustomEvent(name: "event_name", value: 123.12)
try event.setProperties(
    [
        "my_custom_property": "some custom value",
        "is_neat": true,
        "any_json": [
            "foo": "bar"
        ]
    ]
)
event.track()
```



#### Objective-C


```objc
UACustomEvent *event = [[UACustomEvent alloc] initWithName:@"event_name" value:123.12];
[event setProperties: @{
    @"my_custom_property": @"some custom value",
    @"is_neat": @YES,
    @"any_json": @{
        @"foo": @"bar"
    }
} error:&error];
[event track];
```




### Templates

Custom Event Templates are a wrapper for Custom Events and are available for iOS, [Android](https://www.airship.com/docs/developer/sdk-integration/android/analytics/#templates), and [Web](https://www.airship.com/docs/developer/sdk-integration/web/analytics-and-reporting/#templates). See also [CustomEvent](https://urbanairship.github.io/ios-library/v20/AirshipCore/documentation/airshipcore/customevent)
 in the iOS SDK library.

#### Account

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


#### Swift



Track a registered account event:

```swift
let acctEvent = CustomEvent(accountTemplate: .registered)
acctEvent.track()
```


With optional properties:

```swift
var acctEvent = CustomEvent(
    accountTemplate: .registered,
    properties: CustomEvent.AccountProperties(
        category: "Premium",
        isLTV: true
    )
)
acctEvent.eventValue = 9.99
acctEvent.transactionID = "12345"
acctEvent.track()
```




#### Media

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


#### Swift



Track a consumed content event:

```swift
let mediaEvent = CustomEvent(mediaTemplate: .consumed)
mediaEvent.track()
```


With an optional value:

```swift
var mediaEvent = CustomEvent(
    mediaTemplate: .consumed,
    properties: CustomEvent.MediaProperties(isLTV: true)
)
mediaEvent.eventValue = 1.99
mediaEvent.track()
```


With optional properties:

```swift
var mediaEvent = CustomEvent(
    mediaTemplate: .consumed,
    properties: CustomEvent.MediaProperties(
        id: "12322",
        category: "entertainment",
        type: "video",
        eventDescription: "Watching latest entertainment news.",
        author: "UA Enterprises",
        isFeature: true,
        isLTV: true
    )
)
mediaEvent.eventValue = 2.99
mediaEvent.track()
```





#### Swift



Track a starred content event:

```swift
let mediaEvent = CustomEvent(mediaTemplate: .starred)
mediaEvent.track()
```


With optional properties:

```swift
var mediaEvent = CustomEvent(
    mediaTemplate: .starred,
    properties: CustomEvent.MediaProperties(
        id: "12322",
        category: "entertainment",
        type: "video",
        eventDescription: "Watching latest entertainment news.",
        author: "UA Enterprises",
        isFeature: true
    )
)
mediaEvent.eventValue = 2.99
mediaEvent.track()
```






#### Swift



Track a browsed content event:

```swift
let mediaEvent = CustomEvent(mediaTemplate: .browsed)
mediaEvent.track()
```


With optional properties:

```swift
let mediaEvent = CustomEvent(
    mediaTemplate: .browsed,
    properties: CustomEvent.MediaProperties(
        id: "12322",
        category: "entertainment",
        type: "video",
        eventDescription: "Browsed latest entertainment news.",
        author: "UA Enterprises",
        isFeature: true
    )
)
mediaEvent.track()
```





#### Swift



Track a shared content event:

```swift
let mediaEvent = CustomEvent(mediaTemplate: .shared)
mediaEvent.track()
```


With a source and medium:

```swift
let mediaEvent = CustomEvent(
    mediaTemplate: .shared(source: "facebook", medium: "social")
)
mediaEvent.track()
```


With optional properties:

```swift
var mediaEvent = CustomEvent(
    mediaTemplate: .shared(source: "facebook", medium: "social"),
    properties: CustomEvent.MediaProperties(
        id: "1234",
        category: "entertainment",
        type: "video",
        eventDescription: "Watching latest entertainment news.",
        author: "UA Enterprises",
        isFeature: true
    )
)
mediaEvent.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.


#### Swift



Track a purchased event:

```swift
let retailEvent = CustomEvent(retailTemplate: .purchased)
retailEvent.track()
```


With optional properties:

```swift
var retailEvent = CustomEvent(
    retailTemplate: .purchased,
    properties: CustomEvent.RetailProperties(
        id: "1234",
        category: "mens shoe",
        eventDescription: "Low top",
        isLTV: true,
        brand: "SpecialBrand",
        isNewItem: true
    )
)
retailEvent.eventValue = 99.99
retailEvent.transactionID = "13579"
retailEvent.track()
```






#### Swift



Track a browsed event:

```swift
let retailEvent = CustomEvent(retailTemplate: .browsed)
retailEvent.track()
```


With optional properties:

```swift
var retailEvent = CustomEvent(
    retailTemplate: .browsed,
    properties: CustomEvent.RetailProperties(
        id: "1234",
        category: "mens shoe",
        eventDescription: "Low top",
        brand: "SpecialBrand",
        isNewItem: true
    )
)
retailEvent.eventValue = 99.99
retailEvent.transactionID = "13579"
retailEvent.track()
```






#### Swift



Track an added-to-cart event:

```swift
let retailEvent = CustomEvent(retailTemplate: .addedToCart)
retailEvent.track()
```


With optional properties:

```swift
var retailEvent = CustomEvent(
    retailTemplate: .addedToCart,
    properties: CustomEvent.RetailProperties(
        id: "1234",
        category: "mens shoe",
        eventDescription: "Low top",
        brand: "SpecialBrand",
        isNewItem: true
    )
)
retailEvent.eventValue = 99.99
retailEvent.transactionID = "13579"
retailEvent.track()
```





#### Swift



Track a starred product event:

```swift
let retailEvent = CustomEvent(retailTemplate: .starred)
retailEvent.track()
```


With optional properties:

```swift
var retailEvent = CustomEvent(
    retailTemplate: .starred,
    properties: CustomEvent.RetailProperties(
        id: "1234",
        category: "mens shoe",
        eventDescription: "Low top",
        brand: "SpecialBrand",
        isNewItem: true
    )
)
retailEvent.eventValue = 99.99
retailEvent.transactionID = "13579"
retailEvent.track()
```





#### Swift



Track a shared product event:

```swift
let retailEvent = CustomEvent(retailTemplate: .shared())
retailEvent.track()
```


With a source and medium:

```swift
let retailEvent = CustomEvent(
    retailTemplate: .shared(source: "facebook", medium: "social")
)
retailEvent.track()
```


With optional properties:

```swift
var retailEvent = CustomEvent(
    retailTemplate: .shared(source: "facebook", medium: "social"),
    properties: CustomEvent.RetailProperties(
        id: "1234",
        category: "mens shoe",
        eventDescription: "Low top",
        brand: "SpecialBrand",
        isNewItem: true
    )
)
retailEvent.transactionID = "13579"
retailEvent.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.


#### Swift


```swift
let identifiers = Airship.analytics.currentAssociatedDeviceIdentifiers()
identifiers.set(identifier: "value", key:"key")
Airship.analytics.associateDeviceIdentifiers(identifiers)
```



#### Objective-C


```objc
UAAssociatedIdentifiers *identifiers = [UAirship.analytics currentAssociatedDeviceIdentifiers];
[identifiers setIdentifier:@"value" forKey:@"key"];
[UAirship.analytics associateDeviceIdentifiers:identifiers];
```




## 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.


#### Swift


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



#### Objective-C


```objc
[UAirship.analytics trackScreen:@"MainScreen"];
```




