1

I have started learning about Javascript and I am trying to include it to my Django Project to calculate the time users are on the page so I found the answer to this question HERE but I am unable to implement it as it is the first time for me to link between JS and Django Python backend.

Here is what I understand:

Step 1 Add the following script for all HTML templates:

    <script src="timeme.js"></script>
    <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>

Step 2 I am not sure exactly to place the below code to send the data to the backend to be read in Django Admin to know which user has been spending on each page

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","ENTER_URL_HERE",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var timeSpentOnPage = TimeMe.getTimeOnCurrentPageInSeconds();
xmlhttp.send(timeSpentOnPage);

My questions are:

  1. Where should I place the 2nd Code?
  2. Should I start a new app in my django project to read the file sent from the JS?
  3. What is the TimeMe.setCurrentPageName("my-home-page"); what variable should I add? is it the HTML name of the name in the URL?
  4. Are there an easier way to track how long a user has been in a page?
Shiko
  • 149
  • 9

0 Answers0