This article assumes you already have a VBCS based ODA client using Oracle Web SDK 20.x+. Mit Bot Analytics can be configured using the following steps:



  • The test service may fail but ignore that and save it.


Create Page Variables

A custom type needs to be created to easily pass data to the log analytics service. 

  • Go to variables --> Types tab in the main-flow and create a new type "MitAnalyticsType" as below


  • Click add fields and add below fields of type string 
    • device
    • intent
    • lastUtterance
    • msg
    • os
    • sender
    • sessionId
    • uid
    • orgName
  • Add one more field of type Number as below
    • time
  • Go to page variables and create a new variable mitAnalyticObject of type MitAnalyticType as below
  • Some of the values can be default for web based clients as below. Click each of the attribute and put below in default
    • device: web
    • os: web
    • orgName:  <UNIQUE ID PROVIDED BY MOBLIZE.IT>


Also create a string type variable named mitIntent


Implement JS functions


Go to js and create a new js function as below


 // get bot user id
 PageModule.prototype.getBotUserId = function() {
  console.log("get bot user id...");
  var userId =  window.Bots.getConversationHistory().userId;
  console.log("bot user id is:", userId)
  return userId
 };
 
 
 //this function decides if analytic call is needed or not. This is needed for the cases when intent name is passed in the botML intent.
 PageModule.prototype.checkMitCallNeed = function(message) {
    console.log("get checkMitCallNeed...");
    if(JSON.stringify(message).indexOf("#INTENT_NAME#:") > -1){
        console.log("skip intent name processing")
        var lastIntent = message.text.substring("#INTENT_NAME#:".length)
        return lastIntent
      }      
    
    return ''
 };
 
 //Process bot repsonse
 PageModule.prototype.mitProcessBotResponse = function(message) {
   var lastIntent = ''
   const ansIntIdentifier = '<div id="intentName" style="display:none">'
    if(message != undefined && message.cards != undefined){
       //if it is carousel type no need to log all the cards. just first card description is enough
       return {
         "intent": lastIntent,
         "msg": message.cards[0].description
       }
     }else if(message != undefined && message.actions != undefined){
       return {
         "intent": lastIntent,
         "msg": message.text
       }
     }else if(message != undefined && message.type == 'attachment'){
       console.log("logging as commonResponseComponent intent")
       if(message.attachment.type == 'image'){
         console.log("logging as commonResponseComponent intent of type image")
         return {
         "intent": lastIntent,
         "msg": 'Image response'
         }
       }
     }else{
       console.log("logging as answer intent")
       let text = message.text
       if(message.text.indexOf(ansIntIdentifier) > -1){
           text = text.substring(text.indexOf('</div>') + '</div>'.length)
       }
       
       //get intent text
       let intText = message.text
        intText = intText.substring(intText.indexOf(ansIntIdentifier) + ansIntIdentifier.length )
        lastIntent = intText.substring(0, intText.indexOf("</div>"))
        return {
         "intent": lastIntent,
         "msg": text
         }
     }
 };




Logging Messages

To log messages, we need to create two action chains. One for ability to make calls to the MitAnalytic REST service and another to handle the case when a bot receives the response. 


mitBotAnalytics Action Chain

  1. Go to Actions

  2. Click create new Action Chain
  3. Use id: mitBotAnalytics and description: Mit Bot Analytics integration as shown below:
  4. Hit create.
  5. On the action chain designer, go to the variables tab and create a new variable "chainMitAnalyticObj" of type MitAnalyticsType. It will look like below:
  6. Switch back to Diagram. Drop Call Module function and select getBotUserId. Select return type "String". Name this as getUserIdFunction (important as it will be used in a later step).
  7. Then drop an "Assign Variables" and click assign. In the new popup click on "chainMitAnalyticsObj" and in the value paste below:

    { 
     "uid": "{{ $chain.results.getUserIdFunction }}",
     "sessionId": "{{ $chain.results.getUserIdFunction }}",
     "msg": "{{ $page.variables.mitAnalyticObject.msg }}",
     "lastUtterance": "{{ $page.variables.mitAnalyticObject.lastUtterance }}",
     "sender": "{{ $page.variables.mitAnalyticObject.sender }}",
     "time": "{{ $page.variables.mitAnalyticObject.time }}",
     "os": "web",
     "device": "web",
     "orgName": "<UNIQUE ID PROVIDED BY MOBLIZE.IT>",
     "intent": "{{ $page.variables.mitAnalyticObject.intent }}"
    }
  8. Drop a "if" activity and put the below condition
    [[ $page.variables.mitIntent != undefined && $page.variables.mitIntent != '' ]]
  9. On the true side drop another "Assign Variables" and click assign. 
  10. In the new popup, expand variable "chainMitAnalyticObj" and click on "intent" attribute and map it to page level "mitIntent" variable. It will look like below:
  11. Drop a "Call Rest Endpoint" activity which should connect the outcome of if as well as false. 
  12. Delete the failure notification error as that is not required.
  13. Click select endpoint and pick logMitAnalytics service as shown below:
  14. Click on "Call REST Endpoint" activity and click on body -->assign. Map "chainMitAnalyticObj" to body as shown below
  15. Save and close. And then drop "Assign Variable" on the success end of the REST activity. Click assign.
  16. Assign '' to mitIntent (basically a blank value).
  17. After all the above is done the action chain will look like below:


    Modify SendMessageActionChain 

    Existing SendMessageActionChain needs minor modification to call MitAnalytic Action chain and set page variables there. Do the following steps:

 

  1. Go to sendMessageActionChain.
  2. Drop Assign Variables activity after existing "Call Module Function sendMessage".
  3. Click assign and in the dialog select mitAnalyticObject page variable.
  4. In the value put below expression and save
    { "sender": "user",
     "time": "{{ (new Date()).getTime() }}",
     "msg": "{{ $chain.variables.payload }}",
     "lastUtterance": "{{ $chain.variables.payload }}",
     "intent": ""
    }
  5. It will look like below
  6. Drop Call Action Chain activity and select mitBotAnalytics action chain as shown below
  7. Finally, it will look like below:


At this point, the messages sent to ODA should log into the analytic dashboard. No intents would be logged and no responses would be logged. To do that, we need to create another action chain and attach it to messageReceived event. follow the steps to create last action chain now:


Create "mitBotAnalyticsReceiver" action chain


  1. Create a new action chain with name mitBotAnalyticsReceiver.
  2. Go to variables tab and create a variable called "messagePayload" of type any. Mark it required. It will look like below
  3. Drop "Call Module function" and assign it to "CheckMitCallNeed".
  4. Select input parameters and assign messagePayload to message.
  5. Drop a "if" activity with below condition
    [[ $chain.results.callModuleFunction2 == '' ]]
  6. On the false side drop the "Assign Variable" and assign "mitIntent" the outcome of callModuleFunction2 as below
  7. On the true side, drop a "Call Module Function" activity and link it to mitProcessBotResponse.
  8. Assign input to same as in #4
  9. Select return type Object.
  10. Drop a "Assign Variables" and click assign.
  11. In the popup, assign output of "CallModuleFunction1" to mitAnalyticObject as shown below
  12. Drop another assign variable and click assign and put below expression
    { "sender": "bot",
     "time": "{{ (new Date()).getTime() }}",
     "lastUtterance": "{{ $page.variables.userMessage }}"
    }
  13. It will look like below. Save
  14. Finally, just drop a "Call Action Chain" and link "mitBotAnalytics" action chain.
  15. Final diagram would look like below




The final step left is to associate "mitBotAnalyticsReceiver" action chain to the message receive event. Follow these steps:

  1. Go to page events
  2. Click on messageReceived custom event
  3. On the right side under "run action chains" click + to add the action chain "mitBotAnalyticsReceiver"
  4. It will look like below



This should finish the integration of MitAnalyticSDK. 


Also for annotation of BotML read here



You can download the reference in case of issues.