Google Assistant Chatbase Analytics guide

By @cm-steem3/25/2018google

Google Assistant Chatbase analytics snippet

I couldn't find full examples of Chatbase integration for the Google Assistant other than the snippets in the chatbase-node readme, so here's the snippet recently created for Beyond Bitshares.

Recently, Dialogflow announced further Chatbot integration enabling session flow within the Dialogflow web UI, despite these recent improvements there is still additional functionality available within Chatbase worth exploring.

A quote from the above blog post:

We’re continually working with Chatbase, the cloud service for more easily analyzing and optimizing bots, on expanding the Analytics dashboard to help you monitor and improve agent performance.

Why use Chatbase?

Save time on bot analysis

Track key metrics like active users, sessions, and retention across multiple chat platforms.

Improve bot accuracy

Automatically identify problems and get suggestions for doing quick optimizations via machine learning.

Increase conversion rates

Visualize the flow of conversations to understand how efficiently users interact with your bot.

Source

Pre-Reqs

Include the following dependencies in your package.json:

    "@google/chatbase": "^1.1.0",
    "actions-on-google": "2.0.0-alpha.2",

Import the package: in your code:

Register on Chatbase, get your key and replace 'API_KEY' with it.

var chatbase = require('@google/chatbase')
              .setApiKey('API_KEY') // Your Chatbase API Key
              .setPlatform('Google Assistant'); // The type of message you are sending to chatbase: user (user) or agent (bot)

Create functions for errors and invalid input

function catch_error(conv, error_message, intent) {
  /*
  Generic function for reporting errors & providing error handling for the user.
  */
  chatbase_analytics(
    conv,
    `Error within intent ${intent}`, // input_message
    intent, // input_intent
    'error' // win_or_fail
  );

  if(error_message instanceof Error) {
      console.error(error_message);
  } else {
      console.error(new Error(error_message));
  }
  return conv.close(
      new SimpleResponse({
      // If we somehow fail, do so gracefully!
      speech: "An unexpected error was encountered! Let's end our Beyond Bitshares session for now.",
      text: "An unexpected error was encountered! Let's end our Beyond Bitshares session for now."
    })
  );
}

function invalid_input(conv, intent_name) {
  /*
  Reducing code duplication.
  Replace conv.close with a direction to a fallback intent in the future!
  */
  chatbase_analytics(
    conv,
    `User input invalid data within intent ${intent_name}`, // input_message
    intent_name, // input_intent
    'fail' // win_or_fail
  );

  return conv.close(
      new SimpleResponse({
      // If we somehow fail, do so gracefully!
      speech: "You provided invalid input data, try again with alternative input!",
      text: "You provided invalid input data, try again with alternative input!"
    })
  );
}

Then create the chatbase_analytics function:

Similarly in this section, change the 'API_KEY' to your chatbase analytics key.

function chatbase_analytics(conv, input_message, input_intent, win_or_fail) {
  /*
  Integrating chatbase chat bot analytics.
  Will help optimize user experience whilst minimizing privacy impact.
  */
  var userId = conv.user.id;

  if (win_or_fail === 'Win') {
    // For reporting successful bot interaction
    chatbase.newMessage('API_KEY')
    .setPlatform('Google Assistant')
  	.setMessage(input_message)
  	.setVersion('1.0')
  	.setUserId(userId.toString())
    .setAsTypeUser() // sets the message as type user
    .setAsHandled() // set the message as handled -- this means the bot understood the message sent by the user
    .setIntent(input_intent) // the intent of the sent message (does not have to be set for agent messages)
    .setTimestamp(Date.now().toString()) // Only unix epochs with Millisecond precision
  	.send()
  	.then(msg => console.log(msg.getCreateResponse()))
  	.catch(err => console.error(err));
  } else {
    // For reporting fallback attempts
    chatbase.newMessage('API_KEY')
    .setPlatform('Google Assistant')
    .setMessage(input_message)
    .setVersion('1.0')
    .setUserId(userId.toString())
    .setAsTypeUser() // sets the message as type agent
    .setAsNotHandled() // set the message as not handled -- this means the opposite of the preceding
    .setIntent(input_intent) // the intent of the sent message (does not have to be set for agent messages)
    .setTimestamp(Date.now().toString()) // Only unix epochs with Millisecond precision
    .send()
    .then(msg => console.log(msg.getCreateResponse()))
    .catch(err => console.error(err));
  }
}

Then finally - call the function at the end of intents

For successful intent outcome

chatbase_analytics(
  conv,
  'About page', // input_message
  'About', // input_intent
  'Win' // win_or_fail
);

For invalid user input outcome

return invalid_input(conv, `account_balances`);

For handling errors

return catch_error(conv, error_message, 'account_balances');

Input message contents?

Try to include useful information, like what the user did in the function, including potentially limited input variable information.


Have any questions about the above? Just write a comment below!

Cheers,
@cm-steem

191

comments