简体   繁体   English

测量用户停留在网页上的时间并为管理员显示(Python-Django)

[英]Measure time a user stays in a webpage and display it for the admin (Python - Django)

I have a website built in django. 我有一个在Django建立的网站。 All pages are static. 所有页面都是静态的。 Now I'm trying to measure time each user spends in each page and display it in a table. 现在,我正在尝试衡量每个用户在每个页面上花费的时间,并将其显示在表格中。 Currently I'm working on an idea I got in another similar topic as in the link: How to measure a time spent on a page? 目前,我正在研究一个想法,该想法与链接中的另一个主题类似: 如何计算在页面上花费的时间?

Firstly I have placed this in the header of the page which imports the TimeMe.js library and initializes it. 首先,我将其放置在导入TimeMe.js库并对其进行初始化的页面的标题中。 This captures the time a user spends on the webpage: 这捕获了用户在网页上花费的时间:

<script type="text/javascript">
            TimeMe.setIdleDurationInSeconds(15);
            TimeMe.setCurrentPageName("my-home-page");
            TimeMe.initialize();
            window.onload = function(){
                setInterval(function(){
                    var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
                    document.getElementById('timeInSeconds').textContent = timeSpentOnPage.toFixed(2);
                }, 25);
            }
            </script>

Then I have the following for sending the measured time to the server (I'm working locally, in my laptop): 然后,我将以下内容用于将测得的时间发送到服务器(我在本地工作,在我的笔记本电脑中):

window.onbeforeunload = function (event) {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("POST","http://127.0.0.1:8000",false);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
    xmlhttp.send(timeSpentOnPage);
};

I'm a beginner in Javascript and I don't understand where should I use the second piece of code to post data to the server in order to display them in a view. 我是Java语言的初学者,但我不明白我应该在哪里使用第二段代码将数据发布到服务器,以便在视图中显示它们。 Is the format of the code correct? 代码格式正确吗?

Thank you 谢谢

Sending the data onunload may or may not work. onunload发送数据可能会或可能不会。

Another way would be to ping your server every x seconds, so you know that the use has the page still open. 另一种方法是每隔x秒对服务器执行一次ping操作,因此您知道使用情况仍会打开该页面。

setInterval(function () {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("POST", "http://127.0.0.1:8000",false);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send();
}, 15);

The ping sends the session cookie along with it, so you know which user it is and can count the time on the server. ping随同发送会话cookie,因此您知道它是哪个用户,并且可以计算服务器上的时间。

However, if you just want to know how much time each user spends on your pages, its probably easier to add Google Analytics (or a self-hosted alternative like Piwik ) to your site and have the metrics collected and presented in a tested and reliable way. 但是,如果您只想知道每个用户在页面上花费了多少时间,则将Google Analytics(分析)(或类似Piwik的自托管替代方案)添加到您的网站中,并收集经过测试且可靠的指标来显示可能会更容易方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM