How to know the time a user spends on a page in a Django Project

Submitted 3 years, 5 months ago
Ticket #284
Views 369
Language/Framework Django
Priority Medium
Status Closed

Hello, 

I want to know if there is a way to get the time spent by each user on each page created in a Django Project.

If there is how do I implement it? 

Thank you

Submitted on Nov 05, 20
add a comment

1 Answer

Verified

We can achieve this by using some Javascript library, 

Find below the high level draft version you can use it,And one more thing which is not related to Django Project alone, we can apply the same concept in any backend framework,

We can write our own Javascript like below,

$(document).ready(function() {
  var startDate = new Date();

  $(window).unload(function() {
      var endDate = new Date();
      $.ajax({ 
        type: POST,
        url: "/backendurl",
        data: {'timeSpent': endDate - startDate},
       
      })
   });
});

Or another approach is using TimeJS library to handle it

<script src="TimeMe.js"></script>
<script type="text/javascript">
TimeMe.initialize({
    currentPageName: "page you want", // page name
    idleTimeoutInSeconds: 15 // time before user considered idle
});
</script>

and push the data to Django backend using above Jquery POST call

Submitted 3 years, 5 months ago

Could you give me an example of how it works in step as I am new to Javascript after adding the Javascript above to each template?

- ahmed.hisham87 3 years, 5 months ago

@ahmed above is an example , just try to use the above example in your code and it should work.

- Vengat 3 years, 5 months ago

as this ticket more specific to question and its already answered. Can you close the ticket?

- Vengat 3 years, 5 months ago


Latest Blogs