Product Events
Product events record commerce actions on a product, such as views, add to cart and purchases, and keep product attributes up to date. General rules for events: Sending Events.
Requirements
- A successful
ping()in this installation, see Contacts. - A stable product ID for each product, the same one you use on your website or in your product feed.
Setup
Call sendProductEvent when the action happens:
- Kotlin
- Java
UserCom.default?.sendProductEvent(
"SKU-1234",
ProductEventType.ADD_TO_CART,
mapOf(
"name" to "Running Shoes",
"product_url" to "https://www.example.com/shoes/1234",
"price" to 89.99,
"currency" to "USD",
"size" to "42"
)
) { error ->
if (error != null) Log.w("App", "Product event failed: ${error.message}")
}
Map<String, Object> attributes = new HashMap<>();
attributes.put("name", "Running Shoes");
attributes.put("product_url", "https://www.example.com/shoes/1234");
attributes.put("price", 89.99);
attributes.put("currency", "USD");
attributes.put("size", "42");
UserCom sdk = UserCom.getDefault();
if (sdk != null) {
sdk.sendProductEvent("SKU-1234", ProductEventType.ADD_TO_CART, attributes, error -> {
if (error != null) Log.w("App", "Product event failed: " + error.getMessage());
});
}
ProductEventType is in the package com.user.sdk.events.
The product event appears on the contact's timeline, and the product in the panel has the attributes you sent (price, currency, size).
Handling
Attributes are product attributes
Each key in attributes is stored as a separate attribute of the product, next to the product ID. Pass the product name as name and the product page URL as product_url. A new value replaces the previous value of that product attribute. Every new key creates a new product attribute, so use a fixed set of keys with stable value types. If a key in attributes is custom_id, the product ID wins.
Values must be JSON values, see Attribute value types. Any other value, for example a Date, a Uri or a Double that is NaN, ends with InvalidData and nothing is sent.
Events in quick succession
The event time is sent with one second precision. Two events with the same product, the same event type and the same second are recorded as one event.
Methods
sendProductEvent
Queues a product event for the current contact.
Optional unless marked required
- productId
- Your product ID. Must not be empty.
- eventType
- The action, see the table below.
- attributes
- Product attributes, for example
name,product_url,price. Defaultnull. - callback
- Called with the result on the main thread. See Callback thread.
Returns nothing. Without a contact (no successful ping yet), the callback gets NotIdentified and the event is not queued. An empty product ID ends with InvalidName without a request.
UserCom.default?.sendProductEvent("SKU-1234", ProductEventType.VIEW)
ProductEventType values and how they appear in the panel:
| Value | Panel |
|---|---|
ADD_TO_CART | add to cart |
PURCHASE | purchase |
LIKING | liking |
ADD_TO_OBSERVATION | add to observation |
ORDER | order |
RESERVATION | reservation |
RETURN | return |
VIEW | view |
CLICK | click |
DETAIL | detail |
ADD | add |
REMOVE | remove |
CHECKOUT | checkout |
CHECKOUT_OPTION | checkout option |
REFUND | refund |
PROMO_CLICK | promo click |
Common errors
| Symptom | Cause | Fix |
|---|---|---|
Callback reports NotIdentified | No successful ping yet. | Call ping() first. |
Callback reports InvalidName | The product ID is empty. | Pass your product ID. |
| The panel shows many similar product attributes | Attributes use changing keys, for example with IDs or dates in the key. | Use a fixed set of keys. Put changing data in values. |
| Two quick events count as one | Same product, type and second. | Expected. Send distinct actions with distinct event types. |