Google Assistant Chatbase Analytics guide
google·@cm-steem·
0.000 HBDGoogle Assistant Chatbase Analytics guide
 # 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](https://github.com/google/chatbase-node/), so here's the snippet recently created for [Beyond Bitshares](https://steemit.com/bitshares/@cm-steem/beyond-bitshares-google-assistant-upgrading-to-latest-action-on-google-alpha-release). Recently, Dialogflow announced further Chatbot integration [enabling session flow within the Dialogflow web UI](https://blog.dialogflow.com/post/session-flow-analytics/), 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](https://chatbase.com/welcome/) ## 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
👍 cm-steem, c4h8n8o8, arimaulana, emenim, scalextrix, fernandorivera, trixiedraws, steemtruth, anoopsingh, konelectric, vortac, xxcynicalkidxx, jkkim, unrared, itsragged, profitgenerator, o0pepper0o, sc-steemit, michaelwilshaw, parejan, neuralminer, mandagoi, hottopic, hashmieali77, byresteem, jsj9346, cokenrum, peaceandlove, yesiruolaide, mjzo, agnessejanot, ankavatar, art-is, artdirector, artworld, banka, bilgi, bitcoinfast, boklassen, bursa, dawnasheelagh, doryaeriell, eksisozluk, elladinenolana, eolandananni, supandipunoe, everdayfood, franchescasacco, fueprojacan1988, funnytime, juninalethia, kaanight, lifernesec1973, mariano35, mariginal, montecarlo35, naturecenter, nejdat, odl, ohadiyorum, onedio, orhun, osmanli, shizuka, racdaolato1976, simay, sohbet, tamer5461, teddieelga, traveled, valenesiana, zkan, lyon89, ayraaprilia, micaiah, graceelecte, magloria, dmilliz, ebargains, ashokkumarsharma, xelda, saiful39, drac59, fredeibarra, talhadogan, steemit-new, komedi, mayagag54, bestnews, nicegirls, porschegts, luxurycars, selenagomes, katy.perry, clipmovie, koreanmovies, aaaaaaaaaaa, steemit.admin, steemit.coin, steemit-coins, animalsworld, steemit-follow, steemit.art, a1453a, moviesandtrailer, steemit-comic, interestingman, steemit-photo, steemit-dtube, steemit-bitcoin, steemit-kr, steemit-world, ottoman, slowmotion, avasome, fantasticgirl, yarasa, tomraider, upmea, bodyguard, freesoundeffects, bass-boosted, semad, supervote, xwhale, mavibalina, aa1, trendpost, steemitwall, guzelsozler, dvote, funny-meme, ottomanwhale, music-maker, trendingpost, steem15, minersean, dekteng, yoon0862, marlysa16, blockchained, on0tole, zaebars, gomeravibz, blckchnd, smailer, miikkikiikki, happyhippy87, rusteemitblog, marel, spacecowboy, blockained, marco.world, humanabstract, whalepunk, criticalkush, topgame, sveokla, vikx, pheonike, anasya, marinabogumil, kell234, hyiparena, dmilash, mama-steem, uuuhha, nadin3, xanoxt, victoriart, tatianka, dan-bn, elena-singer, anton333, ekaterinka, borishaifa, htyfn, therajmahal, steemlift, max-max, kiton, kostaslou, erikkartmen, empereur, xionic, ziaulhaqnsm, honeythief, artem.timofeev, ubg, ilikechocolate, zipity, highvote, smitcaen, rothmans, bandumona, newacco, konte, meeertcakir, plakout, varvara89, zarifkumaro, votemen, casonova, saadetarabaci, plipplek, victorivera, mateosiit, ayanlarevc, aatifmele, votevote, quickman, jeryalex, alexverge, urkmez, bicopak, arrowmed, highcom, speedgonzales, meertcakir, ronkojic, fishhead, nazarmobil, fingerman, blockswip, dubailive, mynames, zamazingo1898, tefanii, mikomike, holmesx, kocero232, hearter,