Optimize your chatbot with Google Analytics
Tags: chatbot, csml, startup
Published: 2020-08-03 10:45:36

The **golden rule** when creating any kind of online service is to monitor its **activity** through analytics. Any business needs analytics, they are helping leaders take decisions, increase revenues, optimize processes among other things.

Chatbots are no exceptions to this rule and they also need their data to be analysed in order to improve. Right now, any chatbot company provides data like the number of messages sent and received, number of users, etc… and this is great to measure the chatbot activity on a macro level, but more often than not, chatbot creators also need some use-case oriented analytics, like a **conversion funnel** for a **newsletter signup chatbot**.

### Google Analytics for chatbots
Google Analytics is a very powerful tool that pretty much everybody has used in the past. Most people know it as a service that provides analytics about the audience behaviour on a website, but it can also be used with chatbots, and in this case to create a funnel.

## The overall mechanism
In order to create a funnel, we’ll need the chatbot to send:
- **A starting event**: the user starts the flow
- **Events throughout the flows**, very much like checkpoints, that will show us when a user drops
**An ending event**: the user subscribed

All these events must be sent to Google Analytics.

Let’s see how that works with [CSML](https://www.csml.dev?utm_source=dev.to&utm_medium=article&utm_campaign=ganalytics) using [the development studio](https://studio.csml.dev?utm_source=dev.to&utm_medium=article&utm_campaign=ganalytics).
*In the example below, we’ll assume you’ve created a free account on the studio.*

## Step 1: the chatbot
Let’s create a simple newsletter signup chatbot, only three questions in three different steps that eventually trigger a Zapier event to register the user.

Right now this chatbot is not yet linked to Google Analytics but at least we know what it’ll look like.
```js
start:
  say "Hi, let's subscribe to an awesome newsletter 💌 !"
  // Starting event should go here
  goto firstname

firstname:
  say "What's your firstname?"
  hold
  remember firstname = event
  // First flow event
  goto lastname

lastname:
  say "What's your lastname?"
  hold
  remember lastname = event
  // Second flow event
  goto email

email:
  say "What's your email address?"
  hold
  if (!event.contains("@")) {
    say "Please enter a valid email address"
    goto email
  }
  remember email = event
  // Third flow event
  goto save

save:
  do Fn("zapier",
    hook="myhook/myhookid",
    payload={
      "firstname": firstname,
      "lastname": lastname,
      "email": email
    }
  )
  say "Ok **{{firstname}} {{lastname}}**, I have registered your email **{{email}}** to our newsletter!"
  say "Enjoy the great content 🎉"
  // Ending event
  goto end
```

## Step 2: Setting up Google Analytics

### a. Get the Google Analytics Property ID
First of all you will obviously need a Google Analytics account. You can then either use an existing property (website account), or create one. To go forward, you will only need the property ID that looks like “UA-xxxxxxx-x”.
To find this ID, open Google Analytics, go to “Admin” at the bottom left of the screen, then click on “Property settings”, you’ll then see the ID under “Tracking ID”.

### b. Setup the Google analytics App
Now we need to connect the chatbot to Google Analytics. In order to do so, you can use the Google Analytics App in the Studio, you’ll just need to provide your Google Analytics **property ID** (UA-xxxxxxx-x) when you setup the app.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o0vkpm2gey2ut43agwzp.png)


### c. Setup the Uuid App
Since you want Google analytics to be able to track sessions, you’ll need to provide a **unique ID** per user. Luckily, there is an app for that : uuid. Go to the app store and set it up.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fljgnqfw8lffkpwn343c.png)


## Step 3: Get the chatbot to send the events at the right time
Remember the code we wrote earlier ? We’re going to edit it as follow:
- Adding a unique id for each new user
- Adding the events checkpoints

It should look like this:
```js
start:
  // If the user doesn't exist, we create a new id using the uuid app
  if (!user_id) remember user_id = Fn("utils/uuid", version="v4")
  say "Hi, let's subscribe to an awesome newsletter 💌 !"
  do Fn("google/analytics", user_id=user_id, category="csml_nl_chatbot", action="flow_process", label="start")
  goto firstname

firstname:
  say "What's your firstname?"
  hold
  remember firstname = event
  do Fn("google/analytics", user_id=user_id, category="csml_nl_chatbot", action="flow_process", label="firstname")
  goto lastname

lastname:
  say "What's your lastname?"
  hold
  remember lastname = event
  do Fn("google/analytics", user_id=user_id, category="csml_nl_chatbot", action="flow_process", label="lastname")
  goto email

email:
  say "What's your email address?"
  hold
  if (!event.contains("@")) {
    say "Please enter a valid email address"
    goto email
  }
  remember email = event
  do Fn("google/analytics", user_id=user_id, category="csml_nl_chatbot", action="flow_process", label="firstname")
  goto save

save:
  do Fn("zapier",
    hook="myhook/myhookid",
    payload={
      "firstname": firstname,
      "lastname": lastname,
      "email": email
    }
  )
  say "Ok **{{firstname}} {{lastname}}**, I have registered your email **{{email}}** to our newsletter!"
  say "Enjoy the great content 🎉"
  do Fn("google/analytics", category="csml_nl_chatbot", action="flow_process", label="end")
  goto end
```

As you can see, we create “checkpoints” throughout the flow, every time a user reaches one of these checkpoints, an event will be sent to Google Analytics.

Now if you try it out, you should see something that looks like the screenshot below.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g9acjib6ungl5t575ifv.png)

## Step 4: The funnel!
It’s hard to believe, but the funnel is ready…you only need to wait for Google Analytics to update it. Updates happen the next day, so once you see the events in the “Realtime > Events” menu, just wait 24 hours and then go to “Behavior > Events > Events flow”, and you’ll should see the funnel below!
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jofv0tocffcjtg82kik5.png)

That’s it, you’re now ready to analyse and optimize your chatbots.

Here are a few ideas:

- You could perform AB testing by creating two steps doing the same thing and randomly sending the users to one step or the other. Each of these two steps would send different labels to Google Analytics, you would then be able to check which step performs best.
- In the example above, you could send an event to Google Analytics if the users enter an invalid email address, then you would be able to see how many users drop after entering an invalid email address.
- You could also ask more information to the users if your funnel shows very low drop rate.

I am sure there are tons of possible optimizations, what would be the first optimization on your list? Share it in the comments!

For more informations about CSML, visit [csml.dev](https://www.csml.dev/?utm_source=dev.to&utm_medium=article&utm_campaign=ganalytics)!