【问题标题】:How to add the Google Analytics tag to website developed with Streamlit?如何将 Google Analytics 标签添加到使用 Streamlit 开发的网站?
【发布时间】:2023-01-26 01:45:13
【问题描述】:

我想在 Google Analytics 中跟踪我使用 Streamlit 构建的 UI。 根据 Google Analytics,需要将以下内容添加到 HTML 的 <head> 部分。

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

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

由于我的代码架构中没有任何 html 文件,因此我在主 .py 文件中包含了以下 Streamlit 行:

google_analytics_js = '[previous_html_code]'
st.components.v1.html(google_analytics_js)

不幸的是,该网站未被跟踪。对此的任何帮助将不胜感激。

【问题讨论】:

标签: python html google-analytics fastapi streamlit


【解决方案1】:

由于组件 HTML 将被添加到 iframe,您需要允许其脚本访问父级:

<script crossorigin='anonymous'>
add script
</script>

除此之外,每当脚本访问windowdocument时,您应该使用parent.windowparent.document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2017-12-01
    相关资源
    最近更新 更多