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=""></script>

Also, what about Google Adsense code?

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

Thanks in advance for support.


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 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!


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


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:


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

It would be really awesome to have this… really would like to track the users on the site I deployed using streamlit!


Any updates on this request?

1 Like

Not answers the question on using javascript code from google analytics, but allows measuring traffic by using a tracking pixel code through GA:

1 Like

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.


Your suggestion works for me. My site at now has GA. Many thanks!


Would like to share what worked for me. I made a flask website and put the streamlit app inside an iframe on one of the pages (the …/ai_app page). This way you have direct access to the head tag and so can easily insert the adsense or analytics code. In case you’d like to see how it turned out, the site is at (adsense says it’d take “a few days” to show, but analytics fully works right now).


This looks a very good approach. I tried to do it similar way, but while uploading image in the streamlit app from iframe it is giving 403 error. My streamlit app and flask website is running seperately on my lock desktop. Looks like streamlit server is not allowing flask server to upload the file. Am I doing things correctly. Can you please advise and guide me. Thanks !

I remember having a similar problem when uploading big images. Maybe try with a small (<30KB) one. I’m running the flask site with gunicorn and nginx. To allow larger images I had to modify the nginx configuration file.
Also, I’m not using the latest version of streamlit. I recall that one of the changes in more recent versions was that the uploader now requires to explicitly specify the decoding.

@suryatejreddy and @Quoc_Tran is this done through the terminal? I tried going to the path of the repo of my app and tried ls -a. There was not a (st. file) in the directory? Can you please guide me how to find it?


This is done through the terminal via a python session. For me:

import os
import streamlit as st
‘anaconda3/envs/st65/lib/python3.7/site-packages/streamlit’ If you still need help, we can chat through LinkedIn

suryatejreddy thanks for suggestion, worked great for me.

since I deploy automatically I needed to change the file using code and came up with the following lines in the app code that checks whether there is GA script in the index.html and if not inserts it from the ‘code’ variable.

code = """<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXXXXXXX');

with open(a, 'r') as f:
    if len(re.findall('UA-', data))==0:
        with open(a, 'w') as ff:

Hi there, maybe this could be useful streamlit-analytics 0.2.1

This wrecked my server… Cannot go back the tags are permanently in the static files now, how do we reset them ? Thanks

Edit : For those looking for a way, I resolved it by recreating the conda environment I used for the script. Apparently the static files are stored in it. If it’s the base env, you’ll need to create a new one.
The right way would be to fix the static index.html but I messed it and got a blank file ^^