2

We want know how many times each user calls IBM Watson Assistant service from client application and its billing details per user. I am trying to enable user metrics for watson assistant service based on this URL(https://console.bluemix.net/docs/services/assistant/logs_oview.html#user_id) and added the headers and metadata in my node.js code. But when I check the Improve tab in conversation its not showing the user details, its showing count 0.

I am using LITE plan and below is the code.

// conversation config
var conversation = new ConversationV1({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
version_date: '2018-02-16',
version: 'v1',
context : {
        metadata : {
           "user_id": "{1234}"
           }
         },
headers: {'X-Watson-Metadata':'customer_id=user777;customer_id=xyz'}
});

app.js code:

'use strict';

require('dotenv').config({ silent: true });

var express = require('express'); // app server
var bodyParser = require('body-parser'); // parser for post requests

//setup watson services
var ConversationV1 = require('watson-developer-cloud/conversation/v1'); // 
watson sdk
var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');

var app = express();
// Bootstrap application settings
 app.use(express.static('./public')); // load UI from public folder
 app.use(bodyParser.json()); 
// conversation config

var conversation = new ConversationV1({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: process.env.CONVERSATION_USERNAME || 'replace with the 
username',
password: process.env.CONVERSATION_PASSWORD || 'replace with the 
password',
version_date: '2018-02-16', //set currenct date, check here 
https://www.ibm.com/watson/developercloud/conversation/api/v1/#versioning
version: 'v1',
context : {
metadata : {
   "user_id": "{1234}"
  }
 },
headers: {'X-Watson-Metadata':'customer_id=user777;customer_id=xyz'}
});
// Endpoint to be call from the client side for message
app.post('/api/message', (req, res) => {
var workspace = process.env.WORKSPACE_ID || '<workspace-id>';
if (!workspace || workspace === '<workspace-id>') {
    return res.json({
        'output': {
            'text': 'Please update the WORKSPACE_ID in your .env file with 
 your credential! If you did update, try to verify if the file are just 
with the name: .env'
        }
    });
  }
  var payload = {
    workspace_id: workspace,
    context: {},
    input: {}
 };
 if (req.body) {
    if (req.body.input) {
        payload.input = req.body.input;
    }
    if (req.body.context) {
        // The client must maintain context/state
        payload.context = req.body.context;
    }
 }
  // Send the input to the conversation service
 conversation.message(payload, function(err, data) {
    if (err) {
        return res.status(err.code || 500).json(err);
    }
    updateMessage(res, payload, data);
 });
});
function updateMessage(res, input, response) {
if (!response.output) {
    response.output = {};
} else if (response.output && response.output.text) {
    return res.json(response);
}
}

 module.exports = app;
data_henrik
  • 16,724
  • 2
  • 28
  • 49
user2319726
  • 143
  • 1
  • 1
  • 10

1 Answers1

1

You need to add the user id information to the context that you send when calling the message API. Each request to Watson Assistant can have a different userid, e.g., when you have a multi-tenant application and your app / server handles multiple users and their requests in parallel.

Check out the context section for the message call, it has information about where to place that data in the JSON structure. You could merge your context updates into the message payload:

{
  workspace_id: '{workspace_id}',
  input: {'text': 'Hello'},
  context: {
       metadata : {
         "user_id": "{1234}"
       }
  }
}

Once your app has sent different user_id values, you should see the following in the Improve dashboard (I tested on my Lite plan with 2 user IDs). Different active users:

2 active users

Average conversations per user: average conversations per user

In addition, you can check the logs. In that conversation entries is the user_id field in the context metadata - the same your app sent as part of the message request.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • thanks @data_henrik for your response. You mean i need pass the user details in below payload context? var payload = { workspace_id: workspace, context: {}, input: {} – user2319726 Dec 07 '18 at 12:12
  • I have followed above steps and deployed my code, still I am not seeing user details in IMPROVE tab, its showing 0 users, var payload = { workspace_id: workspace, context: {metadata : { "user_id": "{testuser}" }}, input: {} }; – user2319726 Dec 07 '18 at 12:40
  • Yes, i have got the response for the input – user2319726 Dec 07 '18 at 12:49
  • You cannot see user details in the Improve tab, only the averages are show. You would examine the actual logs. Of, if you have Plus or Premium plans, there would be user-specific billing info. – data_henrik Dec 07 '18 at 12:56
  • ok I am using Lite plan and i will not be able to see the user details. But user averages also its showing NO data, do we need to use Plus/Premium planes only to see both user details and averages ? – user2319726 Dec 07 '18 at 13:00
  • Have you checked the logs? I can see the user_id in the metadata under context. – data_henrik Dec 07 '18 at 14:33
  • Apologies for late response @data_henrik, i am able to see the user details in IMPROVE tab now. if i use the premium service I will get the report of per person api calls details in IMPROVE tab right ? (Please let me know where i can see the report details) – user2319726 Dec 10 '18 at 11:46
  • hi @data_henrik, i tried with stranded plan with above code, I am not able to see the user details in Active user tab, its showing NO data, var payload = { workspace_id: workspace, context: { metadata : { "user_id":"{user1234}" }}, input: {} }; – user2319726 Dec 18 '18 at 16:54
  • user details are updated now but i am not able to see the user id and its api calls details in IMPROVE tab, I want get the report of that now. I am using Stranded plan. – user2319726 Dec 19 '18 at 06:20
  • The details are in the logs, billing info etc. is for Premium plans (I have no experience with that). – data_henrik Dec 20 '18 at 09:43
  • okay @data_henrik, I have checked logs and billing/usage tabs but user based details are not there. i have opened a case with IBM team and they are working on this. thanks for your help. – user2319726 Dec 20 '18 at 09:55
  • @data_henrik I have a doubt regarding the user calculation for Watson in case of integration with Intercom. Are the visitors calculated as users for pricing purpose in Watson? – Yaseen Shareef Apr 27 '20 at 09:00