Google Analytics for Streamlit in 3 Easy Steps
A complete answer to an often-asked question.
Streamlit has no official support for Google Analytics — and that sucks. Google Analytics is crucial to understanding how your users behave on your application: tracking clicks, page navigations, and other key events.
There some tracking resources through the Streamlit ecosystem, namely the streamlit-analytics
extension [Github link here], but Google Analytics is still the industry standard for … **checks list** … all industries.
If you have your Streamlit web app deployed on a platform like Heroku.com or Railway.app, you are going to want access to the micro-level events and the macro-level trends of behaviors on your site.
Luckly, as is usually the way with Streamlit, there is a roundabout way of getting more complex operations to work.
Right off the .bat — What doesn’t work:
In Streamlit you can inject HTML+Javascript into your page like so:
import streamlit as st
st.markdown(body, unsafe_allow_html=True)
As long as body
is proper html, and doesnt depend on any code outside of this snippet, it should usually work fine; however, do not use this method to inject any Google Analytics.