Tracking Events
Tracking events is how you record any actions your users perform, along with any properties that describe the action. Every trackEvent call records a single user action. We recommend that you make your event names human-readable, so that everyone on your team can know what they mean instantly.
You can track an event using trackEvent with the event name and it's characteristics (attributes/properties). Make sure you have implemented Activity Tracking before you can track an event.
Every event has 2 attributes, action name and key, value pairs which represent additional information about the action. Add all the additional information which you think would be useful for segmentation while creating campaigns. For eg. the following code tracks a purchase event of a product. We are including attributes like amount, quantity, category which describe the event we are tracking.
val properties = Properties()
properties
// tracking integer
.addAttribute("quantity", 2)
// tracking string
.addAttribute("product", "iPhone")
// tracking date
.addAttribute("purchaseDate", Date())
// tracking double
.addAttribute("price", 5999.99)
// tracking location
.addAttribute("userLocation", Geolocation(40.77, 73.98))
MoEHelper.getInstance(applicationContext).trackEvent("Purchase", properties)
Properties properties = new Properties();
properties
// tracking integer
.addAttribute("quantity", 2)
// tracking string
.addAttribute("product", "iPhone")
// tracking Date
.addAttribute("purchaseDate", new Date())
// tracking double
.addAttribute("price", 5999.99)
// tracking location
.addAttribute("userLocation", new Geolocation(40.77, 73.98));
MoEHelper.getInstance(context).trackEvent("Purchase", properties);
context
- context instance, please change the name accordingly
Please make sure that you are tracking event attributes without changing their data types. For instance, in the above purchase event, amount and quantity are tracked in the numeric form. Our system detects the data type automatically unless you explicitly specify it as a string.
The SDK by default tracks a certain set of commonly used events, make sure you use those events instead of tracking similar events again. Refer to the documentation to know more about the default events tracked by the SDK.
Analytics
Starting from version 9.7.01 MoEngage SDK has started tracking user session and application traffic source. Refer to the Sessions in MoEngage Analytics and Source Analysis in MoEngage Analytics to learn more about how user session and application traffic source tracking works.
With user session tracking we have introduced the flexibility to selectively mark events as non-interactive.
What is a non-interactive event?
Events which do not affect the session calculation in anyways are called non-interactive events. Non-interactive events have the below properties
- Do not start a new session.
- Do not extend the session.
- Do not have information related to a user session.
How to mark an event as non-interactive?
Any event can be marked as non-interactive using the setNonIteractive()
in the PayloadBuilder
provided by the SDK to build event attributes.
Example
val properties = Properties()
properties.addAttribute("quantity", 2)
.addAttribute("product", "iPhone")
.addAttribute("purchaseDate", Date())
.addAttribute("price", 5999.99)
.addAttribute("currency", "dollar")
.setNonInteractive()
MoEHelper.getInstance(applicationContext).trackEvent("Purchase", properties)
Properties properties = new Properties();
properties.addAttribute("quantity", 2)
.addAttribute("product", "iPhone")
.addAttribute("purchaseDate", new Date())
.addAttribute("price", 5999.99)
.addAttribute("currency", "dollar")
.setNonInteractive();
MoEHelper.getInstance(context).trackEvent("Purchase", properties);
Testing Events
Login to the MoEngage dashboard
Look at top left and Switch to Test environment. Ensure that your testing is done on the test environment to keep the test data separate from the Live data.
After adding event tracking in the app as shown above, you can visit Recent Events link and check whether the events are being tracked.
Events are sent as a batch when your app goes to background.
Updated over 5 years ago