首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查看网站访问统计数据Heroku / Streamlit

查看网站访问统计数据Heroku / Streamlit
EN

Stack Overflow用户
提问于 2020-12-03 02:51:10
回答 1查看 168关注 0票数 2

有没有办法跟踪我的网站的访问量?如果是,有没有办法将计数器添加到streamlit应用程序中?

EN

回答 1

Stack Overflow用户

发布于 2021-02-10 02:11:22

最简单的方法是使用Google Analytics:

代码语言:javascript
复制
anlytcs_code = """<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-XXXXXXXXX');
</script>"""

# Fetch the path of the index.html file
path_ind = os.path.dirname(st.__file__)+'/static/index.html'

# Open the file
with open(path_ind, 'r') as index_file:
    data=index_file.read()

    # Check whether there is GA script
    if len(re.findall('UA-', data))==0:

        # Insert Script for Google Analytics
        with open(path_ind, 'w') as index_file_f:

            # The Google Analytics script should be pasted in the header of the HTML file
            newdata=re.sub('<head>','<head>'+anlytcs_code,data)

            index_file_f.write(newdata)

在使用上面的代码时,将id的值替换为Google Analytics上为您的项目生成的值。

Reference

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65114309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档