Google Analytics component

I am trying to make a simple component to enable google analytics, and I can’t seem to get it to work.

import streamlit.components.v1 as components


def google_analytics(google_analytics_id):
    components.html(
        """
        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src=f"https://www.googletagmanager.com/gtag/js?id={google_analytics_id}"></script>
        <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', google_analytics_id);
        </script>
	""",
        height=1,
        scrolling=False,
    )

Then in my app.py sidebar menu page, I have call the component. But I do not see the script loaded.

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 really want to use Google Analytics as I am interested in much more than just how many hits. I want to know geo location, sources, and other things.

Yeah, statcounter gives you all that too. Their location, IP address, country, and more data.

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.

1 Like

Understood, all the best with it :slight_smile:

Just reopening this thread to ask how do you install statcounter.
I’ve created an account and added the following in my main script.

components.html("""<!-- Default Statcounter code for Pears
https://pearstest.herokuapp.com -->
<script type="text/javascript">
var sc_project=12673269; 
var sc_invisible=1; 
var sc_security="1c2a1959"; 
</script>
<script type="text/javascript"
src="https://www.statcounter.com/counter/counter.js"
async></script>
<noscript><div class="statcounter"><a title="Web Analytics"
href="https://statcounter.com/" target="_blank"><img
class="statcounter"
src="https://c.statcounter.com/12673269/0/1c2a1959/1/"
alt="Web Analytics"
referrerPolicy="no-referrer-when-downgrade"></a></div></noscript>
<!-- End of Statcounter Code -->""")

However, startcounter cannot detect the code on my website.
Any help would be very appreciated :slight_smile:

Emmanuel

Just want to update this if anyone need it. Another alternative to track your Streamlit visits:

1 Like

an ugly hack:

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)
1 Like

Thanks. I just implemented the statcounter and super straight forward, stats is available right way.

Question:

What if I have a multiple page streamlit application, do I need to add the statcounter code on everypage? Is there any smart way of doing it?

How did you get statcounter to work on your site? I can see the code but statcounter is saying they are unable to access my site.

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.

1 Like

How can we install this plugin ? couldnt find it in pip

How can we install this plugin ? couldnt find it in pip

I just documented a solution here: GitHub - MarceloCajueiro/streamlit-html-injection-docker: Elegant solution for injecting HTML tags (Google Tag Manager, Analytics, SEO meta tags) into Dockerized Streamlit applications. Uses build-time SED injection for reliable, production-ready HTML customization without runtime overhead.

This is what i do to inject my gtag:

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.