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):
        <!-- Global site tag (gtag.js) - Google Analytics -->
        <script async src=f"{google_analytics_id}"></script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', google_analytics_id);

Then in my 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: