Dynatrace supports custom events to track user interactions, business metrics, and application-specific signals. Events can also be enriched before they are sent by applying event modifiers.
Before you can send event or session properties from your application, you must first define them in the Dynatrace web UI. Any properties that are not pre-configured will be discarded.
To define a new property
Experience Vitals, select the frontend for which you want to add the property.cart.total_value).string, boolean, or number.Dynatrace automatically prefixes your field name with event_properties. or session_properties. based on the property type you selected. For example, a field name of cart.total_value will become event_properties.cart.total_value.
Custom events are reported using Dynatrace.sendEvent(...) with optional duration and event properties.
// Simple eventDynatrace.sendEvent(EventData())// Event with duration (in milliseconds)Dynatrace.sendEvent(EventData().withDuration(150))
Custom properties provide context for reported events. Property keys must be prefixed with event_properties..
Dynatrace.sendEvent(EventData().withDuration(250).addEventProperty("event_properties.checkout_step", "payment_confirmed").addEventProperty("event_properties.cart_value", 149.99).addEventProperty("event_properties.item_count", 3))
For naming rules and limits, see Event and session properties.
Event modifiers intercept events before they are sent. They can be used to add common properties, redact sensitive information, or filter events.
On Android, modifiers operate on a JSONObject representation of the event.
val modifier = EventModifier { event ->// Add build variant context to all eventsevent.put("event_properties.build_type", BuildConfig.BUILD_TYPE)event.put("event_properties.flavor", BuildConfig.FLAVOR)event}Dynatrace.addEventModifier(modifier)
Returning null discards an event.
val modifier = EventModifier { event ->// Events happening on the com.example.MainActivity activity will be dropped.if (event.optString("view.detected_name") == "com.example.MainActivity") {return@EventModifier null}event}Dynatrace.addEventModifier(modifier)
val modifier = EventModifier { event ->// Redact user IDs from URLsval url = event.optString("url.full", null)if (url != null) {val redactedUrl = url.replace(Regex("/users/\\w+/"), "/users/{id}/")event.put("url.full", redactedUrl)}event}Dynatrace.addEventModifier(modifier)
// Store the modifier when adding itval modifier = EventModifier { event ->event.put("event_properties.custom", "value")event}Dynatrace.addEventModifier(modifier)// Remove when no longer neededDynatrace.removeEventModifier(modifier)
Event modifiers have restrictions on which fields can be modified to ensure data integrity.
The following fields can be modified or added:
event_properties.*—event properties.session_properties.*—session properties (only on session property events).url.full—complete request URL.exception.stack_trace—exception stack traces.All other fields are read-only and can't be modified. The original values are preserved.
The following example shows how to enrich only specific event types. It adds context to HTTP events (for API segmentation) and to error events (for faster triage).
fun setupConditionalEnrichment(apiClientName: String) {val modifier = EventModifier { event ->// Add context only for HTTP eventsif (event.optBoolean("characteristics.has_request")) {event.put("event_properties.api_client", apiClientName)event.put("event_properties.api_kind", "backend")}// Add context only for error eventsif (event.optBoolean("characteristics.has_error")) {event.put("event_properties.triage_owner", "mobile")event.put("event_properties.triage_severity", "error")}event}Dynatrace.addEventModifier(modifier)}