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

Hi @ozgurdugmeci

I am trying to implement the statcounter code. Does it look something like this?

st.markdown("""
        <!-- Default Statcounter code for product-tour-with-analytics
        https://product-tour-with-analytics.onrender.com -->
        <script type="text/javascript">
        var sc_project=AAAAAAA; 
        var sc_invisible=0; 
        var sc_security="BBBBB"; 
        var scJsHost = "https://";
        document.write("<sc"+"ript type='text/javascript' src='" + scJsHost+
        "statcounter.com/counter/counter.js'></"+"script>");
        </script>
        <noscript><div class="statcounter"><a title="Web Analytics Made Easy -
        Statcounter" href="https://statcounter.com/" target="_blank"><img
        class="statcounter" src="https://c.statcounter.com/AAAAAA/0/BBBBB/0/"
        alt="Web Analytics Made Easy - Statcounter"
        referrerPolicy="no-referrer-when-downgrade"></a></div></noscript>
        <!-- End of Statcounter Code -->
""", unsafe_allow_html=True
)

I insert the above code before I render any widgets (buttons, forms, expanders etc.)

Is that correct?

1 Like