有没有办法跟踪我的网站的访问量?如果是,有没有办法将计数器添加到streamlit应用程序中?
发布于 2021-02-10 02:11:22
最简单的方法是使用Google Analytics:
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上为您的项目生成的值。
https://stackoverflow.com/questions/65114309
复制相似问题