Google Analytics integration

Hi all.
Can someone please help me get Google Analytics to pick up how many people are using my app. I already have a Google Analytics account and I also already received my tracking ID. Give me an example How I should incorporate the tracking ID into my code.

Thank you.

1 Like

Hi @Arnoldt_Visagie! :wave:

Currently, there is no native integration of GA with Streamlit. You can use our analytics functionality, which is available for free when you deploy your apps on Streamlit Community Cloud. More info about it here.

Now if you really want to add GA to your app, as far as I know, the only way is to add the tracking code in an HTML file and embed it into your app using Streamlit’s components.html() function.

You need an HTML file (e.g., google_analytics.html) in the same directory as your Streamlit app.

Your HTML code should look like this:

<html>
  <head>
    <!-- Google Analytics tracking code -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_TRACKING_ID"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'YOUR_TRACKING_ID');
    </script>
  </head>
  <body></body>
</html>

Here’s what the streamlit/Python file may look like:

import streamlit as st
import streamlit.components.v1 as components

# Include Google Analytics tracking code
with open("google_analytics.html", "r") as f:
    html_code = f.read()
    components.html(html_code, height=0)

st.title("My Streamlit App")

Remember to replace YOUR_TRACKING_ID with your Google Analytics tracking ID! :smiling_face:

Note that I have not personally tried this, but I suggest you give it a try and provide feedback to us here so that we can potentially investigate further.

Good luck!

Charly

Thank you very much for coming back to me on this. I have been struggling for almost three days to get it to work. I have tried your suggestions and it still doesn’t work. here is my GitHub repo, maybe you can have a look at what I am trying to do. GitHub - arnoldtRealph/lesson-planafr

Thanks, @Arnoldt_Visagie.

Sure, I can liaise with a few people internally and see whether there’s a way to make it work for you.

Just before I do so, may I ask why the solution proposed by our community cloud platform is not suiting your needs?

Best,
Charly

I was looking for an alternative, because Workspace Analytics isn’t appearing on my dashboard header. If I had Workspace Analytics, it would suit my needs.
Thank you for giving attention to my concern.

1 Like

Thanks, @Arnoldt_Visagie!

I don’t know any other workaround, although, did you check out @jrieke’s analytics add-on?

Charly

2 Likes

I tried jrieke"s track visualizer. It fits my needs. Thank you very much for the effort.

1 Like

Glad to hear that @jrieke’s mighty component is doing what you’re after! :hugs:

Happy Streamlitin’! :balloon:

Charly

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.