How to add Google Analytics or JS code in a Streamlit app?

I wanted to set up Google Analytics for my Streamlit app. What worked for me was changing the static index.html inside the streamlit package (os.path.dirname(st.__file__)) . Just below the <head> you can add your javascript code for the analytics. This definitely isn’t good practice but I needed analytics set up urgently.

Note: I have only one app deployed, so this works great. If you have multiple apps, this might create some problems. In that case, create a different virtual environment for each of them and repeat the same.

9 Likes