Try statcounter, simpler to use and works. Same implementation as the above, but it works. Also, this seems relatively straight forward. I wouldn’t waste any further time on google analytics. But if you can get it to work like he did, then be my guest.
I still prefer to use Google Analytics for a few reasons, but I also want to know why it doesn’t work for future components. The link you posted to the one that got it working didn’t work. I did read a few convoluted ways to get it working, but I wanted to use a component as it feels like the right way to do it.
def inject_ga():
"""Add this in your streamlit app.py
see https://github.com/streamlit/streamlit/issues/969
"""
# new tag method
GA_ID = "google_analytics"
# NOTE: you should add id="google_analytics" value in the GA script
# https://developers.google.com/analytics/devguides/collection/analyticsjs
GA_JS = """
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX"> id="google_analytics" </script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX');
</script>
"""
# Insert the script in the head tag of the static template inside your virtual
index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
logging.info(f'editing {index_path}')
soup = BeautifulSoup(index_path.read_text(), features="lxml")
if not soup.find(id=GA_ID): # if cannot find tag
bck_index = index_path.with_suffix('.bck')
if bck_index.exists():
shutil.copy(bck_index, index_path) # recover from backup
else:
shutil.copy(index_path, bck_index) # keep a backup
html = str(soup)
new_html = html.replace('<head>', '<head>\n' + GA_JS)
index_path.write_text(new_html)
There is a basic example of how to implemente Google Analytics in Streamlit via bi-directional components API. A lot of things to improve, but it works.
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.