Facebook Analytics - Introduction

Collecting information on your application user base is an extremely useful activity which allows you to know how many users you have and, more important, how they behave. This information can be crucial to spot usability problems or other rough edges. In other words, analytics helps you make a better application.

When you develop an AI application targeted at interacting with people (for instance, a chat bot) this is even more important because it is hard to predict every possible way the application interacts with each user.

In this post, we start covering Facebook Analytics for Apps (FAfA), an analytic tool designed by Facebook, that can be useful for capturing application analytics.

Basic FAfA Elements

Events, Parameters and User Properties

Facebook Analytic works around two main concepts: events and user properties.

User Properties are a way to attach user information to events. In this way, it is possible to query the data with respect to specific properties such as age, gender, and nationality (market segmentation).

The FAfA Dashboard

The dashboard represents the heart of FAfA. From this web application, you can explore all the received data and perform every query you like. Do you want to know how many german-speaking female users living in New York user you have? Provided that you correctly set up the user properties in your application, you can have the answer.

However, there is more than that. Another very useful type of information you can extract from the dashboard, especially for chatbot applications, is the funnels. A funnel is a specific sequence of actions the user can perform in your application. A typical example is the checkout procedure for an e-commerce website: in this case, the funnel is the sequence of steps from check-out to the order confirmation (e.g., specify shipping address, payment method, insert additional coupons, etc).

In chatbots, entire conversation contexts can be represented as a funnel. If we want to book a flight using a chatbot, the sequence of answers and questions until the flight is booked is nice candidate for a funnel.

Once we set up a funnel, we have access to many useful data such "how many users complete the funnel to the end?", "where users stop more frequently?", "how much time users spend on each step?". We will talk more about funnels in a future post.

Let's get practical!

It is time now to see how to start using FAfA in an application.

Prerequisites

FAfA does not require your application to have a Facebook login for the users. However, you need to set up two things:

Initialize your app

The first step is to connect your application with Facebook. We will assume we are writing a front-end web application.

  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'your-app-id',
      xfbml      : true,
      version    : 'v2.8'
    });
    FB.AppEvents.logPageView();
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

This is the standard way to include Facebook SDK into your web page. This code dynamically includes, loads, and initializes the SDK.

Log an Event

Suppose now we have a button and we want to log every time a user clicks on it. So, we write a function with the following code:

 function userClick()
 {
     ...
     // Do whatever the button have to do and then...
     FB.AppEvents.logEvent("userClick");
 }

In some cases, you may need to attach data to an event. For instance, if you are developing a dungeon crawler game, you may be interested in the levelCompleted event. However, this is not enough. You want also to know which level the user completed and with which score.

To do this we can use the two optional parameters for logEvent.

  FB.AppEvents.logEvent(
    "levelCompleted",
    score,  // numeric value for this event
    { level_name: "The Dark Forest" } // Additional parameters.
  );

Add User Properties

Adding user properties is still a beta feature and there is no documentation about how to use them in web applications, but we can assume that this feature should come soon. In the meantime, we can peek at the Android SDK to have an idea of how it will look like.

The first function we need to know is AppEvents.setUsersID. We use this function to set up a unique ID for the user.

FB.AppEvents.setUserID(string userID);  

After we set up the user ID, all the following events sent by the application will be assigned to this user.

Then, we can assign properties to the user such as gender, nationality, age, etc. To do this, we use the following:

let properties = {  
  'gender': 'male',
  'country': 'Italy'
  'shoeSize': '11',
  'subscription': 'premium',
}
FB.AppeEvents.updateUserProperties(properties, callback)  

Easy.

Facebook Analytics for Chat Bots

Up to this point, we have seen FAfA used in the front end. But what if I want to log events in the backed? This is really important when developing chat bots because, in this case, the front-end is a third party messaging application to which we have no access.

Fortunately, Facebook provides by default FAfA for every messenger bot application. However, default events are a bit limiting and we would like to specify our custom events. For instance, we may be interested in triggering an event every time the user books a flight or says something in particular ro every time our bot is unable to understand the user.

We will look into this problem in the next part. Stay tuned!