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

Hi all,

I would like to monitor the traffic on my app using Google Analytics. What is the best way to achieve this?
I need to insert something like that

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-44444444-1"></script>

Also, what about Google Adsense code?

I hope there is a way to inject these in Steamlit somehow.

Thanks in advance for support.

3 Likes

It is very proud to see Streamlit become so far :slight_smile: I believe this will go bigger and bigger :pray:

1 Like

Hi @mzeidhassan.

There are a couple of javascript hacks using st.bokeh_chart in the Gallery at awesome-streamlit.org. In the current implementation you need to click a button or move a slider for it to run.

But i think that somebody with JS skills can find a workaround for that.

I havent understood the problem with JS. In All the other frameworks like bokeh, panel, voila and Dash you Can do it.

1 Like

Thanks @Marc for your kind reply. You have been doing great things for Streamlit, so I want to thank you for that.

Streamlit is amazing indeed, but we should be able in an easy way to add JS scripts for things like Google Analytics, Google Adsense, etc.

Do you think I should create a github issue for that?

Thanks again for your support!

1 Like

I’ll save you a step: I made an enhancement request for the ability to use arbitrary Javascript blocks here. :bow_and_arrow:

1 Like

It’s not a technical problem so much as it is a question of which features to develop before other ones.

Currently we’re working on a couple of performance boosting improvements especially to do with serialization of large datasets, making caching more reliable, and polishing a lot of existing features. It’s all about making good on the promise of being able to do beautiful things with data, first and foremost.

Meanwhile, I personally spent most of December gathering user stories as per desires for the use of HTML and Javascript in Streamlit. We definitely get that these limitations are preventing certain types of uses. And this month we have a couple of PRs in the making in response to those user stories.

Thanks for all your continued bug reports and feature suggestions. We read them all. :heart:

2 Likes

Thanks a million @nthmost for creating the github issue. I appreciate it. I hope we will see this feature added soon. This will be an amazing and much-needed addition to Streamlit. Thanks again for creating such a great framework. :+1:

1 Like