A reliable, production-ready guide to adding Google Analytics (GA4) to your Streamlit applications using Docker build-time injection.
Streamlit doesn't provide a native way to add analytics tracking. You can't simply paste your Google Analytics or GTM code into your Python app. This makes it challenging to track:
- User behavior and engagement
- Page views and session duration
- Custom events and conversions
- Marketing campaign effectiveness
After testing multiple approaches (runtime injection, Python components, monkey patching), the only reliable solution is to modify Streamlit's HTML during Docker image build using sed
. This approach:
- β Works 100% of the time
- β Has zero runtime overhead
- β Is production-ready
- β Works with any deployment platform
Add to your Dockerfile
:
# Inject Google Analytics
ARG GA_ID
RUN sed -i "/<\/head>/i\
<script async src=\"https://www.googletagmanager.com/gtag/js?id=${GA_ID}\"></script>\
<script>\
window.dataLayer = window.dataLayer || [];\
function gtag(){dataLayer.push(arguments);}\
gtag('js', new Date());\
gtag('config', '${GA_ID}');\
</script>" \
$(python -c "import streamlit, os; print(os.path.join(os.path.dirname(streamlit.__file__), 'static', 'index.html'))")
Build with:
docker build --build-arg GA_ID=G-YOURCODE -t my-app .
For GTM, Facebook Pixel, Microsoft Clarity, and more advanced examples, check out the complete solution:
GitHub Repository: streamlit-html-injection-docker
The repository includes:
- π Modular scripts for different analytics providers
- π·οΈ Google Tag Manager with noscript support
- π Facebook Pixel integration
- π SEO meta tags injection
- π³ Docker Compose examples
- π Complete documentation
FROM python:3.9-slim
WORKDIR /app
# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy your Streamlit app
COPY app.py .
# Inject Google Analytics
ARG GA_ID
RUN sed -i "/<\/head>/i\
<script async src=\"https://www.googletagmanager.com/gtag/js?id=${GA_ID}\"></script>\
<script>\
window.dataLayer = window.dataLayer || [];\
function gtag(){dataLayer.push(arguments);}\
gtag('js', new Date());\
gtag('config', '${GA_ID}');\
</script>" \
$(python -c "import streamlit, os; print(os.path.join(os.path.dirname(streamlit.__file__), 'static', 'index.html'))")
EXPOSE 8501
CMD ["streamlit", "run", "app.py"]
Build and run:
# Build with your GA4 measurement ID
docker build --build-arg GA_ID=G-ABC123XYZ -t analytics-dashboard .
# Run the container
docker run -p 8501:8501 analytics-dashboard
# Good - flexible
ARG GTM_ID
ENV GTM_ID=${GTM_ID}
# Bad - hardcoded
ENV GTM_ID=GTM-ABC123
# 1. Build and run
docker build --build-arg GTM_ID=GTM-TEST123 -t test-app .
docker run -p 8501:8501 test-app
# 2. Check if injection worked
curl -s http://localhost:8501 | grep "GTM-TEST123"
# 3. Use browser DevTools
# - Check Network tab for gtm.js or analytics.js requests
# - Check Console for dataLayer object
# - Use Tag Assistant Chrome extension
- Check injection: View page source and search for your tracking ID
- Clear cache: Use incognito mode or clear browser cache
- Wait: Analytics can take 24-48 hours to show data
- Check filters: Ensure your IP isn't filtered in Analytics
# Debug the sed command
docker run --rm python:3.9-slim python -c "import streamlit, os; print(os.path.join(os.path.dirname(streamlit.__file__), 'static', 'index.html'))"
The Docker build-time injection with SED is the only reliable way to add Google Analytics to Streamlit apps. Other approaches (Python components, runtime injection) don't work consistently due to Streamlit's architecture.
This solution:
- β Works 100% of the time
- β Has zero runtime overhead
- β Is production-ready
- β Works on any platform (AWS, GCP, Azure, etc.)
Need GTM, Facebook Pixel, or SEO tags?
Check out the complete solution with modular scripts:
streamlit-html-injection-docker
Credits: Thanks to @snehankekre for the original SED suggestion on Streamlit forums.
Found this helpful? Star the repository and share with others facing the same challenge!