Tracking custom events - optimove-tech/Optimove-SDK-Android GitHub Wiki
Once you and the Optimove Product Integration Team have reviewed your use cases, you will define the custom events together. The Product Integration Team will configure your particular events and parameters within your Optimove site, while you will be responsible for passing the event and parameter information to Optimove.
- Custom events must be first configured by Optimove Product Integration team before implementation
- Events and parameters must be in lowercase and use snake_case naming convention only
-
- snake_case definition: Separate each word with one underscore character (_) and no spaces. (e.g., checkout_completed)
- The parameter types available for use in event reporting functions are:
-
- String: A series of alphanumeric characters of up to 255 characters in length, using any encoding
-
- Number: Any numeric value, whether an integer or a value containing a decimal point
-
- Boolean: Is either "true" or "false" values, not a string
- Optimove supports up to 50 parameters per custom event
There are two (simple & complex) ways to report a Custom Event through the Optimove SDK reportEvent() function. See them both below.
Use the following function to report a simple event:Optimove.getInstance().reportEvent('<EVENT_NAME>', Map<String, Object> parameters).
- EVENT_NAME: Required string value of the exact event name configured by the Product Integration Team
-
Map<String, Object> params: Optional value of the exact parameter name(s) configured by the Product Integration Team
Code Snippet Example
public class MainActivity extends AppCompatActivity {
public void reportSignUps() {
Optimove.getInstance().reportEvent("cart_is_empty");
}
public void reportSignUps() {
Map<String, Object> params = new HashMap<>();
params.put("first_name", "John");
params.put("last_name", "Doe");
params.put("email", "[email protected]");
params.put("age", 42);
params.put("opt_in", false);
Optimove.getInstance().reportEvent("signup", params);
}
}Use this interface to conform your complex event objects to the Optimove format and pass it to the SDK via the Optimove.getInstance().reportEvent(OptimoveEvent optimoveEvent) method.
The Optimove SDK defines an interface called OptimoveEvent in two methods.
-
String getName()– Declares the custom event's name -
Map<String, Object> getParameters()– Specifies the custom event's parameters.
Code Snippet Example
public class MainActivity extends AppCompatActivity {
public void reportPlacedOrderEvent(List<CartItem> cartItems) {
Optimove.getInstance().reportEvent(new PlacedOrderEvent(cartItems));
}
}
class PlacedOrderEvent implements OptimoveEvent {
private List<CartItem> cartItems;
public PlacedOrderEvent(List<CartItem> cartItems) {
this.cartItems = cartItems;
}
@Override
public String getName() {
return "placed_order";
}
@Override
public Map<String, Object> getParameters() {
Map<String, Object> params = new HashMap<>();
int totalPrice = 0;
for (int i = 1; i < this.cartItems.size(); i++) {
CartItem item = this.cartItems.get(i);
params.put("item_name_" + i, item.name);
params.put("item_price_" + i, item.price);
params.put("item_image_" + i, item.image);
totalPrice += item.price;
}
params.put("total_price", totalPrice);
return params;
}
}