How to create your personal habit tracker on google assistant

The laziest way to track your habits, by a programmer for all programmers!

Amin Moghaddam
Towards Data Science

--

Prologue

For a long period of time, I have been trying to add new habits to my daily life. I tried various applications, paper templates, etc. but none of them worked well. I usually forget to fill them or feel so tired to do so. Recently, I got a new Google nest mini and It was fun chatting a little bit with it before sleep. So, I thought why not program the google assistant to track my habits for me.

How it works

Me: “Hey Google, Talk to Habit Tracker”
Assistant: “Getting the test version of Habit Tracker”
HabitTracker: “Did you exercise today?”
Me: “Yes”
… more questions …
HabitTracker: “Did you read any papers?”
Me: “No”
HabitTracker: “I asked all my questions”
After that, a new row is inserted in my “Habits” google sheet:

Nothing feels better than filling a spreadsheet with your voice!

How to create your own

What we are going to do in this tutorial:
1. Create a Google sheet and add your own questions
2. Create a service which can edit your sheet
3. Setup your Dialogflow project
4. Talk to your habit tracker

You need some basic programming knowledge and a Google developer account in order to complete this walkthrough. It is beneficial to know a little about Dialogflow concepts if you want to modify some parts of the code. It would take around an hour to complete the walkthrough.

1. Create your google sheet

The script that I use for filling the google sheet has some assumptions that you should follow:

  • Keep the first column for time
  • Keep the first two rows for questions

You can basically duplicate this sheet and add your own questions. Save your spreadsheet key for the later steps.

2. Create a service for editing your sheet

Now you need to make a google service account and share this document with it. Check this tutorial for this section. After this step, you should have your client_email and private_key.

3. Setup your Dialogflow project

Dialogflow is the Google service for creating conversational bots. We need to set it up and connect it to our own web service to make things work. You need to upload some files from my GitHub project in the Dialogflow which can be downloaded directly from here.

  1. First, you need to create a new project.
  2. From the left menu, go to the entities section and upload the Yes-No entity. Entities are concepts that Dialogflow can detect from the conversation and pass them to our service.
  3. From the left menu, go to the intents section and delete the Default Welcome Intent. Then, upload the dynamic intent.
  4. From the left menu, go the Fullfillment menu and enable the Inline Editor. Copy the index.js content from here and package.json content from here and paste them in their tabs. Now you should modify index.js and add your own configuration. This is basically the first 3 lines in the code.
    All the logic is inside index.js it creates a Dialogflow app to handle the requests, it connects to google spreadsheet and reads the questions and add a new row when all the questions are answered.
  5. Deploy your function! Unfortunately, this is not the end. Firebase functions don’t allow you to make requests to other services in the test plan. You also have to upgrade your plan in Firebase. The Blaze plan is almost free since your function is not going to be invoked more than a few times per day.

4. Test everything

From the right section of Dialogflow you can start talking with your Habit Tracker by saying or typing “Track my habits”. You can check Firebase functions log to see everything is going smooth over there or not. In the end, you can test it directly on Google assistant by clicking on “See how it works on Google Assistant” link and deploy your bot to all your devices from there.

Epilogue

It’s been around 10 days that I’m using google assistant for tracking my habits and It really feels better than the other methods.
First of all, I have more motivation to do my habits since it really feels good to say “YES” to my google assistant when it asks about them; somehow it’s similar to the time when I was a kid and my teacher asked me if I did my homework. It felt really good when I said yes and I felt shameful when I said no.
Second, it’s really easy and doesn’t disturb your sleep. You don’t have to look at some screen, you don’t need your mobile next to you or a pen and paper.
Last but not least, it really feels good that the data is saved on a google sheet. I can do whatever I want with my data and no third-party has access to it. This is not easy with paper habit trackers nor with third-party apps that don’t have export functionalities.

Hope you enjoyed my article and happy habit tracking!

--

--