Unleash the Power of Analytics: Uncover Google Analytics Insights Through Conversations with a Chatbot

Marcello Dichiera
Data And Beyond
Published in
7 min readJul 1, 2023
Image by author

The power of pandasai library and streamlit to create a chatbot that can answer to your questions about your google analytics data

Introduction

I will showcase how to create a chatbot that can answer to your questions about your google analytics data. The chatbot will be able to answer questions like:

- can you summarize the topic of the dataset?

- plot a bar chart of column x showing top 5 values in descending order

- How many visitors we had in total? And many more

Webapp Demo

You can find the full source code in my github repo here.

The data

The dataset is available at this link, you need to have a google account to download it. The dataset used is a Google Analytics public dataset provided through Bigquery.

The main library used

The library used to create the chatbot is pandasai, created by Gabriele Venturi. It is a library that allows you to create a chatbot that can answer to questions about your data.

It is based on the pandas library and openai and it is very easy to use.

Also you need to have an openai account to use it. To find more information about openai and how to create an account you can visit openai website.

For the frontend of the chatbot I used Streamlit, a library that allows you to create webapps in python. You can find more information about it at this link.

The code

You can find the code of the chatbot I created in my github repository here. Now let’s see how to create the chatbot.

The chatbot

First of all you need to install pandasai library and streamlit library via pip, you can use the requirements.txt file in my github repository to install all the dependencies.

After you installed the library and created your environment you can start coding. For simplicity, I will use my openai api key directly in the code (of course is masked for the sake of my bank account :-)).

I will list here below the content of the requirements.txt file:

pandasai==0.5.5 #this version is the one that works with if you want to ask the chatbot to create plots and charts, previous versions they don't work really well with charts.
openai #this is the openai library
pandas
streamlit

I will split the full code in 2 parts, the full code is in the app.py file that you will use as webapp file

Part One: the libraries and the pandasai function

import os
import pandas as pd
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
from pandasai.middlewares.streamlit import StreamlitMiddleware
import streamlit as st

# instantiate LLM with PandasAI

llm = OpenAI(api_token='sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx', engine='gpt-3.5-turbo')

pandas_ai = PandasAI(llm, middlewares=[StreamlitMiddleware()], verbose=False)

Look how short is the code to instantiate the pandasai library.

In the first part of the code I import the libraries I need, I instantiate the openai library with my api key and I instantiate the pandasai library with the openai library and the streamlit middleware. The streamlit middleware is needed to create the webapp and to allow the chatbot to interact with the user and to show the charts and plots.

Part Two: the webapp with Streamlit

## streamlit app

# streamlt app page config

st.set_page_config(page_title='Google Analytics Dataset Chatbot with PandasAI', page_icon='🤖', layout='wide')

# title

st.title('📉 Google Analytics Dataset Chatbot with PandasAI')

# subtitle

st.subheader('A conversational AI chatbot that can answer questions about your Google Analytics data.')

# dataset info and download link

st.info('The dataset used is a Google Analytics public dataset provided through Bigquery that can be downloaded from [Here](https://console.cloud.google.com/marketplace/product/obfuscated-ga360-data/obfuscated-ga360-data?project=hotelcustomerdata).')

st.success('Please upload your Google Analytics data in excel format. After the file is uploaded, you will see the first 5 rows of the dataset. Then, you will see an input text area wher you can ask a questions about the dataset and get an answer from the chatbot.')

# upload file

uploaded_file = st.file_uploader('Upload your Google Analytics data in excel format', type=['xlsx'])

# check if file is uploaded

if uploaded_file is not None:
# store the file in a Pandas dataframe
df = pd.read_excel(uploaded_file)

# describe the dataframe

st.warning('Showing the 5 first rows of the dataframe.')

# show the first 5 rows of the dataframe
st.write(df.head())

# create an input text box to start asking questions about the dataset and get answers from the chatbot

st.subheader('Ask a question about your Google Analytics data and get an answer from the chatbot.')

# create an input text box to start asking questions about the dataset and get answers from the chatbot

question = st.text_input(label='Ask a question')

# check if the question is not empty

if st.button('Generate Response'):
if question:
with st.spinner('Generating response, please wait...'):
# get the response from the chatbot
#pandas_ai.run(df,question, is_conversational_answer=True)
# show the response
st.write(pandas_ai.run(df,question, is_conversational_answer=True))
else:
st.warning('Please ask a question.')

I could never stop to say thanks to Streamlit creators!! It is so easy to create a webapp with it. In the code above I created the webapp, I added a title, a subtitle, a description of the dataset and a link to download it, an upload file button to upload the dataset, a text input box to ask questions to the chatbot and a button to generate the response from the chatbot.

For this example, the file extension is xlsx, but you can use any other file extension supported by pandas. After the file is uploaded, the first 5 rows of the dataset are shown and then you can start asking questions to the chatbot.

The st.text_input(label=’Ask a question’) creates an input text box where you can ask a question to the chatbot. This is the typical text field you see on ChatGPT for example. The st.button(‘Generate Response’) creates a button that you can click to generate the response from the chatbot.

The st.write(pandas_ai.run(df,question, is_conversational_answer=True)) generates the response from the chatbot and shows it in the webapp. As you can see, after instantiating the pandas_ai, you can easily ‘’run’’ the magic of the chatbot with the pandas_ai.run(df,question, is_conversational_answer=True) function. The is_conversational_answer=True is needed to generate the response from the chatbot.

To launch the webapp via streamlit, you have to go in your terminal under the folder where you have the app.py file and run:

streamlit run app.py

Full Code of app.py

import os
import pandas as pd
from pandasai import PandasAI
from pandasai.llm.openai import OpenAI
from pandasai.middlewares.streamlit import StreamlitMiddleware
import streamlit as st

# instantiate LLM with PandasAI

llm = OpenAI(api_token='sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx', engine='gpt-3.5-turbo')

pandas_ai = PandasAI(llm, middlewares=[StreamlitMiddleware()], verbose=False)

# streamlit app

# streamlt app page config

st.set_page_config(page_title='Google Analytics Dataset Chatbot with PandasAI', page_icon='🤖', layout='wide')

# title

st.title('📉 Google Analytics Dataset Chatbot with PandasAI')

# subtitle

st.subheader('A conversational AI chatbot that can answer questions about your Google Analytics data.')

# dataset info and download link

st.info('The dataset used is a Google Analytics public dataset provided through Bigquery that can be downloaded from [Here](https://console.cloud.google.com/marketplace/product/obfuscated-ga360-data/obfuscated-ga360-data?project=hotelcustomerdata).')

st.success('Please upload your Google Analytics data in excel format. After the file is uploaded, you will see the first 5 rows of the dataset. Then, you will see an input text area wher you can ask a questions about the dataset and get an answer from the chatbot.')

# upload file

uploaded_file = st.file_uploader('Upload your Google Analytics data in excel format', type=['xlsx'])

# check if file is uploaded

if uploaded_file is not None:
# store the file in a Pandas dataframe
df = pd.read_excel(uploaded_file)

# describe the dataframe

st.warning('Showing the 5 first rows of the dataframe.')

# show the first 5 rows of the dataframe
st.write(df.head())

# create an input text box to start asking questions about the dataset and get answers from the chatbot

st.subheader('Ask a question about your Google Analytics data and get an answer from the chatbot.')

# create an input text box to start asking questions about the dataset and get answers from the chatbot

question = st.text_input(label='Ask a question')

# check if the question is not empty

if st.button('Generate Response'):
if question:
with st.spinner('Generating response, please wait...'):
# get the response from the chatbot
#pandas_ai.run(df,question, is_conversational_answer=True)
# show the response
st.write(pandas_ai.run(df,question, is_conversational_answer=True))
else:
st.warning('Please ask a question.')

Conclusion

The possibilities to create chatbots easily to ‘’interrogate’’ your data are now endless, as you may know new libraries such as langchain, pandasai, etc. are coming out every day.

I am very excited to see what the future will bring us in this field, helping us to create more and more complex chatbots that can answer to our questions about our data and also to allow people that don’t know how to code to create their own chatbots or to use the chatbots created by others.

Large Language Models are the future of chatbots and I am very excited to see what the future will bring us in this field.

In this article I showed how to create a chatbot that can answer to your questions about your google analytics data. The chatbot is based on pandasai library and it is very easy to use.

You can find the code of the chatbot in my github repository here. I hope you enjoyed this article, and happy to hear any comment, feedback. The web app is just a prototype and definitely there are areas of improvements and upgrading.

Author:

I write about data science, python coding projects and data driven marketing. I also provide data and business mentorship for data novice or data entry level people.

You can follow me on Medium, and Twitter, or visit my website and Github page.

--

--

Marcello Dichiera
Data And Beyond

I am an experienced data driven and business leader with over 10 years of experience in the field.