Hi all.
Can someone please help me get Google Analytics to pick up how many people are using my app. I already have a Google Analytics account and I also already received my tracking ID. Give me an example How I should incorporate the tracking ID into my code.
Currently, there is no native integration of GA with Streamlit. You can use our analytics functionality, which is available for free when you deploy your apps on Streamlit Community Cloud. More info about it here.
Now if you really want to add GA to your app, as far as I know, the only way is to add the tracking code in an HTML file and embed it into your app using Streamlit’s components.html() function.
You need an HTML file (e.g., google_analytics.html) in the same directory as your Streamlit app.
Your HTML code should look like this:
<html>
<head>
<!-- Google Analytics tracking code -->
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOUR_TRACKING_ID');
</script>
</head>
<body></body>
</html>
Here’s what the streamlit/Python file may look like:
import streamlit as st
import streamlit.components.v1 as components
# Include Google Analytics tracking code
with open("google_analytics.html", "r") as f:
html_code = f.read()
components.html(html_code, height=0)
st.title("My Streamlit App")
Remember to replace YOUR_TRACKING_ID with your Google Analytics tracking ID!
Note that I have not personally tried this, but I suggest you give it a try and provide feedback to us here so that we can potentially investigate further.
Thank you very much for coming back to me on this. I have been struggling for almost three days to get it to work. I have tried your suggestions and it still doesn’t work. here is my GitHub repo, maybe you can have a look at what I am trying to do. GitHub - arnoldtRealph/lesson-planafr
I was looking for an alternative, because Workspace Analytics isn’t appearing on my dashboard header. If I had Workspace Analytics, it would suit my needs.
Thank you for giving attention to my concern.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.