|
| 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 | +Note: The minimum supported Kotlin version is 1.9.23. |
| 22 | + |
| 23 | +## Setup |
| 24 | +### Step 1 - Declare "dd-sdk-android-compose" as a dependency |
| 25 | +To track Jetpack Compose views with RUM, 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 | +### What Datadog Gradle Plugin does for Jetpack Compose |
| 72 | +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. |
| 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`. |
| 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( |
| 150 | + navController = rememberNavController(), |
| 151 | + startDestination = "Home Screen"){ |
| 152 | + composable("Home Screen"){ |
| 153 | + HomeScreen() |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +@Composable |
| 159 | +fun CustomButton(onClick: () -> Unit) { |
| 160 | + Button(onClick = onClick){ |
| 161 | + Text("Welcome Button") |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | +In the example above: |
| 166 | +- "Home Screen" is used as the **view name** when `HomeScreen()` is loaded. |
| 167 | +- "CustomButton" is used as the **action target** when the button is clicked. |
| 168 | + |
| 169 | +{{< img src="real_user_monitoring/android/android-auto-instrumentation-naming.png" alt="Default naming of auto-instrumentation" style="width:90%;">}} |
| 170 | + |
| 171 | + |
| 172 | +## Manual Instrumentation |
| 173 | + |
| 174 | +### Actions tracking |
| 175 | +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. |
| 176 | +```kotlin |
| 177 | +@Composable |
| 178 | +fun HomeScreen(){ |
| 179 | + Column{ |
| 180 | + Image(modifier = Modifier.datadog(name = "Welcome Image").clickable{ |
| 181 | + // Action can be tracked if this image is clickable |
| 182 | + }, |
| 183 | + // Other arguments |
| 184 | + ) |
| 185 | + |
| 186 | + Text(modifier = Modifier.datadog(name = "Welcome Text").clickable{ |
| 187 | + // Action can be tracked if this text is clickable |
| 188 | + }, |
| 189 | + // Other arguments |
| 190 | + ) |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | +In the example above, the custom names are used for the interactive elements in Rum actions tracking. |
| 195 | + |
| 196 | +{{< img src="real_user_monitoring/android/android-actions-tracking-1.png" alt="Component name in actions tracking" style="width:90%;">}} |
| 197 | + |
| 198 | + |
| 199 | +### Views tracking |
| 200 | +To enable RUM view tracking based on Jetpack Compose navigation, call the `NavigationViewTrackingEffect` API and pass your app's `NavHostController`. |
| 201 | +```kotlin |
| 202 | +@Composable |
| 203 | +fun AppScaffold(){ |
| 204 | + val navController = rememberNavController() |
| 205 | + NavigationViewTrackingEffect( |
| 206 | + navController = navController, |
| 207 | + trackArguments = true, |
| 208 | + destinationPredicate = AcceptAllNavDestinations() |
| 209 | + ) |
| 210 | + NavHost(navController = navController, |
| 211 | + // other arguments |
| 212 | + ) { |
| 213 | + // (...) |
| 214 | + } |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +## Further Reading |
| 219 | + |
| 220 | +{{< partial name="whats-next/whats-next.html" >}} |
| 221 | + |
| 222 | +[1]: https://github.com/DataDog/dd-sdk-android/tree/develop/integrations/dd-sdk-android-compose |
| 223 | +[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 |
| 224 | +[3]: https://github.com/DataDog/dd-sdk-android-gradle-plugin |
| 225 | +[4]: https://developer.android.com/develop/ui/compose/accessibility/semantics |
0 commit comments