# Analytics

PRESTOplay provides plugin integrations for Youbora, Broadpeak, Conviva, and Mux.

For each backend, the setup flow is the same:

1. Register the analytics plugin during `PRESTOplaySDK.shared.setup(...)`.
2. Create a player.
3. Create an analytics instance for that player.
4. Assign backend-specific metadata.

## Youbora

### Integrate

```swift
import PRESTOplay
import CastlabsYoubora
import NpawPlugin

let analyticsOptions = NpawPlugin.AnalyticsOptions()
analyticsOptions.userName = "User Name"

let youboraSettings = YouboraSettings(
    accountCode: "YOUR-ACCOUNT-CODE",
    analyticsOptions: analyticsOptions)

_ = PRESTOplaySDK.shared.setup(
    "LICENSE",
    [
        HLSPlugin(),
        YouboraPlugin(youboraSettings),
    ])
```

### Analytics instance and metadata

```swift
import PRESTOplay
import CastlabsYoubora
import NpawPlugin

let player = PRESTOplaySDK.shared.player()
guard var analytics = PRESTOplaySDK.shared.analytics(for: player, .youbora) else {
    return
}

let metadata = YouboraMetadata(live: false, assetId: "assetId")
metadata.accountCode = "YOUR-ACCOUNT-CODE"

let options = NpawPlugin.AnalyticsOptions()
options.userName = "User Name"
options.contentTitle = "Title"
options.autoDetectBackground = false
metadata.analyticsOptions = options

analytics.metadata = metadata
```

Set `autoDetectBackground = false` to keep the session active when switching to Picture in Picture.

`YouboraMetadata` supports Youbora-specific fields such as `accountCode` and `analyticsOptions`.

```swift
let metadata = YouboraMetadata(live: false, assetId: "assetId")
metadata.accountCode = "YOUR-ACCOUNT-CODE"

let options = NpawPlugin.AnalyticsOptions()
options.userName = "User Name"
options.contentTitle = "Title"
options.adCustomDimension1 = "campaign-a"
options.appName = "Your App"
options.appReleaseVersion = "1.0.0"
metadata.analyticsOptions = options
```

## Broadpeak

### Integrate

```swift
import PRESTOplay
import CastlabsBroadpeak

let broadpeakSettings = BroadpeakSettings(
    analyticsAddress: "YOUR-ANALYTICS-SERVER-ADDRESS",
    nanoCDNHost: "",
    domainNames: "")

_ = PRESTOplaySDK.shared.setup(
    "LICENSE",
    [
        HLSPlugin(),
        BroadpeakPlugin(broadpeakSettings),
    ])
```

### Analytics instance and metadata

```swift
import PRESTOplay
import CastlabsBroadpeak

let player = PRESTOplaySDK.shared.player()
guard var analytics = PRESTOplaySDK.shared.analytics(for: player, .broadpeak) else {
    return
}

let metadata = BroadpeakMetadata(live: false, assetId: "assetId")
analytics.metadata = metadata
```

`BroadpeakMetadata` also provides `options` for Broadpeak-specific streaming session settings.

```swift
import PRESTOplay
import CastlabsBroadpeak

let player = PRESTOplaySDK.shared.player()
guard var analytics = PRESTOplaySDK.shared.analytics(for: player, .broadpeak) else {
    return
}

let metadata = BroadpeakMetadata(live: false, assetId: "assetId")
metadata.options = [
    .timeoutNanoCDNDiscoverResolving: Int32(100),
    .timeoutNanoCDNStaticResolving: Int32(200),
    .timeoutNanoCDNRequestRouter: Int32(300),
]
analytics.metadata = metadata
```

## Conviva

### Integrate

Include Conviva in your app (for example via CocoaPods):

```ruby
pod 'ConvivaSDK'
```

Then register the plugin:

```swift
import PRESTOplay
import CastlabsConviva

let convivaSettings = ConvivaSettings(
    customerKey: "YOUR-CUSTOMER-KEY",
    gatewayUrl: "YOUR-GATEWAY-URL")

_ = PRESTOplaySDK.shared.setup(
    "LICENSE",
    [
        HLSPlugin(),
        ConvivaPlugin(convivaSettings),
    ])
```

### Analytics instance and metadata

```swift
import PRESTOplay
import CastlabsConviva

let player = PRESTOplaySDK.shared.player()
guard var analytics = PRESTOplaySDK.shared.analytics(for: player, .conviva) else {
    return
}

let metadata = ConvivaMetadata(live: false, assetId: "conviva_assetId")
metadata.assetName = "Title"
analytics.metadata = metadata
```

`ConvivaMetadata` exposes Conviva-specific metadata. A common field is `assetName`:

```swift
let metadata = ConvivaMetadata(live: false, assetId: "conviva_assetId")
metadata.assetName = playerConfiguration.metaData?.title
analytics.metadata = metadata
```

## Mux Data

### Integrate

Include Mux in your app (for example via CocoaPods):

```ruby
pod 'Mux-Stats-Core'
```

Then register the plugin:

```swift
import PRESTOplay
import CastlabsMux

let muxSettings = MuxSettings(environmentKey: "YOUR-ENV-KEY")

_ = PRESTOplaySDK.shared.setup(
    "LICENSE",
    [
        HLSPlugin(),
        MuxPlugin(muxSettings),
    ])
```

### Analytics instance and metadata

```swift
import PRESTOplay
import CastlabsMux
import MuxCore

let player = PRESTOplaySDK.shared.player()
guard var analytics = PRESTOplaySDK.shared.analytics(for: player, .mux) else {
    return
}

guard let playerData = MUXSDKCustomerPlayerData(environmentKey: "YOUR-ENV-KEY") else {
    return
}

let videoData = MUXSDKCustomerVideoData()
videoData.videoTitle = "Video Title"

let customerData = MUXSDKCustomerData()
customerData.customerPlayerData = playerData
customerData.customerVideoData = videoData

let metadata = MuxMetadata(live: false, assetId: "assetId")
metadata.customerData = customerData
analytics.metadata = metadata
```

`MuxMetadata` accepts `customerData` from `MuxCore`:

```swift
let metadata = MuxMetadata(live: false, assetId: "assetId")
metadata.customerData = customerData
analytics.metadata = metadata
```

Mux metadata fields are defined by the Mux SDK. See:

- https://docs.mux.com/guides/monitor-avplayer
- https://docs.mux.com/guides/make-your-data-actionable-with-metadata

## Shared analytics controls

The analytics interface also supports:

- `reportWarnings` to include warning-level reports.
- `filterOutgoingErrorReport` to modify or drop outgoing error reports.

```swift
analytics.reportWarnings = true
analytics.filterOutgoingErrorReport = { error in
    error.toAnalyticsErrorReport()
}
```
