Tracking Events

Event tracking is used to track user behavior in an app. And later based on the same tracked behavior you can target those users for sending relevant notifications. Make sure to track all the events relevant to your business, so that your product managers and marketers can segment your app users and create targeted campaigns. For eg. You can track what a user is purchasing, whether has a user added an item to cart etc.

📘

DEFAULT SDK EVENTS:

We track certain events by default in our SDK, so please make sure to use the default events instead of tracking a new event for the same scenarios. Find the list of default events tracked by SDK here.

How to track events?

Every event has 2 parts to it, the "name" of the event and the properties/attributes of the event. You have to make use of MOProperties to track events and their attributes.

For eg. The following code tracks a Successful Purchase event. We are including attributes like the Product Name, a Brand Name that describes the event we are tracking.

let eventAttrDict = NSMutableDictionary()
eventAttrDict["ProductName"] = "iPhone XS Max"
eventAttrDict["BrandName"] = "Apple"
eventAttrDict["Items In Stock"] = 109

let eventProperties = MOProperties.init(attributes: eventAttrDict)

eventProperties.addAttribute(87000.00, withName: "price")
eventProperties.addAttribute("Rupees", withName: "currency")
eventProperties.addAttribute(true, withName: "in_stock")
eventProperties.addDateEpochAttribute(1439322197, withName: "Time added to cart")
eventProperties.addDateISOStringAttribute("2020-02-22T12:37:56Z", withName: "Time of checkout")
eventProperties.addDateAttribute(Date(), withName: "Time of purchase")

eventProperties.addLocationAttribute(MOGeoLocation.init(latitude: 12.23, andLongitude: 9.23), withName: "Pickup Location")
MoEngage.sharedInstance().trackEvent("Successful Purchase", with: eventProperties)
// track event example
NSMutableDictionary* eventAttrDict = [NSMutableDictionary dictionary];
eventAttrDict[@"ProductName"] = @"iPhone XS Max";
eventAttrDict[@"BrandName"] = @"Apple";
eventAttrDict[@"Items In Stock"] = @109;

MOProperties* eventProperties = [[MOProperties alloc] initWithAttributes:eventAttrDict];
[eventProperties addAttribute:@87000.00 withName:@"price"];
[eventProperties addAttribute:@"Rupees" withName:@"currency"];
[eventProperties addAttribute:[NSNumber numberWithBool:true] withName:@"in_stock"];

[eventProperties addDateEpochAttribute:1439322197 withName:@"Time added to cart"];
[eventProperties addDateISOStringAttribute:@"2020-02-22T12:37:56Z" withName:@"Time of checkout"];
[eventProperties addDateAttribute:[NSDate date] withName:@"Time of purchase"];

MOGeoLocation* pickupLocation = [[MOGeoLocation alloc] initWithLatitude:12.23 andLongitude:9.23];
[eventProperties addLocationAttribute:pickupLocation withName:@"Pickup Location"];

[[MoEngage sharedInstance] trackEvent:@"Successful Purchase" withProperties:eventProperties];

📘

Non-Interactive Events

Events that should not affect the session duration calculation in anyways in MoEngage Analytics should be marked as a Non-Interactive event. Refer this for more info on the same.

Please make sure that you are tracking event attributes without changing their data types. Also, make sure the attributes have the same data type across platforms . For instance, in the above purchase event, price is tracked in the numeric form, therefore when the same event is tracked in Android SDK make sure to track it as a number and not string. Our system detects the data type automatically unless you explicitly specify it as a string.

❗️

RESTRICTIONS :

  • Event names and attributes should not contain any special characters other than "_". It can contain spaces and underscore.
  • In the SDK we reject events with invalid event-attributes. Event attribute values can only have Strings, Numbers & an array(strings or numbers) or else events might get rejected.

If you don’t have any attributes, just pass nil as second argument. For eg.

MoEngage.sharedInstance().trackEvent("Event Name", with: nil)
[[MoEngage sharedInstance] trackEvent:@"Event Name" withProperties:nil];

Manual Sync

For syncing the tracked events instantaneously, use the syncNow: method as shown below:

MoEngage.sharedInstance().syncNow()
[[MoEngage sharedInstance] syncNow];

Change Periodic Flush Interval

By default SDK performs a periodic flush(syncing the events tracked in the SDK) once every 60 sec. Incase if you want to update the periodic flush interval, use setFlushInterval: method as shown below to provide new interval in number of seconds:

MOAnalytics.sharedInstance().setFlushInterval(100)
[[MoEngage sharedInstance] setFlushInterval:100];

Disable Periodic Flush

To disable periodic flush made every 60 sec from the SDK use disablePeriodicFlush: method as shown below:

MOAnalytics.sharedInstance().disablePeriodicFlush()
[[MoEngage sharedInstance] disablePeriodicFlush];

Testing events after integration

Login to the MoEngage account with the credentials provided for your app.

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. For this, you have to Initialize the SDK properly

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 can take upto 20 minutes to show up in the dashboard

While testing it is recommended to enable logs in Debug Mode.

SDK prints list of all events which are synced in the current flush, so you can always refer to the logs to check if the events tracked by you are being sent to backend or not. Also logs provide info about if the sync with backend was successful or not. (In case it is unsuccessful, SDK saves all the tracked events and attempts to sync again in next flush attempt)


What’s Next