Skip to content

Commit 6fe16fd

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

File tree

5 files changed

+231
-1
lines changed

5 files changed

+231
-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,224 @@
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 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+
{{< tabs >}}
27+
{{% tab "Groovy" %}}
28+
```groovy
29+
dependencies {
30+
implementation "com.datadoghq:dd-sdk-android-compose:2.21.0+"
31+
//(...)
32+
}
33+
```
34+
{{% /tab %}}
35+
{{% tab "Kotlin" %}}
36+
```kotlin
37+
dependencies {
38+
implementation("com.datadoghq:dd-sdk-android-compose:2.21.0+")
39+
//(...)
40+
}
41+
```
42+
{{% /tab %}}
43+
{{< /tabs >}}
44+
45+
### Step 2 - Enable actions tracking option in `RumConfiguration`
46+
After adding the dependency, enable Compose action tracking in your `RumConfiguration`. This step is required regardless of the instrumentation mode.
47+
{{< tabs >}}
48+
{{% tab "Kotlin" %}}
49+
```kotlin
50+
val rumConfig = RumConfiguration.Builder(applicationId)
51+
//other configurations that you have already set
52+
.enableComposeActionTracking()
53+
.build()
54+
Rum.enable(rumConfig)
55+
```
56+
{{% /tab %}}
57+
{{% tab "Java" %}}
58+
```java
59+
RumConfiguration rumConfig = new RumConfiguration.Builder(applicationId)
60+
//other configurations that you have already set
61+
.enableComposeActionTracking()
62+
.build();
63+
Rum.enable(rumConfig);
64+
```
65+
{{% /tab %}}
66+
{{< /tabs >}}
67+
68+
## Automatic Instrumentation
69+
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.
70+
71+
<div class="alert alert-info"><p>
72+
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.
73+
</p></div>
74+
75+
### Step 1 - Declare Datadog Gradle Plugin in your buildscript
76+
{{< tabs >}}
77+
{{% tab "Groovy" %}}
78+
```groovy
79+
buildscript {
80+
dependencies {
81+
classpath "com.datadoghq:dd-sdk-android-gradle-plugin:1.17.0+"
82+
}
83+
}
84+
85+
plugins {
86+
id 'com.datadoghq.dd-sdk-android-gradle-plugin'
87+
//(...)
88+
}
89+
```
90+
{{% /tab %}}
91+
{{% tab "Kotlin" %}}
92+
```kotlin
93+
buildscript {
94+
dependencies {
95+
classpath("com.datadoghq:dd-sdk-android-gradle-plugin:1.17.0+")
96+
}
97+
}
98+
99+
plugins {
100+
id("com.datadoghq.dd-sdk-android-gradle-plugin")
101+
//(...)
102+
}
103+
```
104+
{{% /tab %}}
105+
{{< /tabs >}}
106+
107+
### Setup 2 - Select the instrumentation mode
108+
In your module’s Gradle configuration, define the desired Compose instrumentation mode:
109+
110+
{{< tabs >}}
111+
{{% tab "Groovy" %}}
112+
```groovy
113+
datadog{
114+
// Other configurations that you may set before.
115+
//(...)
116+
117+
// Jetpack Compose instrumentation mode option.
118+
composeInstrumentation = InstrumentationMode.AUTO
119+
}
120+
```
121+
{{% /tab %}}
122+
{{% tab "Kotlin" %}}
123+
```kotlin
124+
datadog{
125+
// Other configurations that you may set before.
126+
//(...)
127+
128+
// Jetpack Compose instrumentation mode option.
129+
composeInstrumentation = InstrumentationMode.AUTO
130+
}
131+
```
132+
{{% /tab %}}
133+
{{< /tabs >}}
134+
135+
Available instrumentation modes:
136+
- `InstrumentationMode.AUTO`: Instruments all `@Composable` functions.
137+
- `InstrumentationMode.ANNOTATION`: Only instruments `@Composable` functions annotated with `@ComposeInstrumentation`. You can define the scope of auto-instrumentation by using this annotation.
138+
- `InstrumentationMode.DISABLE`: Disables instrumentation completely.
139+
140+
Note: if you don't declare `composeInstrumentation` in `datadog` block, the auto-instrumentation is disabled by default.
141+
142+
### How names are assigned with auto-instrumentation
143+
When auto-instrumentation is enabled:
144+
- The **Compose navigation route** is used as the **view name**.
145+
- The **name of the direct composable function** that wraps an interactive element is used as the **action target**.
146+
147+
```kotlin
148+
@Composable
149+
fun AppScaffold(){
150+
NavHost(navController = rememberNavController(), startDestination = "Home Screen"){
151+
composable("Home Screen"){
152+
HomeScreen()
153+
}
154+
}
155+
}
156+
157+
@Composable
158+
fun CustomButton(onClick: () -> Unit) {
159+
Button(onClick = onClick){
160+
Text("Welcome Button")
161+
}
162+
}
163+
```
164+
In the example above:
165+
- "Home Screen" is used as the **view name** when `HomeScreen()` is loaded.
166+
- "CustomButton" is used as the **action target** when the button is clicked.
167+
168+
{{< img src="real_user_monitoring/android/android-auto-instrumentation-naming.png" alt="Default naming of auto-instrumentation" style="width:90%;">}}
169+
170+
171+
## Manual Instrumentation
172+
173+
### Actions tracking
174+
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.
175+
```kotlin
176+
@Composable
177+
fun HomeScreen(){
178+
Column{
179+
Image(modifier = Modifier.datadog(name = "Welcome Image").clickable{
180+
// Action can be tracked if this image is clickable
181+
},
182+
// Other arguments
183+
)
184+
185+
Text(modifier = Modifier.datadog(name = "Welcome Text").clickable{
186+
// Action can be tracked if this text is clickable
187+
},
188+
// Other arguments
189+
)
190+
}
191+
}
192+
```
193+
In the example above, the custom names are used for the interactive elements in Rum actions tracking.
194+
195+
{{< img src="real_user_monitoring/android/android-actions-tracking-1.png" alt="Component name in actions tracking" style="width:90%;">}}
196+
197+
198+
### Views tracking
199+
To enable RUM view tracking based on Jetpack Compose navigation, call the `NavigationViewTrackingEffect` API and pass your app's `NavHostController`.
200+
```kotlin
201+
@Composable
202+
fun AppScaffold(){
203+
val navController = rememberNavController()
204+
NavigationViewTrackingEffect(
205+
navController = navController,
206+
trackArguments = true,
207+
destinationPredicate = AcceptAllNavDestinations()
208+
)
209+
NavHost(navController = navController,
210+
// other arguments
211+
) {
212+
// (...)
213+
}
214+
}
215+
```
216+
217+
## Further Reading
218+
219+
{{< partial name="whats-next/whats-next.html" >}}
220+
221+
[1]: https://github.com/DataDog/dd-sdk-android/tree/develop/integrations/dd-sdk-android-compose
222+
[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
223+
[3]: https://github.com/DataDog/dd-sdk-android-gradle-plugin
224+
[4]: https://developer.android.com/develop/ui/compose/accessibility/semantics
Loading
Loading

0 commit comments

Comments
 (0)