このページでは

For AI agents: a documentation index is available at /docs/llms.txt. Append .md to any page URL for markdown, or send Accept: text/markdown.

セッションリプレイ iOS ミドルウェア

Early access SDK

As an Alpha release, this SDK may contain bugs and cause crashes. Before you enable in production, thoroughly test your app in a controlled environment. For more information about best practices for developer preview SDKs, see SDK Maintenance and Support.

この記事では、iOS SDKミドルウェアを使用したセッションリプレイのインストールについて説明します。アプリがすでに(メンテナンス用の)Amplitude SDKを使用している場合は、このオプションを使用してください。

アプリがすでに(最新)iOS Swift SDKを使用している場合は、セッションリプレイiOS SDKプラグインを選択してください。

Analytics-Swift SDKとAmplitude(アクション)の送信先経由でセグメントを使用する場合は、セグメントプラグインを選択してください。

製品内アナリティクスにAmplitude以外のプロバイダーを使用している場合は、スタンドアロンの実装を選択してください。

Session Replay and performance

Amplitude built Session Replay to minimize impact on the performance of the iOS apps in which it's installed by:

  • Asynchronously processing replay data, to avoid blocking the main user interface thread. The main thread must be used to interact with the view hierarchy, but all processing is performed on a background queue.
  • Using batching and lightweight compression to reduce the number of network connections and bandwidth.
  • Optimizing view hierarchy processing. Contact Amplitude if you experience issues with hierarchy processing.

始める前に

セッションリプレイミドルウェアには以下が必要です。

  1. アプリケーションは iOS または iPadOS 上で実行されます。
  2. (メンテナンス)Amplitude iOS SDKの8.22.0以上を使用していること。
  3. デバイスIDをSDKに提供できること。

Supported iOS versions

Session Replay supports a minimum target version of iOS 13.

クイックスタート

プロジェクトの依存関係に最新バージョンのミドルウェアを追加してください。

セッションリプレイをPackage.swiftファイルの依存関係として追加するか、Xcodeのパッケージリストに含めます。

swift
dependencies: [
    .package(url: "https://github.com/amplitude/AmplitudeSessionReplay-iOS", from: "0.12.8")
]

Amplitude-iOSと統合する場合は、AmplitudeiOSSessionReplayMiddlewareターゲットを使用してください。

swift
.product(name: "AmplitudeiOSSessionReplayMiddleware", package: "AmplitudeSessionReplay")

アプリケーションコードを設定します。

swift
import Amplitude
import AmplitudeiOSSessionReplayMiddleware
// Initialize Amplitude Analytics SDK instance
let amplitude = Amplitude.instance()
// Although not required, we recommend enabling session start and end events when enabling Session Replay
amplitude.defaultTracking.sessions = true
// Create and Install Session Replay Middleware
// Recording will be handled automatically
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(sampleRate: 0.1))
amplitude.initializeApiKey(API_KEY)

設定

セッションリプレイミドルウェアを初期化する際には、次のオプションを渡します。

Block on-screen data

Session Replay supports three ways to block sensitive on-screen data. Masking setting for WebViews is in the Web view support section.

Mask level

Session Replay for iOS supports three levels of masking, configurable with the maskLevel option.

Use this option in the Session Replay configuration.

Privacy methods for UIKit

Session Replay provides an extension on UIView to manage privacy. Import the Session Replay library to access it.

swift
import AmplitudeSessionReplay

Privacy Modifiers for SwiftUI

Session Replay provides an extension on View to manage privacy. Import the Session Replay Library to access it.

swift
import AmplitudeSessionReplay

ユーザーのオプトアウト

セッションリプレイミドルウェアはAmplitude-iOS SDKのoptOut設定に従っており、単独ではユーザーのオプトアウトをサポートしていません。

swift
// Set optOut on the Amplitude SDK
amplitude.optOut = true
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(/* session replay options */))

EU data residency

Session Replay is available to Amplitude Customers who use the EU data center. Set the serverZone configuration option to EU during initialization. For example:

swift
// Set serverZone on the Amplitude SDK
amplitude.setServerZone(.EU)
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(/* session replay options */))

Sampling rate

By default, Session Replay captures 0% of sessions for replay. Use your SDK's sampleRate configuration option to set the proportion of sessions to capture.

For manual and dynamic sampling, quota planning, and quota accounting, go to Configure Session Replay sampling.

swift
// This configuration samples 1% of all sessions
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(sampleRate: 0.01))

録画品質

再生の忠実度と、パフォーマンスおよびストレージのバランスを調整するには、品質プロファイルを選択します。プロファイルが小さいほど、キャプチャフレームレートが低くなり、画像解像度も低くなります。プロファイルが高いほど、フレームレートと解像度が高くなります。SDKがデバイスに基づいてプロファイルを選択できるようにする場合は、QualityProfile.automaticを使用します(例:新しいデバイスでは高く、古いデバイスでは低くするなど)。

swift
// Use automatic profile selection based on device
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(
    sampleRate: 0.1,
    quality: .automatic
))
// Or set a fixed profile (low, medium, or high)
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(
    sampleRate: 0.1,
    quality: .medium
))

従量課金制ネットワークでのアップロードを無効にする

デバイスが従量課金制ネットワークに接続されている間は、セッションリプレイのアップロードを一時停止し、ユーザーのモバイルデータの使用を避けてください。セッションリプレイは引き続きデータをローカルに記録します。デバイスがWi-Fi、またはその他の従量制ではない接続に再接続されると、アップロードが再開されます。

swift
amplitude.addEventMiddleware(AmplitudeiOSSessionReplayMiddleware(
    sampleRate: 0.1,
    uploadConfig: UploadConfig(disableMeteredUploads: true)
))

リプレイ収集を無効にする

有効化された後、セッションリプレイは次のいずれかまでアプリ上で実行されます:

  • ユーザーがアプリを離れる。
  • sessionReplayMiddleware.stop()呼び出します。
  • このミドルウェアはamplitude.removeEventMiddleware(sessionReplayMiddleware)を使用することでSDKから削除できます。

ユーザーがアプリの制限付きエリアに移動する前にsessionReplayMiddleware.stop()を呼び出して、ユーザーがそのエリアにいる間のリプレイ収集を無効にします。

参照の保持

これには、セッションリプレイ ミドルウェアインスタンス let sessionReplayMiddleware = AmplitudeiOSSessionReplayMiddleware(/* session replay options */)への参照を保持する必要があります。

ユーザーがアプリの制限のないエリアに戻ったときに、sessionReplayMiddleware.start()を呼び出してリプレイ収集を再度有効にします。

特定の画面のみをキャプチャするには、起動時にミドルウェアが起動しないようにautoStart: falseでミドルウェアを初期化し、ユーザーがこれらの画面に出入りする際にstart()とstop()を呼び出します。

swift
// Add the middleware once, without starting capture
let sessionReplayMiddleware = AmplitudeiOSSessionReplayMiddleware(sampleRate: 1.0, autoStart: false)
amplitude.addEventMiddleware(sessionReplayMiddleware)
// Start capture when the user enters a screen you want to record
sessionReplayMiddleware.start()
// Stop capture when the user leaves that screen
sessionReplayMiddleware.stop()

また、Amplitude Experimentなどのフィーチャーフラグプロダクトを使用して、場所などの基準に基づいてリプレイ収集を有効または無効にするロジックを作成することもできます。たとえば、特定のユーザーグループを対象とするフィーチャーフラグを作成し、それを初期化ロジックに追加できます。

swift
import Amplitude
import AmplitudeiOSSessionReplayMiddleware
// Your existing initialization logic with Amplitude-iOS SDK
let amplitude = Amplitude.instance()
if (nonEUCountryFlagEnabled) {
  // Create and Install Session Replay Middleware
  let sessionReplayMiddleware = AmplitudeiOSSessionReplayMiddleware(sampleRate: 0.1)
  amplitude.addEventMiddleware(sessionReplayMiddleware)
}
amplitude.initializeApiKey(API_KEY)

Web view support (Beta)

By default, Session Replay blocks web views in a capture. To enable capture of all WebView components, set captureWebViews to true in the Session Replay configuration.

To mask specific WebView components, use the privacy methods described in Block on-screen data.

swift
// UIKit
webView.amp_isBlocked = false

Map View Support (Alpha)

Session Replay supports capturing map views in iOS applications. To enable map view capture, unmask the map view in your implementation.

swift
// UIKit
mapView.amp_isBlocked = false

// SwiftUI
Map().amp_setBlocked(false)

Log Recording

Availability

AmplitudeSessionReplay 0.8.0 and later support log recording.

Session Replay supports recording logs in iOS applications. Configure the limits with recordLogOptions when you initialize the integration, then send log messages through recordLog(level:message:date:).

import AmplitudeSessionReplay

let sessionReplay = SessionReplay(
    apiKey: API_KEY,
    recordLogOptions: .init(logCountThreshold: 2000, maxMessageLength: 4000)
)

sessionReplay.recordLog(level: .error, message: "This is an error log")

If you have implemented a log system, you can make single-point modifications to integrate log recording functionality. See the following examples for more information.

CocoaLumberjack

If you use CocoaLumberjack, you can integrate it with a custom logger provider.

swift
class AmplitudeLogRecordLogger: DDAbstractLogger {

    private weak var plugin: SessionReplayPlugin?

    init(_ plugin: SessionReplayPlugin) {
        self.plugin = plugin
    }

    override func log(message logMessage: DDLogMessage) {
        let recordLevel: RecordLogLevel
        switch logMessage.flag {
        case .error:
            recordLevel = .error
        case .warning:
            recordLevel = .warn
        case .info:
            recordLevel = .log
        default:
            return
        }
        plugin?.recordLog(level: recordLevel,
                          message: logMessage.message,
                          date: logMessage.timestamp)
    }
}

let sessionReplayPlugin = AmplitudeSwiftSessionReplayPlugin()
amplitude.add(plugin: sessionReplayPlugin)

DDLog.add(AmplitudeLogRecordLogger(sessionReplayPlugin))

React Native

If you use console log or react-native-logs with consoleTransport, you can integrate it with RCTAddLogFunction.

swift
RCTAddLogFunction { level, source, fileName, lineNumber, message in
  let recordLevel: RecordLogLevel
  switch level {
  case .error:
    recordLevel = .error
  case .warning:
    recordLevel = .warn
  case .info:
    recordLevel = .log
  case .fatal:
    recordLevel = .error
  case .trace:
    recordLevel = .init("trace")
  default:
    return
  }
  self.sessionReplayPlugin.recordLog(level: recordLevel, message: message)
}

Data retention, deletion, and privacy

Review consent guidance when you implement capture.

Retention period

For native and Flutter SDK retention guidance and the effective date of changes, go to the Session Replay data lifecycle reference.

DSAR API

For replay metadata returned by the DSAR API, go to Session Replay DSAR metadata.

Data deletion

For user and project deletion behavior, go to Session Replay data deletion.

Bot filter

For bot filtering and property-filter restrictions, go to Session Replay bot filtering.

Session Replay storage

If a user opts out tracking in your app, use the optOut configuration option to disable replay collection for that user.

Session Replay temporarily stores replay data data on the file system before it is uploaded. At every initialization, the least recent replays are trimmed to bring the total disk usage down to a maximum size.

Known limitations

Keep the following limitations in mind as you implement Session Replay:

  • Session Replay doesn't stitch together replays from a single user across multiple projects. For example:

    • You instrument multiple apps as separate Amplitude projects with Session Replay enabled in each.
    • A known user begins on one app, and then switch to another.
    • Amplitude captures both sessions.
    • The replay for each session is available for view in the corresponding host project.
  • The User Sessions chart doesn't show session replays if your organization uses a custom session definition.

  • Session Replay cannot capture the following iOS views:

    • Out-of-process iOS views, such as SFSafariViewController
    • AVPlayerLayer backed views

これは役に立ちましたか?