oTree Forum >

How to Track the Time Spent on a Page in oTree Studio?

#1 by Jaki

I’m looking to track the time (in seconds) that participants spend on a specific page in oTree Studio, from when the page loads until they click the "Next" button. 🧐I wonder if it's possible to achieve this using the timeout_seconds feature or through other methods. Could someone guide me on how to implement this?

#2 by BonnEconLab

I think the simplest way to track the time that participants spend on a page is to do so server-side.

Here is a simple example which assumes that your experiment includes three pages, Intro, PageOne, and PageTwo:

In your __init__.py, include

import time


class Player(BasePlayer):

    Intro_offset = models.FloatField()
    PageOne_offset = models.FloatField()
    PageTwo_offset = models.FloatField()


class Intro(Page):

    @staticmethod
    def before_next_page(player, timeout_happened):
        player.Intro_offset = time.time()


class PageOne(Page):

    @staticmethod
    def before_next_page(player, timeout_happened):
        player.PageOne_offset = time.time()


class PageTwo(Page):

    @staticmethod
    def before_next_page(player, timeout_happened):
        player.PageTwo_offset = time.time()


The time (in seconds) that a participant spent on PageOne is then player.PageOne_offset - player.Intro_offset, and the time spent on PageTwo is player.PageTwo_offset - player.PageOne_offset.

Server-side recording of the time spent on a page has the drawback that it is subject to any delays caused by the data transmission between the participant’s device and the oTree server. That is, if a participant’s internet connection is slow or unreliable, the measurement will be imprecise. This will most likely not be an issue when using oTree for a lab experiment but may be an issue when conducting an online study.

Instead of before_next_page, one could also use vars_for_template. Then, however, the time recorded will be affected by participants refreshing the page, since this triggers another execution of vars_for_template. For a more complicated solution, see https://www.otreehub.com/forum/602/.

#3 by Jaki

Thank you for providing such a detailed explanation! Really appreciated it,it helped a lot😊

Write a reply

Set forum username