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.
Post-install script to inject Google Analytics into Streamlit
This script runs after pip install to modify the Streamlit static files
"""
import os
import sys
import streamlit as st
def inject_google_analytics():
"""Inject Google Analytics into Streamlit's index.html"""
# Get Streamlit installation directory
streamlit_dir = os.path.dirname(st.__file__)
index_file = os.path.join(streamlit_dir, 'static', 'index.html')
print(f"Streamlit directory: {streamlit_dir}")
print(f"Index file: {index_file}")
if not os.path.exists(index_file):
print(f"Error: Streamlit index.html not found at {index_file}")
return False
# Read the current file
with open(index_file, 'r', encoding='utf-8') as f:
content = f.read()
# Check if Google Analytics is already injected
if 'gtag' in content:
print("Google Analytics already injected")
return True
# Google Analytics code to inject
ga_code = ''' <!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-(yourtag)"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-(your tag)');
</script>
'''
# Inject after <head> tag
modified_content = content.replace('<head>', f'<head>\n{ga_code}')
# Write back to file
with open(index_file, 'w', encoding='utf-8') as f:
f.write(modified_content)
print("Google Analytics injected successfully!")
return True
if __name__ == "__main__":
print("Injecting Google Analytics into Streamlit...")
success = inject_google_analytics()
sys.exit(0 if success else 1)
Now just run “python inject_analytics.py” after the packages are installed. Although, this is not a good practice, it’s the only way it works.