Google analytics old way broke - what do you suggest?

Hello, I have a few apps running on streamlit community cloud - btw thank you for this service it really has been a superb on-ramp for my coding! I used to have google analytics on the deployed apps, successfully running and counting visitors. However, as of end of july, the method I used broke, getting error 013 - i believe there was an upgrade that didn’t allow for changes to the index.html. The old method that used to work was having the following at the top of my streamlit app code:

def inject_ga():
GA_ID = “google_analytics”

GA_JS = """
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXXXXXXXXX');
</script>
"""

index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
logging.info(f'editing {index_path}')
soup = BeautifulSoup(index_path.read_text(), features="html.parser")
if not soup.find(id=GA_ID):
    bck_index = index_path.with_suffix('.bck')
    if bck_index.exists():
        shutil.copy(bck_index, index_path)
    else:
        shutil.copy(index_path, bck_index)
    html = str(soup)
    new_html = html.replace('<head>', '<head>\n' + GA_JS)
    index_path.write_text(new_html)

inject_ga()

… rest of the code …

now this doesn’t work anymore, my initial solution I tried doing a ga.html file with the above js tag in it, and replaced the function with the following:
def inject_ga():
ga_code = open(‘ga.html’, ‘r’).read()
st.markdown(ga_code, unsafe_allow_html=True)

where I would use this function after initialization of the set_page_config : inject_ga().

Now this new method doesn’t count the visit unfortunately, I believe cause the js tag writing is not added to the head of the page.

Anybody has had the same issue and/or has an idea on how to fix this?
Thank you a lot in advance for the discussion!!

Bestest regards :slight_smile:

Hey @zmazz,

I’m checking with our cloud engineering team to see if there was a change made recently that might have caused this issue. I’ll post an update here when I have more info.

Hi @Caroline , are there any updates on that? I’m experiencing the exact same issue reported by @zmazz

@zmazz have you already discovered any properly working solutions for this problem?

1 Like

no i haven’t - just got the google analytics out of my scripts…

very eager to get an answer as well though, so thanks for the bump!

@Caroline