如何将Google Analytics标签添加到使用Streamlit开发的网站



我想在Google Analytics中跟踪我使用Streamlight构建的UI。根据谷歌分析,以下内容需要添加到<头部>部分。

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

不幸的是,该网站未被跟踪。如有任何帮助,我们将不胜感激。

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

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

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

最新更新