X

The Mobile & Digital Assistant Blog covers the latest in mobile and conversational AI development and engagement

MCS Custom Events 101

Jeff Davies
Cloud Success Manager

Custom Events in MCS

Today I wanted to go into some detail about events in MCS. Specifically, what you can do with custom events. Many apps want to track custom events that occur on the user's mobile device. This process is well documented and a great place to get started if you want to learn about events originating on a mobile device can be found here: https://docs.oracle.com/en/cloud/paas/mobile-cloud/mcsra/op-mobile-platform-analytics-events-post.html
 
However, there are times that you may want to create custom events on the MCS platform itself instead of the mobile device. For example, when processing an order your inventory system reports back that it's current inventory level for a product is low or even zero. Another example would be if the credit card number given with the order appears to be fraudulent, or if the card was simply declined. These are backend events, but they can be just as important to track using MCS as any other event.
 
Fortunately, creating custom events for these backend events is just as easy as it is from a mobile device. There are a few things you need to know about events before we dive into the details:
  • MCS categorizes events into two broad classes: system and custom. A system event can be used to group custom events, or custom events can stand on their own.
  • Every event must have a name.
  • Every event must have a type: either system or custom. If a type is not provided, MCS will classify the event as custom.
To demonstrate how all of this works, we will create an API with some custom code. The API will take a POST of a JSON array of events and in turn call the MCS platform API that will record the events in MCS. Here is the RAML file that defines the API:
 
The implementation of the POST method is trivial:
 
/**
 * Body should be an array of events like the following:
 * [{
 *   "name" : "foobar",
 *   "type" : "custom",
 *   "timestamp" : "2017-07-25T21:14:25.123Z",
 *   "properties" : {
 *      "eventData" : "Baby!"
 *    }
 * }]
 */
service.post('/mobile/custom/GenerateCustomEvent/customevent', function(req,res) {
   var result = {};
 
   var eventArray = req.body;
   console.info(eventArray);

   req.oracleMobile.analytics.postEvent(eventArray).then(
      function(result){
         res.send(result.statusCode, result.result);
      }, function(error){
         res.send(500, error.error);
      }
   
});
 
To test the API and generate events, we will create a BASH script that calls curl in a loop passing in random event data. I vary the loop size on my script each time I run it to make the charts a little more interesting.
 
## Invoke the generate custom event API
event_names=("Order" "Fulfillment")
event_data=("Submitted" "In Progress" "Completed" )

numEventNames=2
numEventData=3

# Uncomment the next line for debugging output to the console.
#set -x

for i in `seq 1 10`;
do
    eventNameIndex=$((RANDOM%$numEventNames))
    eventName=${event_names[$eventNameIndex]}
    eventDataIndex=$((RANDOM%$numEventData))
    eventData=${event_data[$eventDataIndex]}

    eventTime=$(date +%Y-%m-%dT%H:%M:%S.000Z)
    curl -H "Accept: application/json" -H "oracle-mobile-api-version: 1.0" -H "Content-Type: application/json" \
       -H "Oracle-Mobile-Backend-ID: [your mobile backend id]" \
       -H "Authorization: Basic [your basic auth key for your backend" \
       -v -d "[{\"name\" : \"$eventName\", \"type\" : \"custom\", \"timestamp\" : \"$eventTime\", \"properties\" : {\"eventData\" : \"$eventData\"}}]" \
       -X POST \
       "https://[your mobile backend Base URL]/mobile/custom/GenerateCustomEvent/customevent"
done
 
After you run the test script you can then view the event data in the MCS Portal. Log into your MCS portal, open the main menu and click on the Analytics link and then on the Events sub-section.
Navigating to the Events page
In the drop down that prompts you to select an event, select either Orders or Fulfillment. In the second drop down control, select eventData. The chart will then show you the associated events.
Viewing your custom events graphically.
 
It's just that easy to create custom events in custom code. There are a couple of things to note:
  • In the properties json object I used the property name of eventData. you can use any name you like. In this example, orderStatus might have been a better choice.
  • You can have multiple properties, not just one.
 
Now that you know how easy it is to create an use custom events, what events are of interest to your company?

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.

Recent Content