Skip to content

Commit 8d09a89

Browse files
committed
RUM-9755: Add Jetpack Compose Instrumentation documentation
1 parent aae22c7 commit 8d09a89

File tree

5 files changed

+233
-1
lines changed

5 files changed

+233
-1
lines changed

config/_default/menus/main.en.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -7168,11 +7168,16 @@ menu:
71687168
parent: rum_mobile_android
71697169
identifier: rum_mobile_android_integrated_libraries
71707170
weight: 108
7171+
- name: Jetpack Compose Instrumentation
7172+
url: real_user_monitoring/mobile_and_tv_monitoring/android/jetpack_compose_instrumentation
7173+
parent: rum_mobile_android
7174+
identifier: rum_mobile_android_jetpack_compose_instrumentation
7175+
weight: 109
71717176
- name: Troubleshooting
71727177
url: real_user_monitoring/mobile_and_tv_monitoring/android/troubleshooting
71737178
parent: rum_mobile_android
71747179
identifier: rum_mobile_android_troubleshooting
7175-
weight: 109
7180+
weight: 110
71767181
- name: iOS and tvOS
71777182
url: real_user_monitoring/mobile_and_tv_monitoring/ios
71787183
parent: mobile_and_tv_monitoring

content/en/real_user_monitoring/mobile_and_tv_monitoring/android/_index.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ To get started with RUM for Android, create an application and configure the And
3333
<u>Integrated Libraries</u>: Import integrated libraries for your Android and Android TV applications.{{< /nextlink >}}
3434
{{< nextlink href="/real_user_monitoring/mobile_and_tv_monitoring/android/troubleshooting">}}
3535
<u>Troubleshooting</u>: Common troubleshooting Android SDK issues.{{< /nextlink >}}
36+
{{< nextlink href="/real_user_monitoring/mobile_and_tv_monitoring/android/jetpack_compose_instrumentation">}}<u>Jetpack Compose Instrumentation</u>: Instrument Jetpack Compose manually or automatically using the Datadog Gradle Plugin. {{< /nextlink >}}
3637
{{< /whatsnext >}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
---
2+
title: Jetpack Compose Instrumentation
3+
description: Instrument Jetpack Compose manually or automatically using the Datadog Gradle Plugin.
4+
aliases:
5+
- /real_user_monitoring/android/jetpack_compose_instrumentation/
6+
- /real_user_monitoring/mobile_and_tv_monitoring/jetpack_compose_instrumentation/android
7+
further_reading:
8+
- link: https://github.com/DataDog/dd-sdk-android/tree/develop/integrations/dd-sdk-android-compose
9+
tag: "Source Code"
10+
text: Source code for dd-sdk-android-compose
11+
- link: https://github.com/DataDog/dd-sdk-android-gradle-plugin
12+
tag: "Source Code"
13+
text: Source code for Datadog Gradle Plugin
14+
- link: /real_user_monitoring
15+
tag: Documentation
16+
text: Explore Datadog RUM
17+
---
18+
## Overview
19+
If your application uses Jetpack Compose, you can instrument it manually or automatically with the Datadog Gradle Plugin. This enables Real User Monitoring (RUM) similar to what is available for Android classic Views.
20+
21+
<div class="alert alert-info"><p>Note: The minimum supported Kotlin version is 1.9.23.</p></div>
22+
23+
## Setup
24+
### Step 1 - Declare "dd-sdk-android-compose" as a dependency
25+
Add `dd-sdk-android-compose` dependency to each module you want to instrument. This includes the application module, any Jetpack Compose UI modules, or feature modules using Jetpack Compose.
26+
The minimum version of `dd-sdk-android-compose` for Jetpack Compose instrumentation is 2.21.0.
27+
{{< tabs >}}
28+
{{% tab "Groovy" %}}
29+
```groovy
30+
dependencies {
31+
implementation "com.datadoghq:dd-sdk-android-compose:x.x.x"
32+
//(...)
33+
}
34+
```
35+
{{% /tab %}}
36+
{{% tab "Kotlin" %}}
37+
```kotlin
38+
dependencies {
39+
implementation("com.datadoghq:dd-sdk-android-compose:x.x.x")
40+
//(...)
41+
}
42+
```
43+
{{% /tab %}}
44+
{{< /tabs >}}
45+
46+
### Step 2 - Enable actions tracking option in `RumConfiguration`
47+
After adding the dependency, enable Compose action tracking in your `RumConfiguration`. This step is required regardless of the instrumentation mode.
48+
{{< tabs >}}
49+
{{% tab "Kotlin" %}}
50+
```kotlin
51+
val rumConfig = RumConfiguration.Builder(applicationId)
52+
//other configurations that you have already set
53+
.enableComposeActionTracking()
54+
.build()
55+
Rum.enable(rumConfig)
56+
```
57+
{{% /tab %}}
58+
{{% tab "Java" %}}
59+
```java
60+
RumConfiguration rumConfig = new RumConfiguration.Builder(applicationId)
61+
//other configurations that you have already set
62+
.enableComposeActionTracking()
63+
.build();
64+
Rum.enable(rumConfig);
65+
```
66+
{{% /tab %}}
67+
{{< /tabs >}}
68+
69+
## Automatic Instrumentation
70+
As described in the [Setup section][2], declare the [Datadog Gradle Plugin][3] in your build script and apply it to each module you want to instrument.
71+
72+
<div class="alert alert-info"><p>
73+
The Datadog Gradle Plugin scans @Composable functions and adds Semantics tags to their modifiers. These tags allow Datadog RUM to track user interactions on Compose components with the correct target information. The plugin also detects NavHost usage and listens to Jetpack Compose navigation events.
74+
</p></div>
75+
76+
### Step 1 - Declare Datadog Gradle Plugin in your buildscript
77+
The minimum version of Datadog Gradle Plugin for Jetpack Compose instrumentation is 1.17.0.
78+
{{< tabs >}}
79+
{{% tab "Groovy" %}}
80+
```groovy
81+
buildscript {
82+
dependencies {
83+
classpath "com.datadoghq:dd-sdk-android-gradle-plugin:x.x.x"
84+
}
85+
}
86+
87+
plugins {
88+
id 'com.datadoghq.dd-sdk-android-gradle-plugin'
89+
//(...)
90+
}
91+
```
92+
{{% /tab %}}
93+
{{% tab "Kotlin" %}}
94+
```kotlin
95+
buildscript {
96+
dependencies {
97+
classpath("com.datadoghq:dd-sdk-android-gradle-plugin:x.x.x")
98+
}
99+
}
100+
101+
plugins {
102+
id("com.datadoghq.dd-sdk-android-gradle-plugin")
103+
//(...)
104+
}
105+
```
106+
{{% /tab %}}
107+
{{< /tabs >}}
108+
109+
### Setup 2 - Select the instrumentation mode
110+
In your module’s Gradle configuration, define the desired Compose instrumentation mode:
111+
112+
{{< tabs >}}
113+
{{% tab "Groovy" %}}
114+
```groovy
115+
datadog {
116+
// Other configurations that you may set before.
117+
//(...)
118+
119+
// Jetpack Compose instrumentation mode option.
120+
composeInstrumentation = InstrumentationMode.AUTO
121+
}
122+
```
123+
{{% /tab %}}
124+
{{% tab "Kotlin" %}}
125+
```kotlin
126+
datadog {
127+
// Other configurations that you may set before.
128+
//(...)
129+
130+
// Jetpack Compose instrumentation mode option.
131+
composeInstrumentation = InstrumentationMode.AUTO
132+
}
133+
```
134+
{{% /tab %}}
135+
{{< /tabs >}}
136+
137+
Available instrumentation modes:
138+
- `InstrumentationMode.AUTO`: Instruments all `@Composable` functions.
139+
- `InstrumentationMode.ANNOTATION`: Only instruments `@Composable` functions annotated with `@ComposeInstrumentation`. You can define the scope of auto-instrumentation by using this annotation.
140+
- `InstrumentationMode.DISABLE`: Disables instrumentation completely.
141+
142+
Note: if you don't declare `composeInstrumentation` in `datadog` block, the auto-instrumentation is disabled by default.
143+
144+
### How names are assigned with auto-instrumentation
145+
When auto-instrumentation is enabled:
146+
- The **Compose navigation route** is used as the **view name**.
147+
- The **name of the direct composable function** that wraps an interactive element is used as the **action target**.
148+
149+
```kotlin
150+
@Composable
151+
fun AppScaffold(){
152+
NavHost(navController = rememberNavController(), startDestination = "Home Screen"){
153+
composable("Home Screen"){
154+
HomeScreen()
155+
}
156+
}
157+
}
158+
159+
@Composable
160+
fun CustomButton(onClick: () -> Unit) {
161+
Button(onClick = onClick){
162+
Text("Welcome Button")
163+
}
164+
}
165+
```
166+
In the example above:
167+
- "Home Screen" is used as the **view name** when `HomeScreen()` is loaded.
168+
- "CustomButton" is used as the **action target** when the button is clicked.
169+
170+
{{< img src="real_user_monitoring/android/android-auto-instrumentation-naming.png" alt="Default naming of auto-instrumentation" style="width:90%;">}}
171+
172+
173+
## Manual Instrumentation
174+
175+
### Actions tracking
176+
To track user interactions with specific Jetpack Compose components, apply the `datadog` modifier. The `name` argument defines the view name displayed in the RUM event list.
177+
```kotlin
178+
@Composable
179+
fun HomeScreen(){
180+
Column{
181+
Image(modifier = Modifier.datadog(name = "Welcome Image").clickable{
182+
// Action can be tracked if this image is clickable
183+
},
184+
// Other arguments
185+
)
186+
187+
Text(modifier = Modifier.datadog(name = "Welcome Text").clickable{
188+
// Action can be tracked if this text is clickable
189+
},
190+
// Other arguments
191+
)
192+
}
193+
}
194+
```
195+
In the example above, the custom names are used for the interactive elements in Rum actions tracking.
196+
197+
{{< img src="real_user_monitoring/android/android-actions-tracking-1.png" alt="Component name in actions tracking" style="width:90%;">}}
198+
199+
200+
### Views tracking
201+
To enable RUM view tracking based on Jetpack Compose navigation, call the `NavigationViewTrackingEffect` API and pass your app's `NavHostController`.
202+
```kotlin
203+
@Composable
204+
fun AppScaffold(){
205+
val navController = rememberNavController()
206+
NavigationViewTrackingEffect(
207+
navController = navController,
208+
trackArguments = true,
209+
destinationPredicate = AcceptAllNavDestinations()
210+
)
211+
NavHost(navController = navController,
212+
// other arguments
213+
) {
214+
// (...)
215+
}
216+
}
217+
```
218+
219+
## Further Reading
220+
221+
{{< partial name="whats-next/whats-next.html" >}}
222+
223+
[1]: https://github.com/DataDog/dd-sdk-android/tree/develop/integrations/dd-sdk-android-compose
224+
[2]: https://docs.datadoghq.com/real_user_monitoring/mobile_and_tv_monitoring/android/setup?tab=rum#step-1---declare-the-android-sdk-as-a-dependency
225+
[3]: https://github.com/DataDog/dd-sdk-android-gradle-plugin
226+
[4]: https://developer.android.com/develop/ui/compose/accessibility/semantics
Loading
Loading

0 commit comments

Comments
 (0)