---
title: Jetpack Compose Instrumentation
description: >-
  Instrument Jetpack Compose manually or automatically using the Datadog Gradle
  Plugin.
breadcrumbs: >-
  Docs > RUM & Session Replay > Application Monitoring > Android and Android TV
  Monitoring > Jetpack Compose Instrumentation
---

# Jetpack Compose Instrumentation

## Overview{% #overview %}

Jetpack Compose is a toolkit for building native UI in Android. 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.

{% alert level="info" %}
The minimum supported Kotlin version is 1.9.23.
{% /alert %}

After initial setup, you can choose between automatic and manual instrumentation.

## Setup{% #setup %}

### Step 1 - Declare "dd-sdk-android-compose" as a dependency{% #step-1---declare-dd-sdk-android-compose-as-a-dependency %}

Add `dd-sdk-android-compose` as a dependency to each module you want to instrument. This includes the application module, any Jetpack Compose UI modules, or feature modules using Jetpack Compose. The minimum version of `dd-sdk-android-compose` for Jetpack Compose instrumentation is 2.21.0.

{% tab title="Groovy" %}

```groovy
dependencies {
    implementation "com.datadoghq:dd-sdk-android-compose:x.x.x"
    //(...)
}
```

{% /tab %}

{% tab title="Kotlin" %}

```kotlin
dependencies {
    implementation("com.datadoghq:dd-sdk-android-compose:x.x.x")
    //(...)
}
```

{% /tab %}



### Step 2 - Enable actions tracking option in `RumConfiguration`{% #step-2---enable-actions-tracking-option-in-rumconfiguration %}

After adding the dependency, enable Compose action tracking in your `RumConfiguration`. This step is required regardless of the instrumentation mode.

{% tab title="Kotlin" %}

```kotlin
val rumConfig = RumConfiguration.Builder(applicationId)
      //other configurations that you have already set
      .enableComposeActionTracking()
      .build()
Rum.enable(rumConfig)
```

{% /tab %}

{% tab title="Java" %}

```java
RumConfiguration rumConfig = new RumConfiguration.Builder(applicationId)
      //other configurations that you have already set
      .enableComposeActionTracking()
      .build();
Rum.enable(rumConfig);
```

{% /tab %}



## Automatic Instrumentation{% #automatic-instrumentation %}

For full RUM coverage with minimal setup, you can automatically instrument your Jetpack Compose application.

As described in Step 1 of the [Android setup section](https://docs.datadoghq.com/real_user_monitoring/application_monitoring/android/setup.md?tab=rum#step-1---declare-the-android-sdk-as-a-dependency), declare the [Datadog Gradle Plugin](https://github.com/DataDog/dd-sdk-android-gradle-plugin) in your build script and apply it to each module you want to instrument.

{% alert level="info" %}
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.
{% /alert %}

### Step 1 - Declare Datadog Gradle Plugin in your buildscript{% #step-1---declare-datadog-gradle-plugin-in-your-buildscript %}

The minimum version of Datadog Gradle Plugin for Jetpack Compose instrumentation is 1.17.0.

{% tab title="Groovy" %}

```groovy
buildscript {
    dependencies {
        classpath "com.datadoghq:dd-sdk-android-gradle-plugin:x.x.x"
    }
}

plugins {
    id 'com.datadoghq.dd-sdk-android-gradle-plugin'
    //(...)
}
```

{% /tab %}

{% tab title="Kotlin" %}

```kotlin
buildscript {
    dependencies {
        classpath("com.datadoghq:dd-sdk-android-gradle-plugin:x.x.x")
    }
}

plugins {
    id("com.datadoghq.dd-sdk-android-gradle-plugin")
    //(...)
}
```

{% /tab %}



### Setup 2 - Select the instrumentation mode{% #setup-2---select-the-instrumentation-mode %}

In your module's Gradle configuration, define the desired Compose instrumentation mode:

{% tab title="Groovy" %}

```groovy
datadog {
	// Other configurations that you may set before.
	//(...)

	// Jetpack Compose instrumentation mode option.
	composeInstrumentation = "AUTO"
}
```

{% /tab %}

{% tab title="Kotlin" %}

```kotlin
datadog {
  // Other configurations that you may set before.
  //(...)

  // Jetpack Compose instrumentation mode option.
  composeInstrumentation = InstrumentationMode.AUTO
}
```

{% /tab %}

Available instrumentation modes:

{% tab title="Groovy" %}

- `"AUTO"`: Instruments all `@Composable` functions.
- `"ANNOTATION"`: Only instruments `@Composable` functions annotated with `@ComposeInstrumentation`. You can define the scope of auto-instrumentation by using this annotation.
- `"DISABLE"`: Disables instrumentation completely.

{% /tab %}

{% tab title="Kotlin" %}

- `InstrumentationMode.AUTO`: Instruments all `@Composable` functions.
- `InstrumentationMode.ANNOTATION`: Only instruments `@Composable` functions annotated with `@ComposeInstrumentation`. You can define the scope of auto-instrumentation by using this annotation.
- `InstrumentationMode.DISABLE`: Disables instrumentation completely.

{% /tab %}

**Note**: If you don't declare `composeInstrumentation` in `datadog` block, the auto-instrumentation is disabled by default.

### How names are assigned with auto-instrumentation{% #how-names-are-assigned-with-auto-instrumentation %}

When auto-instrumentation is enabled:

- The **Compose navigation route** is used as the **view name**.
- The **name of the direct composable function** that wraps an interactive element is used as the **action target**.

```kotlin
@Composable
fun AppScaffold(){
    NavHost(navController = rememberNavController(), startDestination = "Home Screen"){
      composable("Home Screen"){
        HomeScreen()
      }
    }
}

@Composable
fun CustomButton(onClick: () -> Unit) {
    Button(onClick = onClick){
       Text("Welcome Button")
    }
}
```

In the example above:

- "Home Screen" is used as the **view name** when `HomeScreen()` is loaded.
- "CustomButton" is used as the **action target** when the button is clicked.

{% image
   source="https://docs.dd-static.net/images/real_user_monitoring/android/android-auto-instrumentation-naming.63631d3d0e1cc07331c435249f8ac187.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/real_user_monitoring/android/android-auto-instrumentation-naming.63631d3d0e1cc07331c435249f8ac187.png?auto=format&fit=max&w=850&dpr=2 2x"
   alt="Default naming of auto-instrumentation" /%}

## Manual Instrumentation{% #manual-instrumentation %}

If you need more customization or control over actions and views tracking, you can manually instrument your application(s).

### Actions tracking{% #actions-tracking %}

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.

```kotlin
@Composable
fun HomeScreen(){
 Column{
     Image(modifier = Modifier.datadog(name = "Welcome Image").clickable{
       // Action can be tracked if this image is clickable
     },
      // Other arguments
     )

     Text(modifier = Modifier.datadog(name = "Welcome Text").clickable{
       // Action can be tracked if this text is clickable
     },
      // Other arguments
     )
 }
}
```

In the example above, the custom names are used for the interactive elements in Rum actions tracking.

{% image
   source="https://docs.dd-static.net/images/real_user_monitoring/android/android-actions-tracking-1.668724159b35114b93e85bcaec90e8e6.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/real_user_monitoring/android/android-actions-tracking-1.668724159b35114b93e85bcaec90e8e6.png?auto=format&fit=max&w=850&dpr=2 2x"
   alt="Component name in actions tracking" /%}

### Views tracking{% #views-tracking %}

To enable RUM view tracking based on Jetpack Compose navigation, call the `NavigationViewTrackingEffect` API and pass your app's `NavHostController`.

```kotlin
@Composable
fun AppScaffold(){
	val navController = rememberNavController()
	NavigationViewTrackingEffect(
	    navController = navController,
	    trackArguments = true,
	    destinationPredicate = AcceptAllNavDestinations()
	)
	NavHost(navController = navController,
	    // other arguments
	) {
	   // (...)
	}
}
```

## Further Reading{% #further-reading %}

- [Source code for dd-sdk-android-compose](https://github.com/DataDog/dd-sdk-android/tree/develop/integrations/dd-sdk-android-compose)
- [Source code for Datadog Gradle Plugin](https://github.com/DataDog/dd-sdk-android-gradle-plugin)
- [Explore Datadog RUM](https://docs.datadoghq.com/real_user_monitoring.md)
