Skip to content

Commit 319bef2

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

File tree

5 files changed

+230
-1
lines changed

5 files changed

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

0 commit comments

Comments
 (0)