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.
セッションリプレイAndroidミドルウェア
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.
この記事では、Android SDK ミドルウェアを使用して セッションリプレイ をインストールする方法について説明します。アプリがすでに(メンテナンス用の)Amplitude SDKを使用している場合は、このオプションを使用してください。
アプリがすでに(最新の)Amplitude Android SDKを使用している場合は、セッションリプレイ Android SDKプラグインを使用してください。
製品内アナリティクスにAmplitude以外のプロバイダーを使用している場合は、スタンドアロンの実装を選択してください。
Session Replay and performance
Amplitude built Session Replay to minimize impact on the performance of the Android apps in which it's installed by:
- Asynchronously capturing and processing replay data, to avoid blocking the main user interface thread. The main thread must analyze the view hierarchy, but snapshot capture scheduling and extra processing offloads to the background.
- 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.
始める前に
セッションリプレイミドルウェアには以下が必要です。
- アプリケーションはAndroidをターゲットとしています。
- (メンテナンス)Amplitude Android SDKの
2.40.1以上を使用していること。 - デバイスIDをSDKに提供できること。
Supported Android versions
Session replay supports down to Android 5.0 with a minimum SDK of 21 minSdk = 21. This should support over 99.6% of global Android devices according to Google's distribution data in Android Studio.
クイックスタート
最新バージョンのセッションリプレイミドルウェアをプロジェクトの依存関係に追加してください。
// Install latest version from Maven Central
implementation("com.amplitude:middleware-session-replay-android:0.26.1")
// You will also need the (maintenance) Amplitude Analytics SDK if it's not already installed
implementation("com.amplitude:android-sdk:[2.40.1,3.0.0)")
アプリケーションコードを設定します。
import com.amplitude.api.Amplitude
import com.amplitude.api.SessionReplayMiddleware
// Initialize (maintenance) Amplitude Analytics SDK instance
val amplitude = Amplitude.getInstance()
.initialize(this, AMPLITUDE_API_KEY)
// Replay events will be flushed on close as well
// If setFlushEventsOnClose(false) you must call flush() manually
.setFlushEventsOnClose(true)
// Create Session Replay Middleware
val sessionReplayMiddleware = SessionReplayMiddleware(amplitude, sampleRate = 1.0)
// Add session replay middleware
// Recording will be handled automatically
amplitude.addEventMiddleware(sessionReplayMiddleware)
// Track events
amplitude.logEvent("Setup (maintenance) Amplitude Android SDK with session replay!")
// Send replay events to the server
amplitude.uploadEvents()
// You can also call flush() on the middleware directly to only send replay events
// sessionReplayMiddleware.flush()
// Always flush before app exit (onPause)
// override fun Activity.onPause() { sessionReplayMiddleware.flush() }
設定
セッションリプレイミドルウェアを初期化する際には、次のオプションを渡します。
| オプション | タイプ | 必須 | デフォルト | 概要 |
|---|---|---|---|---|
amplitude | AmplitudeClient | はい | 該当なし | 接続先となるメンテナンスAmplitude SDKクライアントインスタンス。 |
sampleRate | Number | いいえ | 0.0 | Amplitudeがリプレイ収集のために選択するセッション数を制御します。 0から1までの小数(例:0.4)を使用すると、多数のセッションの中から40%のセッションが選択されます。 |
enableRemoteConfig | Boolean | いいえ | true | このセッションリプレイのインスタンスに対するリモートコンフィギュレーションを有効または無効にします。 |
privacyConfig | PrivacyConfig | いいえ | PrivacyConfig() | どのビューをマスクするかを制御するプライバシー設定。 マスクレベルはPrivacyConfig(maskLevel = MaskLevel.MEDIUM)で設定します。マスクレベルを参照してください。 |
autoStart | Boolean | いいえ | true | ミドルウェアの初期化時に自動的にスクリーンキャプチャを開始します。 |
bandwidthLimitBytes | Int? | いいえ | null | 詳細設定従量制(携帯電話など)接続での毎日のアップロード制限(バイト単位)。 |
storageLimitMB | Int? | いいえ | null | 詳細設定ローカルストレージの制限値(MB単位)。 |
recordLogOptions.logCountThreshold | Int | いいえ | 1000 | セッションごとのログの最大数。 |
recordLogOptions.maxMessageLength | Int | いいえ | 2000 | ログメッセージの最大長です。 |
Mask on-screen data
The Session Replay SDK offers four ways to mask user input, text, and other view components.
Mask level
Session Replay for Android supports three levels of masking, set through the privacyConfig option with PrivacyConfig(maskLevel = MaskLevel.MEDIUM). The default level is medium.
Use this option in the Session Replay configuration.
| Mask level | Description |
|---|---|
light | Masks password, email, and phone number input fields. In SDK 0.26.2 and later, light also masks any field with sensitive autofill hints (credit card number, expiration, and security code; name; username; postal address and code); autofill-hint detection requires Android 8.0 (API 26) or higher. Earlier versions, including the middleware-session-replay-android artifact, mask only the input-field types above. |
medium (default) | Everything in light, plus all editable text fields (EditText). |
conservative | Everything in medium, plus all text views (TextView). |
Privacy tags in layout XML
Use this option in your application's layout XML.
| View | Description |
|---|---|
<EditText> | Session Replay masks all text input fields by default. When a users enters text into an input field, Session Replay captures asterisks in place of text. To unmask a text input, add the tag amp-unmask. For example: <EditText android:tag="amp-unmask" android:text="Unmask this">. |
<TextView> | To mask text within non-input elements, add the tag amp-mask. For example, <TextView android:tag="amp-mask" android:text="Mask this"/>. When masked, Session Replay captures masked text as a series of asterisks. |
| non-text elements | To block a non-text element, add the tag amp-block. For example, <ImageView android:tag="amp-block"/>. Session Replay replaces blocked elements with a placeholder of the same dimensions. |
<WebView> | To unmask a web view, add the amp-unmask tag. For example, <WebView android:tag="amp-unmask"/>. |
Privacy methods on the Session Replay SDK
Import the SessionReplay class, then call one of the methods below from your application's code.
import com.amplitude.android.sessionreplay.SessionReplay
| Method | Description |
|---|---|
unmask(view: View) | To unmask a view manually, call SessionReplay.unmask(view) where view is a reference to any view that is masked. |
mask(view: View) | To mask text within non-input views, call SessionReplay.mask(view) where view is a reference to the any view you want to mask. When masked, Session Replay captures masked text as a series of asterisks. |
block(view: View) | To block a non-text view, call SessionReplay.block(view) where view is a reference to the View you want to block. Session Replay replaces blocked views with a placeholder of the same dimensions. |
Privacy modifiers in Jetpack Compose
For composables, use the privacy modifiers instead of layout XML tags or the SessionReplay methods. These modifiers require version 0.21.0 or higher of the Session Replay Android SDK or plugin.
Import the modifier you need from com.amplitude.android.sessionreplay.compose, then add it to the composable's modifier chain.
import com.amplitude.android.sessionreplay.compose.ampMask
import com.amplitude.android.sessionreplay.compose.ampUnmask
import com.amplitude.android.sessionreplay.compose.ampBlock
| Modifier | Description |
|---|---|
Modifier.ampMask() | Masks text that Session Replay captures by default, such as the contents of a Text composable. Session Replay captures masked text as a series of asterisks. |
Modifier.ampUnmask() | Unmasks a composable that Session Replay masks by default, such as a TextField. |
Modifier.ampBlock() | Blocks a composable. Session Replay replaces blocked composables with a placeholder of the same dimensions and doesn't capture their contents. |
// Mask text that Session Replay captures by default
Text(
text = "Account number: 123456789",
modifier = Modifier.ampMask(),
)
// Unmask an input field that Session Replay masks by default
TextField(
value = username,
onValueChange = { username = it },
modifier = Modifier.ampUnmask(),
)
// Block a composable and everything it contains
Box(modifier = Modifier.ampBlock()) {
Text("Session Replay doesn't capture this content")
}
A privacy modifier applies to the composable you add it to and to everything nested inside it. When a nested composable has its own privacy modifier, that modifier takes precedence for that composable and its descendants.
ユーザーのオプトアウト
セッションリプレイ ミドルウェアは Android SDK optOutの設定に従っており、ユーザーによるオプトアウト自体をサポートしていません。
import com.amplitude.api.Amplitude
import com.amplitude.api.SessionReplayMiddleware
// Set optOut on the Amplitude SDK
val amplitude = Amplitude.getInstance()
.initialize(this, AMPLITUDE_API_KEY)
.setOptOut(true)
amplitude.addEventMiddleware(SessionReplayMiddleware(amplitude, /* 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:
import com.amplitude.api.Amplitude
import com.amplitude.api.SessionReplayMiddleware
// Set serverZone on the Amplitude SDK
val amplitude = Amplitude.getInstance()
.initialize(this, AMPLITUDE_API_KEY)
.setServerZone(AmplitudeServerZone.EU)
amplitude.addEventMiddleware(SessionReplayMiddleware(amplitude, /* 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.
// This configuration samples 1% of all sessions
amplitude.addEventMiddleware(SessionReplayMiddleware(amplitude, sampleRate = 0.01))
リプレイ収集を無効にする
開始後、セッションリプレイは次のいずれかが発生するまでアプリ内で実行されます。
- ユーザーがアプリを離れる。
sessionReplayMiddleware.stop()呼び出します。
ユーザーがアプリの制限付きエリアに移動する前にsessionReplayMiddleware.stop()を呼び出して、ユーザーがそのエリアにいる間のリプレイ収集を無効にします。
参照の保持
SessionReplayMiddlewareインスタンスへの参照を保持します:val sessionReplayMiddleware = SessionReplayMiddleware(/* session replay options */)。
ユーザーがアプリの制限のないエリアに戻ったときに、sessionReplayMiddleware.start()を呼び出してリプレイ収集を再度有効にします。
特定の画面のみをキャプチャするには、起動時にミドルウェアが起動しないようにautoStart = falseでミドルウェアを初期化し、ユーザーがこれらの画面に出入りする際にstart()とstop()を呼び出します。
// Add the middleware without starting capture
val sessionReplayMiddleware = SessionReplayMiddleware(amplitude, 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 などのフィーチャーフラグプロダクトを使用して、場所などの基準に基づいてリプレイ収集を有効または無効にすることもできます。たとえば、特定のユーザーグループを対象とするフィーチャーフラグを作成し、それを初期化ロジックに追加します。
import com.amplitude.api.Amplitude
import com.amplitude.api.SessionReplayMiddleware
// Your existing initialization logic with Android SDK
val amplitude = Amplitude.getInstance().initialize(this, AMPLITUDE_API_KEY)
if (nonEUCountryFlagEnabled) {
// Create and Install Session Replay Middleware
val sessionReplayMiddleware = SessionReplayMiddleware(amplitude, sampleRate = 0.5)
amplitude.addEventMiddleware(sessionReplayMiddleware)
}
WebView and MapView support (Beta)
By default, Session Replay blocks web views and map views in a capture. To enable capture of these components, use the settings described in Mask on-screen data.
Web view session replay injects JavaScript into the rendered page. To enable this, the SDK sets javaScriptEnabled to true on the unmasked web view.
Log Recording
Availability
Log recording is available in Session Replay Android 0.22.0 and later.
Session Replay supports recording logs in Android applications. To enable log recording, send log messages through the recordLog(level, message, timestamp) API and configure the limits through recordLogOptions parameter while initializing Session Replay.
import com.amplitude.android.plugins.SessionReplayPlugin
import com.amplitude.android.sessionreplay.config.RecordLogLevel
import com.amplitude.android.sessionreplay.config.RecordLogOptions
val sessionReplayPlugin = SessionReplayPlugin(
recordLogOptions = RecordLogOptions(
logCountThreshold = 2000,
maxMessageLength = 4000
)
)
amplitude.add(sessionReplayPlugin)
sessionReplayPlugin.recordLog(RecordLogLevel.Error, "This is an error log")
// You can also create custom log levels
sessionReplayPlugin.recordLog(RecordLogLevel("debug"), "This is a debug log")
Session Replay supports predefined log levels (Error, Warn, Log) and custom log levels. Create custom log levels by passing any string value to RecordLogLevel(...).
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.
Timber
If you use Timber, you can integrate it with a custom tree.
import com.amplitude.android.plugins.SessionReplayPlugin
import com.amplitude.android.sessionreplay.config.RecordLogLevel
import timber.log.Timber
class AmplitudeLogRecordTree(private val plugin: SessionReplayPlugin) : Timber.Tree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
val recordLevel = when (priority) {
android.util.Log.ERROR -> RecordLogLevel.Error
android.util.Log.WARN -> RecordLogLevel.Warn
android.util.Log.INFO -> RecordLogLevel.Log
android.util.Log.DEBUG -> RecordLogLevel("debug")
android.util.Log.VERBOSE -> RecordLogLevel("verbose")
else -> return
}
plugin.recordLog(recordLevel, message)
}
}
val sessionReplayPlugin = SessionReplayPlugin()
amplitude.add(sessionReplayPlugin)
Timber.plant(AmplitudeLogRecordTree(sessionReplayPlugin))
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 of tracking in your app, use the optOut configuration option to disable replay collection for that user.
The SDK stores replay data locally. It retries transient upload failures, subject to retry and storage limits.
Amplitudeでは、アプリ終了ごとにセッションデータがサーバーに送信されるように、Amplitude SDK設定でamplitude.setFlushEventsOnClose(true)(デフォルト)を設定することを推奨しています。
Jetpack Compose Support (Alpha)
Session Replay supports Jetpack Compose for Android applications. This feature is currently in Alpha. To use Jetpack Compose with Session Replay, ensure you use version 0.20.4 or higher of the Session Replay Android SDK or plugin.
To control what Session Replay captures in a composable, use the privacy modifiers in Jetpack Compose. Masking support for Compose requires 0.21.0 or higher.
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 can't capture the following Android views:
- Canvas Views
これは役に立ちましたか?