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:
- Open VBCS and go to services and add a new service.
- Add a POST end point as shown below for the service:
https://us-central1-mitbotanalytics.cloudfunctions.net/logMitAnalytics
- 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
Go to Actions
- Click create new Action Chain
- Use id: mitBotAnalytics and description: Mit Bot Analytics integration as shown below:
- Hit create.
- On the action chain designer, go to the variables tab and create a new variable "chainMitAnalyticObj" of type MitAnalyticsType. It will look like below:
- 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).
- 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 }}" }
- Drop a "if" activity and put the below condition
[[ $page.variables.mitIntent != undefined && $page.variables.mitIntent != '' ]]
- On the true side drop another "Assign Variables" and click assign.
- 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:
- Drop a "Call Rest Endpoint" activity which should connect the outcome of if as well as false.
- Delete the failure notification error as that is not required.
- Click select endpoint and pick logMitAnalytics service as shown below:
- Click on "Call REST Endpoint" activity and click on body -->assign. Map "chainMitAnalyticObj" to body as shown below
- Save and close. And then drop "Assign Variable" on the success end of the REST activity. Click assign.
- Assign '' to mitIntent (basically a blank value).
- 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:
- Go to sendMessageActionChain.
- Drop Assign Variables activity after existing "Call Module Function sendMessage".
- Click assign and in the dialog select mitAnalyticObject page variable.
- In the value put below expression and save
{ "sender": "user", "time": "{{ (new Date()).getTime() }}", "msg": "{{ $chain.variables.payload }}", "lastUtterance": "{{ $chain.variables.payload }}", "intent": "" }
- It will look like below
- Drop Call Action Chain activity and select mitBotAnalytics action chain as shown below
- 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
- Create a new action chain with name mitBotAnalyticsReceiver.
- Go to variables tab and create a variable called "messagePayload" of type any. Mark it required. It will look like below
- Drop "Call Module function" and assign it to "CheckMitCallNeed".
- Select input parameters and assign messagePayload to message.
- Drop a "if" activity with below condition
[[ $chain.results.callModuleFunction2 == '' ]]
- On the false side drop the "Assign Variable" and assign "mitIntent" the outcome of callModuleFunction2 as below
- On the true side, drop a "Call Module Function" activity and link it to mitProcessBotResponse.
- Assign input to same as in #4
- Select return type Object.
- Drop a "Assign Variables" and click assign.
- In the popup, assign output of "CallModuleFunction1" to mitAnalyticObject as shown below
- Drop another assign variable and click assign and put below expression
{ "sender": "bot", "time": "{{ (new Date()).getTime() }}", "lastUtterance": "{{ $page.variables.userMessage }}" }
- It will look like below. Save
- Finally, just drop a "Call Action Chain" and link "mitBotAnalytics" action chain.
- Final diagram would look like below
The final step left is to associate "mitBotAnalyticsReceiver" action chain to the message receive event. Follow these steps:
- Go to page events
- Click on messageReceived custom event
- On the right side under "run action chains" click + to add the action chain "mitBotAnalyticsReceiver"
- 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.