Dynatrace allows you to send custom events to track user interactions, business metrics, and application-specific data. You can also enrich all events with additional context using 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.
Use sendEvent() to report custom events with optional duration and event properties.
// Simple eventDynatrace.sendEvent(DTXEventData())// Event with duration (in milliseconds)Dynatrace.sendEvent(DTXEventData().withDuration(150))
Add custom properties to provide context for your events. Property keys must be prefixed with event_properties..
Dynatrace.sendEvent(DTXEventData().withDuration(250).addEventProperty("event_properties.checkout_step", value: "payment_confirmed").addEventProperty("event_properties.cart_value", value: 149.99).addEventProperty("event_properties.item_count", value: 3))
For naming rules and limits, see Event and session properties.
Event modifiers allow you to intercept and modify all events before they are sent to Dynatrace. Use them to add common properties, redact sensitive information, or filter events.
let subscriber = Dynatrace.addEventModifier { event in// Add experiment tracking to all eventsevent.fields["event_properties.experiment_id"] = "checkout_flow_v2"event.fields["event_properties.variant"] = "treatment_a"return event}
Return nil to discard an event:
let subscriber = Dynatrace.addEventModifier { event in// Filter out events from test usersif event.fields["event_properties.is_test_user"] as? Bool == true {return nil // Discard this event}return event}
let subscriber = Dynatrace.addEventModifier { event in// Redact user IDs from URLsif let url = event.fields["url.full"] as? String {let redactedUrl = url.replacingOccurrences(of: #"/users/\w+/"#,with: "/users/{id}/",options: .regularExpression)event.fields["url.full"] = redactedUrl}return event}
// Store the subscriber when adding the modifierlet subscriber = Dynatrace.addEventModifier { event inevent.fields["event_properties.custom"] = "value"return event}// Remove when no longer neededDynatrace.removeEventModifier(subscriber)
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.
class UserContext {static var userId: String?static var userTier: String?}func setupEventEnrichment() {_ = Dynatrace.addEventModifier { event inif let userId = UserContext.userId {event.fields["event_properties.user_id_hash"] = userId.hashValue}if let userTier = UserContext.userTier {event.fields["event_properties.user_tier"] = userTier}return event}}
func setupFeatureFlagTracking(featureFlags: [String: Bool]) {_ = Dynatrace.addEventModifier { event infor (key, value) in featureFlags {event.fields["event_properties.ff_\(key)"] = value}return event}}
func setupConditionalEnrichment() {_ = Dynatrace.addEventModifier { event in// Only enrich HTTP eventsif event.fields["http.request.method"] != nil {event.fields["event_properties.api_client"] = "ios_app"event.fields["event_properties.api_version"] = "v2"}// Only enrich error eventsif event.fields["exception.type"] != nil {event.fields["event_properties.error_context"] = "user_session"}return event}}