Session Replay Android Middleware
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.
This article covers the installation of Session Replay using the Android SDK middleware. If your app is already instrumented with (maintenance) Amplitude SDK, use this option.
If your app is already instrumented with (latest) Amplitude Android SDK, use the Session Replay Android SDK Plugin.
If you use a provider other than Amplitude for in-product analytics, choose the standalone implementation.
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.
Before you begin
Use the latest version of the Session Replay Middleware above version {{sdk_versions:session_replay_android_middleware}}. For a list of available versions, see all release versions on Maven Central.
The Session Replay Middleware requires that:
- Your application is Android-based.
- You are using
2.40.1or higher of the (maintenance) Amplitude Android SDK. - You can provide a device ID to the 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.
Quickstart
Add the latest version of the session replay middleware to your project dependencies
// Install latest version from Maven Central
implementation("com.amplitude:middleware-session-replay-android:{{sdk_versions:session_replay_android_standalone}}")
// 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]")
Configure your application code.
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() }
Configuration
Pass the following option when you initialize the Session Replay middleware:
sampleRate- Type:
number - Required: No
- Default:
0 - Description:
- Use this option to control how many sessions to select for replay collection.
- The number should be a decimal between 0 and 1, for example
0.4, representing the fraction of sessions to have randomly selected for replay collection. Over a large number of sessions,0.4would select40%of those sessions.
- Type:
recordLogOptions.logCountThreshold- Type:
Int - Required: No
- Default:
1000 - Description: Use this option to configure the maximum number of logs per session.
- Type:
recordLogOptions.maxMessageLength- Type:
Int - Required: No
- Default:
2000 - Description: Use this option to configure the maximum length of a log message.
- Type:
Mask on-screen data
The Session Replay SDK offers three ways to mask user input, text, and other view components.
Mask level
Session Replay for Android supports three levels of masking, configurable with the the maskLevel config option.
Use this option in the Session Replay configuration.
| Mask level | Description |
|---|---|
light | Masks all password, email address, phone numbers, web views and map views. Doesn't block credit card numbers. |
medium | Masks all editable text views, web views and map views. |
conservative | Masks all text views, web views and map views |
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-block"/>. |
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
| 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 text input 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.unmask(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. |
User opt-out
The Session Replay middleware follows the Android SDK's optOut setting, and doesn't support user opt-outs on its own.
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.add(SessionReplayMiddleware(amplitude, /* session replay options */))
Sampling rate
By default, Session Replay captures 0% of sessions for replay. Use the sampleRate configuration option to set the percentage of total sessions that Session Replay captures. For example:
To set the sampleRate consider the monthly quota on your Session Replay plan. For example, if your monthly quota is 2,500,000 sessions, and you average 3,000,000 monthly sessions, your quota is 83% of your average sessions. In this case, to ensure sampling lasts through the month, set sampleRate to .83 or lower.
Keep the following in mind as you consider your sample rate:
- When you reach your monthly session quota, Amplitude stops capturing sessions for replay.
- Session quotas reset on the first of every month.
- Use sample rate to distribute your session quota over the course of a month, rather than using your full quota at the beginning of the month.
- To find the best sample rate, Amplitude recommends that you start low, for example
.01. If this value doesn't capture enough replays, raise the rate over the course of a few days. For ways to monitor the number of session replays captured, see View the number of captured sessions. - Replays with processing errors don't count toward your monthly quota. Replays with a retention error message have already been counted against the quota, when the session was still in the retention period.
// This configuration samples 1% of all sessions
amplitude.addEventMiddleware(SessionReplayMiddleware(amplitude, sampleRate = 0.01))
Disable replay collection
Once enabled, Session Replay runs on your app until either:
- The user leaves your app
- You call
sessionReplayMiddleware.stopRecording()
Call sessionReplayMiddleware.stopRecording() before a user navigates to a restricted area of your app to disable replay collection while the user is in that area.
Keep a reference
This requires keeping a reference to the SessionReplayMiddleware instance val sessionReplayMiddleware = SessionReplayMiddleware(/* session replay options */).
Call sessionReplayMiddleware.startRecording() to re-enable replay collection when the return to an unrestricted area of your app.
You can also use a feature flag product like Amplitude Experiment to create logic that enables or disables replay collection based on criteria like location. For example, you can create a feature flag that targets a specific user group, and add that to your initialization logic:
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 Block 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 1.13.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
Session replay uses existing Amplitude tools and APIs to handle privacy and deletion requests. <!--vale off-->
Consent management and Session Replay
While privacy laws and regulations vary across states and countries, certain constants exist, including the requirements to disclose in a privacy notice the categories of personal information you are collecting, the purposes for its use, and the categories of third parties with which personal information is shared. When implementing a session replay tool, you should review your privacy notice to make sure your disclosures remain accurate and complete. And as a best practice, review your notice with legal counsel to make sure it complies with the constantly evolving privacy laws and requirements applicable to your business and personal information data practices.
Retention period
If your Amplitude plan includes Session Replay, Amplitude retains raw replay data for 30 days from the date of ingestion.
If you purchase extra session volume, Amplitude retains raw replay data for 90 days from the date of ingestion. If you need a more strict policy, contact Amplitude support to set the value to 30 days.
Changes to the retention period impact replays ingested after the change. Sessions captured and ingested before a retention period change retain the previous retention period.
Retention periods are set at the organization level. Replays that are outside of the retention period aren't viewable in Amplitude.
DSAR API
The Amplitude DSAR API returns metadata about session replays, but not the raw replay data. All events that are part of a session replay include a [Amplitude] Session Replay ID event property. This event provides information about the sessions collected for replay for the user, and includes all metadata collected with each event.
{
"amplitude_id": 123456789,
"app": 12345,
"event_time": "2020-02-15 01:00:00.123456",
"event_type": "first_event",
"server_upload_time": "2020-02-18 01:00:00.234567",
"device_id": "your device id",
"user_properties": { ... }
"event_properties": {
"[Amplitude] Session Replay ID": "cb6ade06-cbdf-4e0c-8156-32c2863379d6/1699922971244"
}
"session_id": 1699922971244,
}
Data deletion
Session Replay uses Amplitude's User Privacy API to handle deletion requests. Successful deletion requests remove all session replays for the specified user.
When you delete the Amplitude project on which you use Session Replay, Amplitude deletes that replay data.
Bot filter
Session Replay uses the same block filter available in the Amplitude app. Session Replay doesn't block traffic based on event or user properties.
Session Replay storage
If a user opts out tracking in your app, use the optOut configuration option to disable replay collection for that user.
- Memory usage grows until you call
flush()to transfer the session replay data to the server. - Session data isn't retried on failure to transfer to the Amplitude servers.
Amplitude recommends setting amplitude.setFlushEventsOnClose(true) in the Amplitude SDK Configuration (the default) to send session data to the server on each app exit.
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.18.0 or higher of the Session Replay Android SDK or plugin.
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
Was this helpful?