Skip to content

Instantly share code, notes, and snippets.

@MarceloCajueiro
Created June 25, 2025 21:28
Show Gist options
  • Save MarceloCajueiro/edab028195b8a5e9ed45a357951b2d60 to your computer and use it in GitHub Desktop.
Save MarceloCajueiro/edab028195b8a5e9ed45a357951b2d60 to your computer and use it in GitHub Desktop.
How to Add Google Analytics to Streamlit Apps

How to Add Google Analytics to Streamlit Apps

A reliable, production-ready guide to adding Google Analytics (GA4) to your Streamlit applications using Docker build-time injection.

🎯 The Problem

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

πŸ’‘ The Solution: Docker Build-Time Injection with SED

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

Quick Implementation

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 .

Want Google Tag Manager or Other Analytics?

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

🎨 Complete Example: Analytics Dashboard

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

πŸš€ Best Practices

1. Use Environment Variables

# Good - flexible
ARG GTM_ID
ENV GTM_ID=${GTM_ID}

# Bad - hardcoded
ENV GTM_ID=GTM-ABC123

2. Test Your Implementation

# 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

πŸ”§ Troubleshooting

Analytics not showing up?

  1. Check injection: View page source and search for your tracking ID
  2. Clear cache: Use incognito mode or clear browser cache
  3. Wait: Analytics can take 24-48 hours to show data
  4. Check filters: Ensure your IP isn't filtered in Analytics

Docker build fails?

# 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'))"

🎯 Conclusion

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.)

πŸ“š More Resources

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment