Skip to content

Commit 3c4b9d8

Browse files
markushiimatwawanaromtsn
authored
feat(android): Add Jetpack Compose automatic UI transaction docs (#5893)
Co-authored-by: Isabel <[email protected]> Co-authored-by: Roman Zavarnitsyn <[email protected]>
1 parent 6a852ae commit 3c4b9d8

File tree

2 files changed

+118
-15
lines changed

2 files changed

+118
-15
lines changed

src/platforms/android/common/performance/instrumentation/automatic-instrumentation.mdx

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,24 +226,58 @@ For more information see our [file I/O integration](/platforms/android/configura
226226

227227
### User Interaction Instrumentation
228228

229+
The UI instrumentation, once enabled, captures transactions for a set of different user interactions, which include clicks, scrolls, and swipes. User interaction instrumentation is available for both classic Android Views as well as Jetpack Compose.
230+
231+
This feature is disabled by default, but you can enable it by setting:
232+
233+
```xml {filename:AndroidManifest.xml}
234+
<application>
235+
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
236+
</application>
237+
```
238+
239+
#### User Interaction Instrumentation for Classic Android View System
240+
229241
_(New in version 6.0.0)_
230242

231-
The UI instrumentation, once enabled, captures transactions for a set of different user interactions, which include clicks, scrolls and swipes. The SDK composes the transaction name out of the host `Activity` and the `id` of the view, which captured the user interaction; for example, `LoginActivity.login_button`. The transaction operation is set to `ui.action` plus the interaction type (one of `click`, `scroll` or `swipe`).
243+
The SDK composes the transaction name out of the host `Activity` and the `id` of the view that captured the user interactionfor example, for `LoginActivity.login_button`. The transaction operation is set to `ui.action` plus the interaction type (one of `click`, `scroll`, or `swipe`).
232244

233245
<Note>
234246

235247
If the view doesn't have the `id` assigned, the transaction won't be captured because it can't be uniquely identified otherwise.
236248

237249
</Note>
238250

239-
The UI instrumentation is disabled by default, but you can enable it by setting:
251+
#### User Interaction Instrumentation for Jetpack Compose
240252

241-
```xml {filename:AndroidManifest.xml}
242-
<application>
243-
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
244-
</application>
253+
_(New in version 6.10.0)_
254+
255+
The SDK composes the transaction name out of the host `Activity` and the `tag` set by way of the `Modifier.testTag("<tag>")` of the `Composable` (for example, `LoginActivity.login_button`). The transaction operation is set to `ui.action` plus the interaction type (one of `click`, `scroll`, or `swipe`).
256+
257+
```kotlin
258+
import androidx.compose.ui.platform.testTag
259+
260+
@Composable
261+
fun LoginScreen() {
262+
Column {
263+
// ...
264+
Button(
265+
modifier = Modifier.testTag("button_login"),
266+
onClick = { TODO() }) {
267+
Text(text = "Login")
268+
}
269+
}
270+
}
245271
```
246272

273+
<Note>
274+
275+
If the `@Composable` doesn't have a `testTag` modifier applied, the transaction won't be captured because it can't be uniquely identified otherwise.
276+
277+
</Note>
278+
279+
#### Transaction Lifetime
280+
247281
The transaction finishes automatically after it reaches the specified [idleTimeout](/platforms/android/configuration/options/#idle-timeout) and all of its child spans are finished. The `idleTimeoout` defaults to `3000` milliseconds (three seconds). You can also disable the idle timeout by setting it to `null`, but the transaction must be finished manually in this case.
248282

249283
<Note>

src/platforms/android/configuration/integrations/jetpack-compose.mdx

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,82 @@ categories:
88
- mobile
99
---
1010

11-
The `sentry-compose` library provides [Jetpack Compose](https://developer.android.com/jetpack/androidx/releases/compose) Navigation support for Sentry.
11+
The `sentry-compose-android` library provides [Jetpack Compose](https://developer.android.com/jetpack/androidx/releases/compose) support for the following interactions:
12+
* User interactions like clicks, swipes and scroll gestures
13+
* Jetpack Compose navigation
1214

13-
On this page, we get you up and running with Sentry's Compose Navigation Integration, so that it will automatically add a breadcrumb and start a transaction for each navigation event.
15+
## User Interactions
16+
_(New in version 6.10.0)_
1417

15-
## Auto-Installation With the Sentry Android Gradle Plugin
18+
### Installation
1619

17-
### Install
20+
To add the Jetpack Compose integration, install the [Android SDK](/platforms/android/), then add the `sentry-compose-android` dependency using Gradle:
21+
22+
```groovy
23+
implementation 'io.sentry:sentry-android:{{ packages.version('sentry.java.android', '6.2.0') }}'
24+
implementation 'io.sentry:sentry-compose-android:{{ packages.version('sentry.java.compose', '6.2.0') }}'
25+
```
26+
27+
### Configuration
28+
29+
This feature is disabled by default, but you can enable it the following ways.
30+
31+
#### Using `AndroidManifest.xml`
32+
```xml {filename:AndroidManifest.xml}
33+
<application>
34+
<meta-data android:name="io.sentry.traces.user-interaction.enable" android:value="true" />
35+
<meta-data android:name="io.sentry.breadcrumbs.user-interaction" android:value="true" />
36+
</application>
37+
```
38+
39+
#### Using `SentryOptions`
40+
41+
If you initialize the SDK [manually as mentioned here](/platforms/android/configuration/manual-init/), you can enable user interactions like this:
42+
```kotlin {filename:MyApplication.kt}
43+
SentryAndroid.init(this) { options ->
44+
// ...
45+
options.isEnableUserInteractionTracing = true
46+
options.isEnableUserInteractionBreadcrumbs = true
47+
}
48+
```
49+
50+
The Sentry SDK utilizes the built-in `Modifier.testTag("<tag>")` of a Composable to determine its identifier. Here's an example:
51+
52+
```kotlin
53+
import androidx.compose.ui.platform.testTag
54+
@Composable
55+
fun LoginScreen() {
56+
Column {
57+
// ...
58+
Button(
59+
modifier = Modifier.testTag("button_login"),
60+
onClick = { TODO() }) {
61+
Text(text = "Login")
62+
}
63+
}
64+
}
65+
```
66+
67+
<Note>
68+
69+
If a `@Composable` doesn't have a `testTag` modifier applied, both breadcrumbs and transactions won't be captured because they can't be uniquely identified.
70+
71+
</Note>
72+
73+
<Note>
74+
75+
If you want to customize the recorded breadcrumbs/transactions, you can skip to [the section below](#customize-the-recorded-breadcrumbtransaction).
76+
77+
</Note>
78+
79+
## Jetpack Compose Navigation
80+
81+
82+
In this section, we get you up and running with Sentry's Compose Navigation Integration, so that it will automatically add a breadcrumb and start a transaction for each navigation event.
83+
84+
### Auto-Installation With the Sentry Android Gradle Plugin
85+
86+
#### Install
1887

1988
If you're using the [Sentry Android Gradle Plugin](/platforms/android/gradle/), the Jetpack Compose Navigation Integration is automatically applied, and both breadcrumbs and transactions are automatically created for every navigation event.
2089

@@ -50,7 +119,7 @@ Learn more about the Sentry Android Gradle plugin in our [Gradle](/platforms/and
50119

51120
</Note>
52121

53-
### Disable
122+
#### Disable
54123

55124
If you want to disable the Jetpack Compose instrumentation feature, you can adapt your `build.gradle(.kts)` file like so:
56125

@@ -77,9 +146,9 @@ sentry {
77146
}
78147
```
79148

80-
## Manual Installation
149+
### Manual Installation
81150

82-
### Install
151+
#### Install
83152

84153
Sentry captures data by adding a `withSentryObservableEffect` extension function to `NavHostController`. To add the Navigation integration, install the [Android SDK](/platforms/android/), then add the `sentry-compose` dependency using Gradle:
85154

@@ -88,7 +157,7 @@ implementation 'io.sentry:sentry-android:{{ packages.version('sentry.java.androi
88157
implementation 'io.sentry:sentry-compose-android:{{ packages.version('sentry.java.compose.android', '6.2.0') }}'
89158
```
90159

91-
### Configure
160+
#### Configure
92161

93162
Configuration should happen in the respective `@Composable` function, once you obtain an instance of `NavHostController`:
94163

@@ -110,7 +179,7 @@ NavHost(
110179

111180
By default, the navigation transaction finishes automatically after it reaches the specified [idleTimeout](/platforms/android/configuration/options/#idle-timeout) and all of its child spans are finished. You can customize the timeout to your needs.
112181

113-
## Verify
182+
### Verify
114183

115184
This snippet includes a sample `Activity` with a couple of navigation events between composables and captures an intentional message, so you can test that everything is working as soon as you set it up:
116185

0 commit comments

Comments
 (0)