Google Analytics for chat bots

Once you made bot, you wonder how many people use it, what requests do they perform and if there are any errors. The easiest way is to save this data to the database, but what if you need a ready-made solution?

As I can see now, there is a bunch of services with unclear pricing models. I’ll try them next time, but now, I’ll tell about connecting your bot to google analytics.

There are two types of applications which you can add to google analytics: a Mobile App and a Website. Both types work the same. The problem is leak of abstraction for chat bots. You work with pages, screens, etc, but we still could use it as workaround.

First of all wee need to decide what types of actions we have and what data types we could use. I have an initial request, ingredient deletion, open recipe page, feedback command, help command, and a “recipe not found” answer. There is also possible to count exceptions, so I have it either. Feedback, help and full recipe text are screens (or pages in case of website), the rest is actions.

As you may know, I use Java for programming, so I’ve found a pretty library for accessing analytics. Let’s add it to the project with gradle

compile 'com.brsanthu:google-analytics-java:1.1.2'

Usage of the library it’s pretty straight-forward, initialization of the client is as simple as this

GoogleAnalytics analytics = new GoogleAnalytics(config.getTrackingCode());

Posting data to analytics is easy as well. I use postAsync method, because I don’t care about realtime data.

//post event
analytics.postAsync(new EventHit("button", "remove-ingridient").clientId(message.getChatId()));
// post page view
analytics.postAsync(new PageViewHit(START_COMMAND,"Start Bot").clientId(message.getChatId()));
//post exception
analytics.postAsync(new ExceptionHit("Error sending response").clientId(chatID));

This is it. There are some problems with this approach. You’ll never get user location unless your bot requests it from users and as I realised now, you’ll have the same analytics spam which you probably have on your website.

I hope this helps. It could give you some basic data and imagination about how your bot is used, but it’s definitely not enough. Now I’m coming up with my own analytics solution.