Track event method for the Android SDK
Describes the trackEvent
method, which tracks a conversion event in Optimizely Feature Experimentation.
Tracks a conversion event for a user. In other words, tracks an action a user takes. Logs an error message if the specified event key does not match any existing events. See How Optimizely Experimentation counts conversions.
Version
SDK 3.7 and higher. See Minimum version support.
Description
Use this method to track events across multiple flag rules for a user. You should only make one trackEvent
call per conversion (for example, user action), even if many rules are measuring the conversion.
You call the Track Event method on a user. For information about the user object, see OptimizelyUserContext for the Android SDK.
Important
The attributes passed to
trackEvent
are only used for results segmentation on the Optimizely Experiment Results page.
Parameters
Beta
Event properties and the reserved tag key,
$opt_event_properties
, are currently in beta.
eventKey
(required) – String – The key of the event to be tracked. This key must match the event key provided when the event was created in the Optimizely app or REST API.eventTags
(optional) – Map – A map of key-value pairs specifying the reserved tag keys names and their corresponding values for this particular event occurrence.eventTags
are contextual metadata about conversion events. For example, revenue, load time, or total value. The following are available tag keys:revenue
– An integer value that is used to track the revenue metric for your experiments, aggregated across all conversion events.revenue
is recorded in cents.value
– A floating point value that is used to track a custom value for your experiments. Use this to pass the value for numeric metrics.$opt_event_properties
– A map of default and custom event properties and their values.
See reserved event tags.
Returns
This method sends conversion data to Optimizely Feature Experimentation. It does not provide return values.
Example
val attributes = hashMapOf<String, Any>("logged_in" to true)
val user = optimizelyClient.createUserContext("user123", attributes)
//Map of event properties
val properties = hashMapOf<String, Any>("category" to "shoes", "color" to "red")
//Map of event eventTags
val tags = hashMapOf<String, Any>(
"revenue" to 10000,
"value" to 100.00,
"$opt_event_properties" to properties
)
//user?.trackEvent(eventKey (required), eventTags (optional)
user?.trackEvent("my_purchase_event_key", tags)
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);
//Map of event properties
Map<String, Object> properties = new HashMap<>();
properties.put("category", "shoes");
properties.put("color", "red");
//Map of eventTags
Map<String, Object> tags = new HashMap<>();
tags.put("revenue", 10000);
tags.put("value", 100.00);
tags.put("$opt_event_properties", properties);
//user.trackEvent(eventKey (required), eventTags (optional));
user.trackEvent("my_purchase_event_key", tags);
Side effects
Additional Feature Experimentation functionality may be triggered by using this method.
Conversions
Calling this method records a conversion and attributes it to the variations that the user has seen. See How Optimizely Experimentation counts conversions.
You can create metrics on this conversion event and add metrics to experiments even after the conversion is tracked. See Events: Tracking clicks, pageviews, and other visitor actions.
Warning
This method does not track events if the event key is invalid.
Changing the traffic allocation of running experiments affects how conversions are recorded and variations are attributed to users. See Why you should not change a running experiment.
Notification listeners
Accessing this method triggers a call to the TRACK
notification listener.
Warning
This method does not call the
TRACK
notification listener if the event key is invalid.
Source files
The language and platform source files containing the Android implementation are available on GitHub.
Updated 21 days ago