# 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

    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

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

    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

    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

    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.

    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):

    pod 'ConvivaSDK'

Then register the plugin:

    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

    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`:

    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):

    pod 'Mux-Stats-Core'

Then register the plugin:

    import PRESTOplay
    import CastlabsMux

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

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

### Analytics instance and metadata

    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`:

    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.

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